如何在我的Android应用程序中播放YouTubevideo?

如何在我的应用程序中播放YouTubevideo? 我想直接从YouTube上直接play video而无需下载。 另外,在播放video时,我想提供菜单选项。 我不想用默认的意图播放video。 我该怎么做?

这是btn点击事件

 btnvideo.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=Hxy8BZGQ5Jo"))); Log.i("Video", "Video Playing...."); } }); 

这种types,它在YouTube的另一个页面打开,你可以显示你的video

查看官方的YouTube Android播放器API 。

脚步

  1. 用菜单选项为您的播放器(全屏)屏幕创build一个新的活动。 在不同的线程中运行mediaplayer和UI。

  2. 对于播放媒体 – 一般来说播放audio/video有在android的mediaplayer api。 FILE_PATH是文件的path – 可能是url(youtube)stream或本地文件path

      MediaPlayer mp = new MediaPlayer(); mp.setDataSource(FILE_PATH); mp.prepare(); mp.start(); 

另外检查: Android YouTube应用播放video意图已经详细讨论了这一点。

你可以看看Android-Youtube-Player应用程序,可以在这里find。
在这个示例应用程序中,youtubevideo在videoview而不是webview中播放。

希望这个链接将帮助你http://android-coding.blogspot.in/2011/03/simple-example-using-videoview-to-play.html

要将YouTubevideo转换为rtsp,请使用此github代码 – youtube-url2rtsp

这个答案可能真的很晚,但它很有用。

您可以使用android-youtube-player在应用中播放youtubevideo。

一些代码片段:

要播放url中包含videoID的YouTubevideo,只需调用OpenYouTubePlayerActivity意图

 Intent intent = new Intent(null, Uri.parse("ytv://"+v), this, OpenYouTubePlayerActivity.class); startActivity(intent); 

其中v是videoID。

在清单文件中添加以下权限:

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 

并在清单文件中声明这个活动:

 <activity android:name="com.keyes.youtube.OpenYouTubePlayerActivity"></activity> 

更多的信息可以从这个代码文件的第一部分获得。

希望能帮助任何人!

您可以按照https://developers.google.com/youtube/iframe_api_reference中所述使用iframe

我不是完全按照谷歌的build议,但这是我使用的代码,它工作正常

 public class CWebVideoView { private String url; private Context context; private WebView webview; private static final String HTML_TEMPLATE = "webvideo.html"; public CWebVideoView(Context context, WebView webview) { this.webview = webview; this.context = context; webview.setBackgroundColor(0); webview.getSettings().setJavaScriptEnabled(true); } public void load(String url){ this.url = url; String data = readFromfile(HTML_TEMPLATE, context); data = data.replace("%1", url); webview.loadData(data, "text/html", "UTF-8"); } public String readFromfile(String fileName, Context context) { StringBuilder returnString = new StringBuilder(); InputStream fIn = null; InputStreamReader isr = null; BufferedReader input = null; try { fIn = context.getResources().getAssets().open(fileName, Context.MODE_WORLD_READABLE); isr = new InputStreamReader(fIn); input = new BufferedReader(isr); String line = ""; while ((line = input.readLine()) != null) { returnString.append(line); } } catch (Exception e) { e.getMessage(); } finally { try { if (isr != null) isr.close(); if (fIn != null) fIn.close(); if (input != null) input.close(); } catch (Exception e2) { e2.getMessage(); } } return returnString.toString(); } public void reload() { if (url!=null){ load(url); } } } 

在资产中我有一个名为webvideo.html的文件

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <style> iframe { border: 0; position:fixed; width:100%; height:100%; bgcolor="#000000"; } body { margin: 0; bgcolor="#000000"; } </style> </head> <body> <iframe src="%1" frameborder="0" allowfullscreen></iframe> </body> </html> 

现在我在我的主布局中定义一个webview

 <WebView android:id="@+id/video" android:visibility="gone" android:background="#000000" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="match_parent" /> 

我在我的活动中使用CWebVideoView

 videoView = (WebView) view.findViewById(R.id.video); videoView.setVisibility(View.VISIBLE); cWebVideoView = new CWebVideoView(context, videoView); cWebVideoView.load(url); 

