2016-12-12 7 views
0

私は衛星放送からテレビチャンネルを流すことができるAndroidアプリを持っています。アプリケーションのメディアプレーヤーはMotionEventクラスを実装しているので、画面上で左から右にスワイプすると、アプリケーションはサーバーにリストの次のチャンネルをストリーミングするリクエストを送信します。サーバーは一度に1つのリクエストしか受け取れませんが、複数回スワイプしてチャンネルを複数回変更することを意味します。チャンネルが切り替わるには5〜8秒かかり、その時間帯には画面にスピナーが表示され、現在のチャンネルはストリーミングを続けますJavaまたはAndroidのロジックを実装して、実行する前に特定の時間、追加のユーザー入力を待つ方法はありますか?

私は待ち時間タイマーを実装したいと思います1秒で、その要求を送信する前にユーザーが追加のスワイプを許可します。たとえば、私は現在チャンネル1を見ています。チャンネル2に切り替えるために右にスワイプします。その1秒の待機タイマーで、もう一度スワイプしてチャンネルを次のチャンネルの次のチャンネルに変更する要求を更新します。また、ユーザーが追加のスワイプをしたときにもう1秒間待機するように、待機タイマーもリセットされます。

私のコードは、PlayerActivityクラスです。このメソッドは、ユーザーが画面上で右にスワイプしたときにonSwipeRightを呼び出し、ユーザーが画面上で左にスワイプしたときに左にスワイプします。左にスワイプすると、前のチャンネルのサーバーが要求されます。

@Override 
    public boolean onTouchEvent(MotionEvent event){ 
     switch (event.getAction()){ 
      case MotionEvent.ACTION_DOWN: 
       x1 = event.getX(); 
       break; 
      case MotionEvent.ACTION_UP: 
       x2 = event.getX(); 
       float diffX = x2 - x1; 
       if (Math.abs(diffX) > Min_distance && player_details.getVisibility() == View.INVISIBLE){ 
        if (diffX > 0){ 
         onSwipeRight(); 
        }else { 
         onSwipeLeft(); 
        } 
       }else { 
        toggleControlsVisibility(); 
       } 
       break; 
     } 
     return super.onTouchEvent(event); 

onSwipeRight()のロジックはそうのようなものです:

public void onSwipeRight(){ 

switch_index = Integer.valueOf(contentManager.readData("current_channel_position", playerActivity.this)); 

     previous_major = majorList.get(switch_index); 
     previous_minor = minorList.get(switch_index); 
     previous_name = nameList.get(switch_index); 

     if (switch_index == 0){ 
      switch_index = majorList.size() - 1; 
     }else{ 
      switch_index = switch_index - 1; 
     } 

     contentManager.saveData(String.valueOf(switch_index),"current_channel_position", playerActivity.this); 

     switch_major = majorList.get(switch_index); 
     switch_minor = minorList.get(switch_index); 
     switch_name = nameList.get(switch_index); 


     final String tuner = tunelink+"Major="+switch_major+"&Minor="+switch_minor+MainActivity.resolution; 

     new Thread(new Runnable() { 
      @TargetApi(Build.VERSION_CODES.KITKAT) 
      @Override 
      public void run() { 
       String playlive =""; 
       String tune_response = tuneConnection.get_response(tuner); 
       if (tune_response.contains("successful")){ 

        do { 
         String hlsstatus_response = hlsstatusConnection.get_response(gethlslink); 
         Matcher matcher = Pattern.compile("<hls_link>(.+?)</hls_link>").matcher(hlsstatus_response); 
         while (matcher.find()) { 

          playlive = matcher.group(1); 
         } 

         playlink = "http://" + Main2Activity.ip + "/" + playlive; 
         Log.d(tag, "Swipte right hls response: " + playlink); 

        }while (Objects.equals(playlive, "none")); 

        player.stop(); 
        if (airingList.get(switch_index)!= null) { 
         String response = streamConnection.get_response(playlink); 
         if (response != null) { 
          thumbnailConnection.get_response(airingList.get(switch_index)); 
         } 
        }else { 
         String response = streamConnection.get_response(playlink); 
         Message mg = Message.obtain(); 
         mg.obj = "no airing"; 
         noairing_handler.sendMessage(mg); 
        } 
       } 



      } 
     }).start();  


    } 

答えて

0

これを実装するには、いくつかの方法があります。サーバーでは、要求がまだ処理されているかどうかを確認し、クライアントが新しいチャネルを取得するために呼び出したときに要求を取り消すことができます。

私の意見では、swiperightイベントがAPI呼び出しを行う前に、チャネルスイッチの後に約2秒の遅延を設定する方が簡単です。このようにして、ユーザーが右にスワイプすると(速くても)、チャンネルのロックインを開始して読み込みを開始する前に、再びswiperightする時間があります。

+0

どのように2秒間の遅延を設定するとよいでしょうか?私は解決策を探そうとしましたが、見つけられるのはThread.sleep()で、タッチイベントを聴いて欲しいので、ここに当てはまるとは思いません。 –

+0

スワイプ・ライト・イベントが呼び出されたら、実行時間を開始するx delayを持つタイマーを実行します。タイマーがなくなり、実行可能ファイルを開始する前に別のスワイプ権利イベントがある場合は、タイマーを強制終了してリセットします。 –

+0

https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html –

関連する問題