Home » Android » SurfaceView, SurfaceTexture and MediaPlayer cant play my video in android

SurfaceView, SurfaceTexture and MediaPlayer cant play my video in android

Posted by: admin May 14, 2020 Leave a comment


I am trying to play live streaming video on my app using SurfaceView, when i try it with Vitamio it plays well, but as it is a HTTP link, I tried to get rid of any 3rd party library and had used the native classes. I have tried VideoView as I always do, then I tried the SurfaceView basic implementation after failure I have tried texture videw like this:

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

        Surface surface = new Surface(surfaceTexture);

        try {

            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(link));

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

            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                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());


but no luck everytime MediaPlayer’s OnError is called and in the logcat I get this:

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)

but the thing is there is no problem with the URL, this url is playing fine on vitamio and every other playes that I could test on, please help!!

How to&Answers:

I’ve had my own pain trying to get video to play on Android via MediaPlayer and I have also tried Vitamio as well. Most of the time, if a video didn’t play properly on Android’s MediaPlayer it was because it was not of a supported format.


This may not be the answer you want, but you’re likely going to have to re-encode whatever you’re trying to play to a supported format. Android’s video playing capabilities are far weaker than that of the iphone, and this is just something you’re going to have to accept.

If instead you’re willing to put in (a lot) more work, you can compile ffmpeg yourself for android, make a jni interface to it’s many components, and play videos into the surface/texture view. I don’t personally recommend this route as my experience with streaming 1080p video via ffmpeg wasn’t great.

Your best, and easiest bet is to simply re-encode your videos.

Background: I made an app that played up to 5 videos silmutaniously from a variety of vendors.


It seems to be one of 2 problems. Either the format is incorrect or there are permission issues with the file and it is unable to open it.

First convert the video using ffmpeg. I use this command to convert to a stream-able 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

Second, try loading the video as a file first and then pass the data source to media player. This is needed at times as I have noticed that when using MediaPlayer to open file it triggers an OS level call to load the file while the file is in the apps private folder and is hence unopenable by the OS. We do so like this:

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