MediaPlayer无法播放来自网址的视频

时间:2015-10-24 19:42:47

标签: android video android-mediaplayer textureview

我正在使用TextureView从我的App-Engine服务器播放视频。如果我在浏览器中打开链接,我可以播放视频。但是MediaPlay在找不到文件时会出现以下错误,然后就不会播放视频了

I/MediaPlayer: Need to enable context aware info
 E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
 D/MediaPlayer: setDataSource IOException happend : 
 D/MediaPlayer: java.io.FileNotFoundException: No content provider: https://companycloud.appspot.com/watchvideo/?videoid=1234567771234567
 D/MediaPlayer:     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1074)
 D/MediaPlayer:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:927)
 D/MediaPlayer:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:854)
 D/MediaPlayer:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1085)
 D/MediaPlayer:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1059)
 D/MediaPlayer:     at com.company.app.android.fragment.VideoFragment.playNewVideo(VideoFragment.java:411)
 D/MediaPlayer:     at com.company.app.android.fragment.VideoFragment.access$400(VideoFragment.java:63)
 D/MediaPlayer:     at com.company.app.android.fragment.VideoFragment$1.onReceive(VideoFragment.java:120)
 D/MediaPlayer:     at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
 D/MediaPlayer:     at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
 D/MediaPlayer:     at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
 D/MediaPlayer:     at android.os.Handler.dispatchMessage(Handler.java:102)
 D/MediaPlayer:     at android.os.Looper.loop(Looper.java:145)
 D/MediaPlayer:     at android.app.ActivityThread.main(ActivityThread.java:5837)
 D/MediaPlayer:     at java.lang.reflect.Method.invoke(Native Method)
 D/MediaPlayer:     at java.lang.reflect.Method.invoke(Method.java:372)
 D/MediaPlayer:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
 D/MediaPlayer:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
 D/MediaPlayer: Couldn't open file on client side, trying server side
10-24 12:25:26.665 6742-7036/com.company.app.android E/MediaPlayer: error (1, -2147483648)
10-24 12:25:26.685 6742-6742/com.company.app.android E/MediaPlayer: Error (1,-2147483648)

1 个答案:

答案 0 :(得分:0)

我不能发表评论,因为没有任何参考代码可供查看,这里有一些建议:

1) 该解决方案适用于Android 4.0或更高版本。

onSurfaceTextureAvailable()方法中初始化MedialPlayer对象并将其放在try / catch块中。

解析Uri。由于A Uri对象通常用于将内容的引用传递给ContentProvider。

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
    Surface surface = new Surface(surfaceTexture);

    try {
        mMediaPlayer= new MediaPlayer(); 
        // parse the URL path
        mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(URL_PATH));
        mMediaPlayer.setSurface(surface);
        mMediaPlayer.setLooping(true);

        mMediaPlayer.prepareAsync();


        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

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

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

对于优先使用prepareAsync()的流,因为它会立即返回,而不是阻塞,直到缓冲了足够的数据。 调用MediaPlayer.prepareAsync()方法,因为您使用构造函数来创建MediaPlayer对象。

完成播放视频后,不要忘记进行内存清理

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mMediaPlayer != null) {
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

希望它有所帮助!