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);
}
};
嗯...你好嗎?任何人?請!!! –