在不使用Android Youtube API的情况下在Android应用中播放Youtube视频

时间:2016-07-27 00:05:32

标签: android video youtube youtube-api

有没有其他方法可以在不使用Android Youtube API的情况下在Android应用内播放Youtube视频?我们已经使用上述API实现了解决方案,但在某些设备上,它需要降级Youtube应用版本才能播放视频。我已经在Github上搜索了我可以使用的库,但大多数都依赖于Android Youtube API。还尝试使用WebView嵌入,但有时会崩溃。

我通过解析http://www.youtube.com/get_video_info?&video_id=p3ND_O6YYg4的结果并获取" dashmpd "的值来实现其他解决方案,然后加载&#的值34;的 dashmpd "并且结果可以用作Android的VideoView中的源,但它没有音频。我注意到视频和音频有不同的来源。

提前致谢!

2 个答案:

答案 0 :(得分:4)

如果您真的想在iframe内使用Youtube Webview而不是YoutubePlayerFragment,那么您将有一些工作要在WebView中“重新编码”Youtube播放器控件图层(屏幕尺寸/键码等...)。

我做了一个可以帮助你开始的实例:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourpackage.name">

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

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">

        <activity
            android:name="com.yourpackage.name.YoutubeActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>

</manifest>

此外,你说你在Weview上使用Youtube iframe时遇到了一些崩溃,也许你没有像上面那样设置android:hardwareAccelerated="true"

youtube.html文件夹中的

src/main/assets

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <title></title>

  <style type="text/css">

    iframe {
      position: absolute;
      border: none;
      box-sizing: border-box;
      width: 100%;
      height: 100%;
    }
  </style>

  <script>

    var player;

    // init player
    function onYouTubeIframeAPIReady() {
      player = new YT.Player('player', {
        height: '1080',
        width: '1920',
        //videoId: 'Orw8CZpzIDU',
        suggestedQuality: 'hd720',
        playerVars: {rel: 0,showinfo:0},
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
      });

    }

    function loadVideo(target){
      target.loadVideoById('Orw8CZpzIDU', 0, 'hd720');
    }

    function onPlayerStateChange(event) {

        var playbackQuality = event.target.getPlaybackQuality();
        var suggestedQuality = 'hd720';

        console.log("Quality changed to: " + playbackQuality );

        if( playbackQuality !== 'hd720') {
            console.log("Setting quality to " + suggestedQuality );
            event.target.setPlaybackQuality( suggestedQuality );
        }

        console.log(event.data + " et " + YT.PlayerState.PLAYING);
    }

    // when ready, wait for clicks
    function onPlayerReady(event) {
      event.target.setPlaybackQuality('hd720');
      var player = event.target;
      loadVideo(player);
      return false;
    }

  </script>
</head>

<body id="body">

  <div id="video_div">

    <script src="https://www.youtube.com/iframe_api"></script>

    <div id="bottom">
      <div id="player"></div>
    </div>

  </div>

</body>

</html>

YoutubeActivity.java

public class YoutubeActivity extends Activity {

    private WebView webview;

    @Override
    protected void onPause() {
        super.onPause();
        webview.onPause();
    }

    @Override
    protected void onResume() {
        webview.onResume();
        super.onResume();
    }

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        webview = new WebView(this);
        setContentView(webview);

        final WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);

        webview.setWebChromeClient(new WebChromeClient());
        webview.setPadding(0, 0, 0, 0);

        webview.loadUrl("file:///android_asset/youtube.html");
    }

}

答案 1 :(得分:1)

我也遇到了YouTube Player API的问题,因此我决定基于Webview构建自己的播放器库。现在它已在GitHub上开源,您可以here找到它。

要开始使用,只需将库添加到依赖项中即可:

dependencies {
  implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:*last-version*'
}

将视图添加到布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

并初始化它:

YouTubePlayerView youtubePlayerView = findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youtubePlayerView);

youtubePlayerView.initialize(new YouTubePlayerInitListener() {
    @Override
    public void onInitSuccess(@NonNull final YouTubePlayer initializedYouTubePlayer) {
        initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady() {
                String videoId = "6JYIGclVQdw";
                initializedYouTubePlayer.loadVideo(videoId, 0);
            }
        });
    }
}, true);