使用YouTube Android播放器API。

activity_main.xml中:

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.andreaskonstantakos.vfy.MainActivity"> <com.google.android.youtube.player.YouTubePlayerView android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" android:layout_centerHorizontal="true" android:id="@+id/youtube_player" android:layout_alignParentTop="true" /> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="195dp" android:visibility="visible" android:id="@+id/button" /> </RelativeLayout> 

MainActivity.java:

 package com.example.andreaskonstantakos.vfy; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.google.android.youtube.player.YouTubeBaseActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayerView; public class MainActivity extends YouTubeBaseActivity { YouTubePlayerView youTubePlayerView; Button button; YouTubePlayer.OnInitializedListener onInitializedListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player); button = (Button) findViewById(R.id.button); onInitializedListener = new YouTubePlayer.OnInitializedListener(){ @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.loadVideo("Hce74cEAAaE"); youTubePlayer.play(); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { youTubePlayerView.initialize(PlayerConfig.API_KEY,onInitializedListener); } }); } } 

和PlayerConfig.java类:

  package com.example.andreaskonstantakos.vfy; /** * Created by Andreas Konstantakos on 13/4/2017. */ public class PlayerConfig { PlayerConfig(){} public static final String API_KEY = "xxxxx"; } 

https://www.youtube.com/watch?v=Hce74cEAAaE中的videoIDreplace“Hce74cEAAaE”。; 从Console.developers.google.com获取您的API_KEY,并将其replace为PlayerConfig.API_KEY。 有关更多信息,您可以按照以下教程逐步进行操作: https : //www.youtube.com/watch?v = 3LiubyYpEUk

你可以使用这个项目来玩你的任何pipevideo,在你的Android应用程序。 现在为其他video或videoID …你可以这样做https://gdata.youtube.com/feeds/api/users/eminemvevo/uploads/其中;eminemvevo =渠道

发现后,videoID,你可以把这个ID在cueVideo("video_id")

 src -> com -> examples -> youtubeapidemo -> PlayerViewDemoActivity @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player , boolean wasRestored) { if (!wasRestored) { player.cueVideo("wKJ9KzGQq0w"); } } 

特别是对于以更好的方式阅读video_id的方式,在您的project创build一个新的Xml文件或将[ 1st_file ]保存的file upload到您的project ,然后将其作为xml [ 1st_file ]文件放入您的桌面,然后right_click它,并用xml_editor文件打开它,在这里你可以find特定video的videoID。

在这里输入图像描述

 MediaPlayer mp=new MediaPlayer(); mp.setDataSource(path); mp.setScreenOnWhilePlaying(true); mp.setDisplay(holder); mp.prepare(); mp.start(); 

Google有一个YouTube Android播放器API ,可让您将video播放function整合到您的Android应用程序中。 API本身非常易于使用,效果很好。 例如,以下是如何创build一个新的活动来使用API​​播放video。

 Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, "<<YOUTUBE_API_KEY>>", "<<Youtube Video ID>>", 0, true, false); startActivity(intent); 

看到这个更多的细节。

我不想在设备上显示YouTube应用,所以我使用了本教程:

http://www.viralandroid.com/2015/09/how-to-embed-youtube-video-in-android-webview.html

…在我的应用程序中生成此代码:

 WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.video_webview); mWebView=(WebView)findViewById(R.id.videoview); //build your own src link with your video ID String videoStr = "<html><body>Promo video<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>"; mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }); WebSettings ws = mWebView.getSettings(); ws.setJavaScriptEnabled(true); mWebView.loadData(videoStr, "text/html", "utf-8"); } //video_webview <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" android:background="#000000" android:id="@+id/bmp_programme_ll" android:orientation="vertical" > <WebView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> 

这工作就是我想要的。 它不会自动播放,但在我的应用程序内的videostream。 值得注意的是一些受限制的video在embedded时不会播放。

 String video = "https://www.youtube.com/watch?v=SrPHLj_q_OQ"; Intent openVideo = new Intent(Intent.ACTION_VIEW); openVideo.setDataAndType(Uri.parse(video), "video/*"); startActivity(openVideo); 
 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watchv=cxLG2wtE7TM")); startActivity(intent);