我有一个YouTubeBaseActivity它运作良好,但当我点击进入另一个活动,然后返回该活动时,它不会自动播放。
我希望它能够恢复并再次开始播放列表。
以下是以下课程:
public class VideoPlayer extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,View.OnClickListener{
private YouTubePlayerView ytp_mainvideo;
private YouTubePlayer vyouTubePlayer;
private YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener;
boolean fullScreen =false;
TextView txtMainVideo;
List<String> videoList;
static String currentKey="";
Utilities utilities = new Utilities(this);
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save state
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
if (vyouTubePlayer == null) {
ytp_mainvideo.initialize(Config.DEVELOPER_KEY, this);
}
}
@Override
protected void onStop() {
super.onStop();
if(vyouTubePlayer!=null)
vyouTubePlayer.cueVideos(videoList);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.vidsplash);
currentKey = utilities.getLicenseKey();
// Initialize the Main Video Player.
ytp_mainvideo =(YouTubePlayerView)findViewById(R.id.youtubPlayerView);
ytp_mainvideo.initialize(Config.DEVELOPER_KEY, this);
txtMainVideo =(TextView)findViewById(R.id.text_mainvideo);
txtMainVideo.setOnClickListener(this);
Resources res = getResources();
String[] varray = res.getStringArray(R.array.videolist);
videoList = Arrays.asList(varray);
mPlayerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {
}
@Override
public void onLoaded(String s) {
}
@Override
public void onAdStarted() {
}
@Override
public void onVideoStarted() {
}
@Override
public void onVideoEnded() {
Log.d("ShowCase", "Video Ended");
if(vyouTubePlayer != null)
vyouTubePlayer.loadVideos(videoList);
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
};
if(currentKey.compareTo("")==0) {
Intent i = new Intent(getApplicationContext(), NewStartActivity.class);
startActivity(i);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.text_mainvideo:
Intent intentsub=new Intent(getApplicationContext(),CateMainActivity.class);
startActivity(intentsub);
break;
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
vyouTubePlayer =youTubePlayer;
youTubePlayer.setPlayerStateChangeListener(mPlayerStateChangeListener);
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
youTubePlayer.loadVideos(videoList);
// Hiding player controls
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
@Override
public void onFullscreen(boolean _isFullScreen) {
fullScreen = _isFullScreen;
}
});
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Toast.makeText(appContext, "BAck", Toast.LENGTH_LONG).show();
AlertDialog.Builder alert = new AlertDialog.Builder(
VideoPlayer.this);
alert.setTitle(getString(R.string.app_name));
alert.setIcon(R.drawable.app_icon);
alert.setMessage("Are You Sure You Want To Quit?");
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//you may open Interstitial Ads here
finish();
}
});
alert.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:3)
首先宣布YoutubePlayer&amp; seekTime变量
private YouTubePlayer youTubePlayer;
private int seekTime = 0;
然后在OnInitializationSuccess方法中设置为youtubeplayer
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
if (!wasRestored) {
youTubePlayer.loadVideo(videoId);
}
}
之后,您可以在onPause中存储视频当前时间,并可以使用下面的技巧在onResume方法中恢复视频
@Override
protected void onResume() {
super.onResume();
if (youTubePlayer == null) {
getYouTubePlayerProvider();
} else {
youTubePlayer.loadVideo(videoId, seekTime);
}
}
@Override
protected void onPause() {
super.onPause();
seekTime = youTubePlayer.getCurrentTimeMillis();
}
它对我来说很好,也适合你。保持编码:)
答案 1 :(得分:2)
试试这个
protected void onResume() {
super.onResume();
if (vyouTubePlayer == null) {
ytp_mainvideo.initialize(Config.DEVELOPER_KEY, this);
}
else {
vyouTubePlayer.Play();
}
}
我认为它会在OnPause暂停,因此您需要拨打Play()
答案 2 :(得分:0)
我的解决方案是创建第二个API密钥。这是一个非常罕见的问题,我很感谢Faceles的回应,这些回应让我解决了这个问题。在这个项目中我使用了2个Youtube播放器,所以当我第二个播放器移动到另一个活动并返回到第一个播放器时它从未加载。