Android VideoView YouTube视频 - 无法播放此视频错误

时间:2016-11-28 05:55:56

标签: youtube-api android-youtube-api android-videoview android-video-player youtube-data-api

我试图在Android视频播放中播放YouTube视频。它无法播放此视频。我试过的就像下面一样..

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    // Widgets
    private VideoView vv_youtube;

    // Variables
    private String base_url = "https://www.youtube.com/watch?v=K8lppDEOkcM";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vv_youtube = (VideoView) findViewById(R.id.vv_youtube);

        Uri video = Uri.parse(base_url);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(vv_youtube);
        vv_youtube.requestFocus();
        vv_youtube.setMediaController(mediaController);
        vv_youtube.setVideoURI(video);

        vv_youtube.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer arg0) {
                vv_youtube.start();
            }
        });

        vv_youtube.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e("Error is: ", String.valueOf(extra));
                return false;
            }
        });

        vv_youtube.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.e("Complete is: ", mp.toString());
            }
        });
    }
}

logcat的

11-19 02:21:45.741 21929-21961 / com.example.inbridge08.videoviewyoutube I / OpenGLRenderer:Initialized EGL,version 1.4 11-19 02:21:45.772 21929-21961 / com.example.inbridge08.videoviewyoutube D / OpenGLRenderer:启用调试模式0 11-19 02:21:45.799 21929-21961 / com.example.inbridge08.videoviewyoutube W / EGL_emulation:eglSurfaceAttrib未实现11-19 02:21:45.799 21929-21961 / com.example。 inbridge08.videoviewyoutube W / OpenGLRenderer:无法在表面0xe2b19220上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS 11-19 02:21:45.898 21929-21929 / com.example.inbridge08.videoviewyoutube D / MediaPlayer:无法在客户端打开文件尝试服务器端11-19 02:21:46.421 21929-21947 / com.example.inbridge08.videoviewyoutube E / MediaPlayer:error(1,-2147483648)11-19 02:21:46.421 21929-21929 / com.example .inbridge08.videoviewyoutube E / MediaPlayer:错误(1,-2147483648)11-19 02:21:46.421 21929-21929 / com.example.inbridge08.videoviewyoutube D / VideoView:错误:1,-2147483648 11-19 02:21 :46.421 21929-21929 / com.example.inbridge08.videoviewyoutube E /错误是:: -2147483648

[11-19 02:21:46.513 82:82 D /]套接字断开

我在谷歌搜索了两天,我决定必须使用 YouTube Android Player API 在视频视频中播放YouTube视频。如果我将网址替换为其他网址(非YouTube视频网址),则说明工作正常。

我在仿真器和设备上都试过了。两个都不兼容。

提前致谢!

1 个答案:

答案 0 :(得分:0)

尝试使用MediaPlayer class在VideoView中播放视频。以下是使用MediaPlayer guide播放来自URI的视频的摘录:

通过HTTP流媒体从远程URL播放如下:

String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
  

注意:如果您要传递URL以流式传输在线媒体文件,则该文件   必须能够逐步下载。

您也可以查看此SO thread以获取更多参考资料。