MediaPlayer not always working

时间:2016-10-19 13:34:22

标签: android media-player android-mediaplayer

In my app, I use a MediaPlayer to play videos (some in the project's assets, some on the device). My problem is sometimes the MediaPlayer works, and sometimes it doesn't appear but I don't understand why... (Even with the same video, sometimes it works and sometimes no). I tried to get the video width and height but they are always good. I often get this error but don't find anything about it :

error (1, -5001)

Here is my code :

final Surface surface = new Surface(surfaceView);
    mMediaPlayer = new MediaPlayer();

    try {
        mMediaPlayer.setDataSource(this.getActivity(), uri);
        mMediaPlayer.setSurface(surface);
        mMediaPlayer.prepare();

        // Play video when the media source is ready for playback.
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                if (current_time != null)
                    mediaPlayer.seekTo(current_time);
                else
                    mediaPlayer.seekTo(10);

                mediaController.init(mMediaPlayer, DrawerActivity.VIDEO_1, VideoDrawer.this);
                video.invalidate();
                if(isPlaying)
                    mediaPlayer.start();
            }
        });
    } catch (IllegalArgumentException e) {
        Log.e(TAG, e.getMessage());
    } catch (SecurityException e) {
        Log.e(TAG, e.getMessage());
    } catch (IllegalStateException e) {
        Log.e(TAG, e.getMessage() + "");
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }

0 个答案:

没有答案