2016-12-14 92 views
1

我在我的android項目中實現了一個youtube播放器(片段)。我試圖以無鉻播放器的風格播放視頻,同時自己實施播放器控制。播放和暫停按鈕工作正常,但是當視頻正在播放時,seekbar不會移動。雖然,我可以通過它跳過視頻(提前播放視頻),但搜索欄並沒有獨立運行。我哪裏做錯了?爲什麼youtube視頻不能在android中播放?

@Override 
     public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { 
      if (!b) { 
       YPlayer = youTubePlayer; 
       displayCurrentTime();  // It displays current progress of video in --:-- format. It's working fine... 
       YPlayer.loadVideo(id);  // id -> YouTube video id 
       YPlayer.play(); 
       YPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS); 
       YPlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() { 
        @Override 
        public void onLoading() { 
        } 

        @Override 
        public void onLoaded(String s) { 
        } 

        @Override 
        public void onAdStarted() { 
        } 

        @Override 
        public void onVideoStarted() { 
         seekBar.setMax(YPlayer.getDurationMillis()/1000); 
        } 

        @Override 
        public void onVideoEnded() { 
        } 

        @Override 
        public void onError(YouTubePlayer.ErrorReason errorReason) { 
        } 
       }); 

       YPlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() { 
        @Override 
        public void onPlaying() { 
         handler.postDelayed(runnable, 100); //Handler's instance initialized as instance variable 
         displayCurrentTime(); 
        } 

        @Override 
        public void onPaused() { 
         handler.removeCallbacks(runnable); 
        } 

        @Override 
        public void onStopped() { 
        } 

        @Override 
        public void onBuffering(boolean b) { 
         handler.postDelayed(runnable, 1000); 
        } 

        @Override 
        public void onSeekTo(int i) { 
         handler.removeCallbacks(runnable); 
        } 
       }); 
       playerControl.setVisibility(View.VISIBLE); //Layout with play (and pause) button, Seekbar (which isn't moving along with the video), and timer (string being returned by the above displayCurrentTime() method) 
      } 
     } 

     @Override 
     public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 
     } 
    }); 
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      long lengthPlayed = (YPlayer.getDurationMillis() * progress)/100; 
      YPlayer.seekToMillis((int) lengthPlayed); 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      while(YPlayer != null && seekBar.getMax() > YPlayer.getCurrentTimeMillis()) 
      { 
       int videoProgressed = 0; 
       try 
       { 
        Thread.sleep(1000); 
        videoProgressed = YPlayer.getCurrentTimeMillis(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
       seekBar.setProgress(videoProgressed); 
      } 
     } 
    }); 


    private void displayCurrentTime() { 
     if (YPlayer != null) { 
      String formattedTime = formatTime(YPlayer.getCurrentTimeMillis()); 
      playTime.setText(formattedTime); 
     } 
    } 

    private String formatTime(int millis) { 
     int seconds = millis/1000; 
     int minutes = seconds/60; 

     return String.format("%02d:%02d", minutes % 60, seconds % 60); 
    } 

    private Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
      if(YPlayer != null) { 
       displayCurrentTime(); 
      } 
      handler.postDelayed(this, 1000); 
     } 
    }; 
+0

嗯...你好嗎?任何人?請!!! –

回答

0

檢查此tutorial如果它可以幫助你。本教程是關於Android建築音頻播放器的。這裏討論如何構建一個簡單的音頻播放器,如播放,暫停,前進,後退,下一首,上一首,播放列表和搜索欄等基本控件。

只需檢查教程解釋如何更新在使用Handler在後臺運行的後臺線程中實現的進度條計時器的代碼。您可以在這裏查看如何實施搜索欄。

欲瞭解更多相關信息,請檢查相關SO question

相關問題