我有一个名为VideoActivity的活动。在youtubeplayerview下面有一系列视频。第一次加载视频时,当我点击某个项目然后转到另一个活动,然后返回到该活动时,它不会播放。请问原因是什么?
以下是代码:
public class VideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public Video video;
private RecyclerView recyclerVideos;
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerView youTubeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
recyclerVideos = (RecyclerView) findViewById(R.id.recyclerVideos);
Intent i = getIntent();
video = (Video) i.getSerializableExtra("video");
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerVideos.setLayoutManager(layoutManager);
new VideoAsyncTask().execute();
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(video.Videoid);
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Constants.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
private class VideoAsyncTask extends AsyncTask<Void, Void, Video> {
@Override
protected void onPreExecute() {
}
@Override
protected Video doInBackground(Void... params) {
try {
return webservice();
} catch (Exception e1) {
e1.printStackTrace();
return null;
}
}
protected void onPostExecute(final Video video) {
try {
if (video == null) {
return;
}
VideosAdapter videoAdapter = new VideosAdapter(video.videos,VideoActivity.this);
recyclerVideos.setAdapter(videoAdapter);
youTubeView.initialize(Constants.DEVELOPER_KEY, VideoActivity.this);
} catch (Throwable ignored) {}
}
}
}