如何播放高清 Youtube 视频

Posted

技术标签:

【中文标题】如何播放高清 Youtube 视频【英文标题】:How to Play HD Youtube Video 【发布时间】:2016-05-02 10:39:03 【问题描述】:

如果我应用此代码错误显示无法播放此视频我如何播放此视频

   @Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VideoView videoView =(VideoView)findViewById(R.id.videoView);
    MediaController mediaController= new MediaController(this);
    mediaController.setAnchorView(videoView);
    Uri uri= Uri.parse("https://www.youtube.com/watch?v=PdrTQmsxzpU");
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.start();

错误

mTotalSize is -1
 setReadTimeOut =  30000ms
message received msg=100, ext1=1, ext2=-2147483648
05-02 15:43:21.686 426-439/com.example.muhammad.youtubeproject E/MediaPlayer: error (1, -2147483648)

V/MediaPlayer: back from callback
 Error (1,-2147483648)
Error: 1,-2147483648

【问题讨论】:

【参考方案1】:

您可以使用YouTubePlayerView 代替Video View

It is more Reliable to Play YouTube Videos.

这是快速启动代码段。

 <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtube_view"
                android:layout_
                android:layout_
                android:layout_marginBottom="30dp" />

代码片段。

public class MainActivity extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener 

    private static final int RECOVERY_DIALOG_REQUEST = 1;

    // YouTube player view
    private YouTubePlayerView youTubeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

        // Initializing video player with developer key
        youTubeView.initialize(Config.DEVELOPER_KEY, this);

    

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

            // loadVideo() will auto play video
            // Use cueVideo() method, if you don't want to play it automatically
            player.loadVideo(Config.YOUTUBE_VIDEO_CODE);

            // Hiding player controls
            player.setPlayerStyle(PlayerStyle.CHROMELESS);
        
    

    @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(Config.DEVELOPER_KEY, this);
        
    

    private YouTubePlayer.Provider getYouTubePlayerProvider() 
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    


这是参考链接 Look.

【讨论】:

它显示这个 YouTubeBaseActivity 实现 YouTubePlayer.OnInitializedListener 的错误 @Ahmad 看看我最后在回答中给出的参考链接。一步一步给出信息。

以上是关于如何播放高清 Youtube 视频的主要内容,如果未能解决你的问题,请参考以下文章

youtube:默认显示高清视频的链接

强制嵌入 Youtube 以高清播放(2016 版)

如何在没有 UIWebView 的情况下播放 youtube 视频或在 youtube 视频开始使用 webview 播放时检测视频播放器?

如何将 youtube 视频嵌入为带有自动播放、循环和隐藏观看、分享、Youtube 徽标和视频标题的标题?

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

youtube.com 上的视频如何在 iOS 上自动播放