SurfaceView、SurfaceTexture 和 MediaPlayer 无法在 Android 上播放我的视频。

11

我正在尝试在我的应用程序上使用 SurfaceView 播放实时流视频,当我使用 Vitamio 时它播放正常,但由于这是一个 HTTP 链接,我尝试摆脱任何第三方库并使用本机类。我已经尝试过像往常一样使用 VideoView,然后在失败后尝试了 SurfaceView 基本实现。我尝试了 texture videw,就像这样:

@Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {

        Surface surface = new Surface(surfaceTexture);

        try {

            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(link));
            mMediaPlayer.setSurface(surface);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepareAsync();

            // Play video when the media source is ready for playback.
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });

            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {

                    Log.d(TAG, "Error occured");

                    return false;
                }
            });

        } catch (IllegalArgumentException e) {
            Log.d(TAG, e.getMessage());
        } catch (SecurityException e) {
            Log.d(TAG, e.getMessage());
        } catch (IllegalStateException e) {
            Log.d(TAG, e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }

    }

但是每当MediaPlayer的OnError被调用时,我总是没有运气,并且在logcat中会得到以下消息:

06-28 16:00:56.612     144-8044/? E/GenericSource﹕ Failed to prefill data cache!
06-28 16:00:56.614    7997-8016/? E/MediaPlayer﹕ error (1, -2147483648)
06-28 16:00:56.614    7997-7997/? E/MediaPlayer﹕ Error (1,-2147483648)

但问题是URL没有问题,这个URL在Vitamio和我测试的其他播放器上都可以正常播放,请帮忙!


我们可以从堆栈跟踪中获取错误输出吗? - George Daramouskas
我已经编辑了我的问题,请查看@GeorgeD。 - Reyjohn
你的视频是什么格式?我认为Vitamio内置了其他编解码器支持。 - Buddy
格式支持库是可以的,但是 Vitamio 在 logcat 中显示我的私人 URL 流,所以我不想使用它。@Buddy - Reyjohn
你尝试过使用本地视频而不是URL吗? - Nilay Dani
尝试使用MediaPlayer.create()代替?这可能会隐式设置一些您没有直接设置的属性,例如流类型。 - Cookster
2个回答

1

我曾经尝试使用MediaPlayer和Vitamio在Android上播放视频,但是遇到了很多问题。大部分情况下,如果视频无法在Android的MediaPlayer上正常播放,那么可能是因为它不支持该格式。

http://developer.android.com/guide/appendix/media-formats.html

这可能不是你想要的答案,但你很可能需要重新编码你想播放的内容以支持格式。安卓的视频播放能力远不如iPhone,这是你必须接受的事实。
如果你愿意付出更多(很多)努力,你可以为安卓自己编译ffmpeg,制作一个jni接口到它的许多组件,并将视频播放到surface/texture视图中。我个人不推荐这种方法,因为我使用ffmpeg流媒体1080p视频的经验并不好。
你最好、最简单的选择就是重新编码你的视频。
背景:我制作了一个应用程序,可以同时从各种供应商播放最多5个视频。

0

这似乎是两个问题之一。要么格式不正确,要么文件存在权限问题,无法打开。

首先使用ffmpeg转换视频。我使用以下命令将其转换为可流式传输的mp4:

ffmpeg -i InputVideo.mp4 -c:v libx264 -profile:v baseline -c:a libfaac -ar 44100 -ac 2 -b:a 128k -movflags faststart OutputVideo.mp4

其次,尝试首先将视频作为文件加载,然后将数据源传递给媒体播放器。有时需要这样做,因为我注意到当使用MediaPlayer打开文件时,它会触发操作系统级别的调用来加载文件,而该文件位于应用程序的私有文件夹中,因此操作系统无法打开它。我们可以这样做:

AssetFileDescriptor afd = contxt.getResources().openRawResourceFd(R.raw.prepare_artwork);

if (afd == null) {
    Log.e(TAG, "Failed to load video.");
} else {
    mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    afd.close();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接