如何在我的Android应用程序中播放YouTube视频?

时间:2021-11-27 18:56:10

How do I play YouTube videos in my application? I want to play video by streaming it directly from YouTube without downloading. Also, while playing videos, I want provide menu options. I don't want to play video using default intent. How can I do this?

如何在我的应用程序中播放YouTube视频?我想通过直接从YouTube流式播放视频而无需下载。此外,在播放视频时,我想提供菜单选项。我不想使用默认意图播放视频。我怎样才能做到这一点?

14 个解决方案

#1


59  

This is the btn click event

这是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....");

    }
}); 

this type it opened in another page with the youtube where u can show your video

这种类型它在youtube的另一个页面中打开,您可以在其中显示您的视频

#2


21  

Check out the official YouTube Android Player API.

查看官方YouTube Android播放器API。

#3


14  

Steps

脚步

  1. Create a new Activity, for your player(fullscreen) screen with menu options. Run the mediaplayer and UI in different threads.

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

  2. For playing media - In general to play audio/video there is mediaplayer api in android. FILE_PATH is the path of file - may be url(youtube) stream or local file path

    对于播放媒体 - 一般来说播放音频/视频都有android中的mediaplayer api。 FILE_PATH是文件的路径 - 可能是url(youtube)流或本地文件路径

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

Also check: Android YouTube app Play Video Intent have already discussed this in detail.

同时检查:Android YouTube应用Play Play Intent已经详细讨论了这一点。

#4


5  

You can have a look at the Android-Youtube-Player App which can be found here.
In this sample app the youtube videos are played inside a videoview rather than a webview.

您可以在这里找到Android-Youtube-Player应用程序。在此示例应用中,YouTube视频在视频内部而非网页视图中播放。

#5


5  

Use YouTube Android Player API.

使用YouTube Android Player API。

activity_main.xml:

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:

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);
        }
    });
}
}

and the PlayerConfig.java class:

和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";
}

Replace the "Hce74cEAAaE" with your video ID from https://www.youtube.com/watch?v=Hce74cEAAaE. Get your API_KEY from Console.developers.google.com and also replace it on the PlayerConfig.API_KEY. For any further information you can follow the following tutorial step by step: https://www.youtube.com/watch?v=3LiubyYpEUk

将“Hce74cEAAaE”替换为您的视频ID,网址为https://www.youtube.com/watch?v=Hce74cEAAaE。从Console.developers.google.com获取API_KEY,并在PlayerConfig.API_KEY上替换它。有关更多信息,您可以按照以下教程一步一步地操作:https://www.youtube.com/watch?v = 3LiubyYpEUk

#6


4  

Hope this link will help you http://android-coding.blogspot.in/2011/03/simple-example-using-videoview-to-play.html

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

To convert a youtube video to rtsp use this github code - youtube-url2rtsp

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

#7


2  

This answer could be really late, but its useful.

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

You can play youtube videos in the app itself using android-youtube-player.

您可以使用android-youtube-player在应用程序中播放YouTube视频。

Some code snippets:

一些代码片段:

To play a youtube video that has a video id in the url, you simply call the OpenYouTubePlayerActivity intent

要播放网址中包含视频ID的YouTube视频,只需调用OpenYouTubePlayerActivity intent即可

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

where v is the video id.

其中v是视频ID。

Add the following permissions in the manifest file:

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

<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"/>

and also declare this activity in the manifest file:

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

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

Further information can be obtained from the first portions of this code file.

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

Hope that helps anyone!

希望能帮助任何人!

#8


2  

You may use iframe as described in https://developers.google.com/youtube/iframe_api_reference

您可以按照https://developers.google.com/youtube/iframe_api_reference中的说明使用iframe

I am not following the google advises exactly but this is the code I am using and it is working fine

我没有完全遵循谷歌的建议,但这是我正在使用的代码,它工作正常

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);
        }
    }
}

in /assets I have a file called webvideo.html

in / assets我有一个名为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>

and now I define a webview inside my main layout

