Youtube 播放器视频未加载
Youtube Player video not loading
我有一个名为 VideoActivity 的 activity。在 youtubeplayerview 下方有一个列表 videos.On第一次视频是 loading.When 我点击一个项目然后转到另一个 activity 然后 return 到那个 activity,它不播放。我可以知道原因吗?
这是代码
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) {}
}
}
}
@Override
public void onResume(){
super.onResume();
try {
if (video == null) {
return;
}
YouTubePlayer.loadVideo(String videoId, int timeMillis);
} catch (Throwable ignored) {}
}
离开时尝试完成 activity,
或在恢复时初始化 youtubeplay 而不是 oncreate
@Override
public void onResume(){
super.onResume();
new VideoAsyncTask().execute();}
Yes.I 解决了问题。
第 1 步:将 youtubeview 添加到线性布局
步骤 2:在 onResume 上删除视图并在 return 到 [=27] 时再次将 youtubeview 添加到 linearlayout =]
第 3 步 : 停止后退 youtube 视频
在离开 activity/fragment YouTubePlayerView 所在位置之前,请执行以下操作:
youtubePlayer.pause();
youtubePlayer.release();
我的清单示例
<application
android:name=".UGNEWS24"
android:allowBackup="true"
android:debuggable="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ugnews24_rounded_logo"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@drawable/ugnews24_rounded_logo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="HardcodedDebugMode">
<activity android:name=".activity.WebViewActivity"></activity>
<activity android:name=".SearchResultsActivity" />
我有一个名为 VideoActivity 的 activity。在 youtubeplayerview 下方有一个列表 videos.On第一次视频是 loading.When 我点击一个项目然后转到另一个 activity 然后 return 到那个 activity,它不播放。我可以知道原因吗?
这是代码
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) {}
}
}
}
@Override
public void onResume(){
super.onResume();
try {
if (video == null) {
return;
}
YouTubePlayer.loadVideo(String videoId, int timeMillis);
} catch (Throwable ignored) {}
}
离开时尝试完成 activity, 或在恢复时初始化 youtubeplay 而不是 oncreate
@Override
public void onResume(){
super.onResume();
new VideoAsyncTask().execute();}
Yes.I 解决了问题。
第 1 步:将 youtubeview 添加到线性布局
步骤 2:在 onResume 上删除视图并在 return 到 [=27] 时再次将 youtubeview 添加到 linearlayout =]
第 3 步 : 停止后退 youtube 视频
在离开 activity/fragment YouTubePlayerView 所在位置之前,请执行以下操作:
youtubePlayer.pause();
youtubePlayer.release();
我的清单示例
<application
android:name=".UGNEWS24"
android:allowBackup="true"
android:debuggable="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ugnews24_rounded_logo"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@drawable/ugnews24_rounded_logo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="HardcodedDebugMode">
<activity android:name=".activity.WebViewActivity"></activity>
<activity android:name=".SearchResultsActivity" />