在 android 的视频视图中播放 Youtube 视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21278633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play Youtube videos in Video View in android
提问by varghesekutty
I am developing a youtube player in android. I am getting the rtsp video correct url. But still the video is not playing. Please help to me to find a solution.
我正在 android 中开发一个 youtube 播放器。我正在获取 rtsp 视频的正确网址。但是视频仍然没有播放。请帮我找到解决办法。
Thanks in advance
提前致谢
Here is my code
这是我的代码
String youtubeURL="rtsp://v6.cache4.c.youtube.com/CigLENy73wIaHwmh5W2TKCuN2RMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_online_video_player);
videoView = (VideoView) findViewById(R.id.video_View);
progressDialog = ProgressDialog.show(OnlineVideoPlayer.this, "", "Buffering video...",true);
progressDialog.setCancelable(false);
PlayVideo();
}
private void PlayVideo()
{
try {
final VideoView videoView =(VideoView)findViewById(R.id.video_View);
//1 //mediaController = new MediaController(Splashscreen.this);
//2 //mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse(youtubeURL);
//videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
}catch(Exception e){
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.getMessage());
}
采纳答案by neo
Here is another working code
这是另一个工作代码
MainActivity.java
主活动.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("rtsp://r2---sn-a5m7zu76.c.youtube.com/Ck0LENy73wIaRAnTmlo5oUgpQhMYESARFEgGUg5yZWNvbW1lbmRhdGlvbnIhAWL2kyn64K6aQtkZVJdTxRoO88HsQjpE1a8d1GxQnGDmDA==/0/0/0/video.3gp");
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
activity_video_view.xml
activity_video_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="@+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
回答by Roman Black
You can't do that. Use YouTubeAndroidApifor playing YouTube videos.
你不能那样做。使用YouTubeAndroidApi播放 YouTube 视频。
回答by Ajay
There might be many reasons to not play the video,You can test it on other devices also set android:hardwareAccelerated="true" in manifest
不播放视频的原因可能有很多,您可以在其他设备上进行测试,也可以在 manifest 中设置 android:hardwareAccelerated="true"
try following listeners to get correct issue
尝试关注听众以获得正确的问题
playerview.setOnPreparedListener(preparelistener);
playerview.setOnErrorListener(errorlistenr);
playerview.setOnCompletionListener(completelistener);