现在我在主要布局中定义了一个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" />

and I use CWebVideoView inside my activity

我在我的活动中使用CWebVideoView

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

#9


1  

you can use this project to play any you tube video , in your android app . Now for other video , or Video id ... you can do this https://gdata.youtube.com/feeds/api/users/eminemvevo/uploads/ where eminemvevo = channel .

你可以使用这个项目在你的Android应用程序中播放任何管视频。现在,对于其他视频或视频ID ...您可以执行此操作https://gdata.youtube.com/feeds/api/users/eminemvevo/uploads/其中eminemvevo =频道。

after finding , video id , you can put that id in cueVideo("video_id")

找到后,视频ID,您可以将该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");
    }
  }

And specially for reading that video_id in a better way open this , and it as a xml[1st_file] file in your desktop after it create a new Xml file in your project or upload that[1st_file] saved file in your project , and right_click in it , and open it with xml_editor file , here you will find the video id of the particular video .

特别是以更好的方式读取video_id打开它,并在项目中创建新的Xml文件后将其作为桌面上的xml [1st_file]文件,或者在项目中上传[1st_file]保存的文件,并右键单击它,并使用xml_editor文件打开它,在这里您将找到特定视频的视频ID。

如何在我的Android应用程序中播放YouTube视频?

#10


1  

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

#11


1  

Google has a YouTube Android Player API that enables you to incorporate video playback functionality into your Android applications. The API itself is very easy to use and works well. For example, here is how to create a new activity to play a video using the API.

Google拥有YouTube Android播放器API,可让您将视频播放功能整合到Android应用中。 API本身非常易于使用且运行良好。例如,以下是如何使用API​​创建新活动来播放视频。

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

See this for more details.

有关详细信息,请参阅此

#12


1  

I didn't want to have to have the YouTube app present on the device so I used this tutorial:

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

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

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

...to produce this code in my app:

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

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>     

This worked just how I wanted it. It doesn't autoplay but the video streams within my app. Worth noting that some restricted videos won't play when embedded.

这就是我想要的方式。它不会自动播放,但我的应用程序中的视频流。值得注意的是,某些受限视频在嵌入时无法播放。

#13


0  

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);  

#14


-1  

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watchv=cxLG2wtE7TM"));
startActivity(intent);

#1


59  

This is the btn click event

这是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....");

    }
}); 

this type it opened in another page with the youtube where u can show your video

这种类型它在youtube的另一个页面中打开,您可以在其中显示您的视频

#2


21  

Check out the official YouTube Android Player API.

查看官方YouTube Android播放器API。

#3


14  

Steps

脚步

  1. Create a new Activity, for your player(fullscreen) screen with menu options. Run the mediaplayer and UI in different threads.

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

  2. For playing media - In general to play audio/video there is mediaplayer api in android. FILE_PATH is the path of file - may be url(youtube) stream or local file path

    对于播放媒体 - 一般来说播放音频/视频都有android中的mediaplayer api。 FILE_PATH是文件的路径 - 可能是url(youtube)流或本地文件路径

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

Also check: Android YouTube app Play Video Intent have already discussed this in detail.

同时检查:Android YouTube应用Play Play Intent已经详细讨论了这一点。

#4


5  

You can have a look at the Android-Youtube-Player App which can be found here.
In this sample app the youtube videos are played inside a videoview rather than a webview.

您可以在这里找到Android-Youtube-Player应用程序。在此示例应用中,YouTube视频在视频内部而非网页视图中播放。

#5


5  

Use YouTube Android Player API.

使用YouTube Android Player API。

activity_main.xml:

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:

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);
        }
    });
}
}

and the PlayerConfig.java class:

和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";
}

Replace the "Hce74cEAAaE" with your video ID from https://www.youtube.com/watch?v=Hce74cEAAaE. Get your API_KEY from Console.developers.google.com and also replace it on the PlayerConfig.API_KEY. For any further information you can follow the following tutorial step by step: https://www.youtube.com/watch?v=3LiubyYpEUk

将“Hce74cEAAaE”替换为您的视频ID,网址为https://www.youtube.com/watch?v=Hce74cEAAaE。从Console.developers.google.com获取API_KEY,并在PlayerConfig.API_KEY上替换它。有关更多信息,您可以按照以下教程一步一步地操作:https://www.youtube.com/watch?v = 3LiubyYpEUk

#6


4  

Hope this link will help you http://android-coding.blogspot.in/2011/03/simple-example-using-videoview-to-play.html

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

To convert a youtube video to rtsp use this github code - youtube-url2rtsp

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

#7


2  

This answer could be really late, but its useful.

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

You can play youtube videos in the app itself using android-youtube-player.

您可以使用android-youtube-player在应用程序中播放YouTube视频。

Some code snippets:

一些代码片段:

To play a youtube video that has a video id in the url, you simply call the OpenYouTubePlayerActivity intent

要播放网址中包含视频ID的YouTube视频,只需调用OpenYouTubePlayerActivity intent即可

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

where v is the video id.

其中v是视频ID。

Add the following permissions in the manifest file:

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

<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"/>

and also declare this activity in the manifest file:

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

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

Further information can be obtained from the first portions of this code file.

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

Hope that helps anyone!

希望能帮助任何人!

#8


2  

You may use iframe as described in https://developers.google.com/youtube/iframe_api_reference

您可以按照https://developers.google.com/youtube/iframe_api_reference中的说明使用iframe

I am not following the google advises exactly but this is the code I am using and it is working fine

我没有完全遵循谷歌的建议,但这是我正在使用的代码,它工作正常

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);
        }
    }
}

in /assets I have a file called webvideo.html

in / assets我有一个名为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>

and now I define a webview inside my main layout

现在我在主要布局中定义了一个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" />

and I use CWebVideoView inside my activity

我在我的活动中使用CWebVideoView

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

#9


1  

you can use this project to play any you tube video , in your android app . Now for other video , or Video id ... you can do this https://gdata.youtube.com/feeds/api/users/eminemvevo/uploads/ where eminemvevo = channel .

你可以使用这个项目在你的Android应用程序中播放任何管视频。现在,对于其他视频或视频ID ...您可以执行此操作https://gdata.youtube.com/feeds/api/users/eminemvevo/uploads/其中eminemvevo =频道。

after finding , video id , you can put that id in cueVideo("video_id")

找到后,视频ID,您可以将该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");
    }
  }

And specially for reading that video_id in a better way open this , and it as a xml[1st_file] file in your desktop after it create a new Xml file in your project or upload that[1st_file] saved file in your project , and right_click in it , and open it with xml_editor file , here you will find the video id of the particular video .

特别是以更好的方式读取video_id打开它,并在项目中创建新的Xml文件后将其作为桌面上的xml [1st_file]文件,或者在项目中上传[1st_file]保存的文件,并右键单击它,并使用xml_editor文件打开它,在这里您将找到特定视频的视频ID。

如何在我的Android应用程序中播放YouTube视频?

#10


1  

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

#11


1  

Google has a YouTube Android Player API that enables you to incorporate video playback functionality into your Android applications. The API itself is very easy to use and works well. For example, here is how to create a new activity to play a video using the API.

Google拥有YouTube Android播放器API,可让您将视频播放功能整合到Android应用中。 API本身非常易于使用且运行良好。例如,以下是如何使用API​​创建新活动来播放视频。

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

See this for more details.

有关详细信息,请参阅此

#12


1  

I didn't want to have to have the YouTube app present on the device so I used this tutorial:

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

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

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

...to produce this code in my app:

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

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>     

This worked just how I wanted it. It doesn't autoplay but the video streams within my app. Worth noting that some restricted videos won't play when embedded.

这就是我想要的方式。它不会自动播放,但我的应用程序中的视频流。值得注意的是,某些受限视频在嵌入时无法播放。

#13


0  

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);  

#14


-1  

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watchv=cxLG2wtE7TM"));
startActivity(intent);