Android MediaPlayer - Sometimes No Video is Played Even Though Audio Plays

13,602

Solution 1

I solved the problem, albeit in a totally hackish way. There are two problems actually:

  1. The Evil Info 35 Message: I found that occasionally MediaPlayer.OnInfoListener will get called with extra==35. When this happens, you're screwed and the video will not play properly. I have no idea what causes it. The only fix I found was to try restarting the video and going through the whole prepareAsync process over again. Video playback usually works the second time.

  2. Video Size Not Set: Even after MediaPlayer.OnPreparedListener gets issued (or equivalently prepare() returns) the video size may not be been set. The video size will usually be set a couple of miliseconds after prepare returns, but for a few moments it is in a nebulous state. If you call MediaPlayer.start() before the video size is set, then playback will sometimes (but not always) fail. There are two potential solutions to this: (1) poll MediaPlayer.getVideoHeight() or getVideoWidth() until they're non-zero or (2) wait until OnVideoSizeChangedListener is called. Only after one of these two events, should you call start().

With these two fixes, video playback is much more consistent. It's very likely that these problems are bugs with my phones (Samsung Charge and a T-Mobile Comet) so I won't be surprised if there are different, but similar problems on other phones.

Solution 2

Following speedplane's suggestions I came up with the following code. If the MediaPlayer.getVideoHeight() returns 0 in onPrepared then I delay for 1 second and try again. Now if it does not play the first time it usually will play 1 second later. I have seen it take several tries though.

   private void videoPlayer(String path){
      if (mMediaController == null)
      {
         mMediaController = new MediaController(this);
         mVideoView.setMediaController(mMediaController);
      }
      if (!mMediaController.isShowing())
         mMediaController.show();

      getWindow().setFormat(PixelFormat.TRANSLUCENT);
      mVideoView.setVideoPath(path);
      mVideoView.requestFocus();
      mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
               public void onPrepared(final MediaPlayer mp) {
               mVideoView.seekTo(mImageSeek);
               if (mp.getVideoHeight() == 0) {
                  final Handler handler = new Handler();
                  handler.postDelayed(new Runnable() {
                        @Override
                           public void run() {
                           mVideoView.stopPlayback();
                           mMediaController = null;
                           videoPlayer(mImageFilepath);
                        }
                     }, 1000);
               } else 
                  mVideoView.start();
            }
         });
   }

Solution 3

I have been studying this bug 2 weeks , and i have understand more or less the real problem . Of course i'm talking about The Evil Info 35 Message . If you have met this wonderfull strange error , now you can be happy and shot some vendors like samsung :D ...

The problem comes cause the surface view can't be seen by the user (activity in tabhost or pause-wakeup function , not in focus sometimes , not in front sometimes too (maybe)).

That's cause when some reset that video it runs ok , cause it gains focus or maybe another activity/object is not overlapping it.

When some of those happen , the mediaplayer thinks you can't see the video or it can't play , and it disable video track, giving you the worst headache in your worklife while listening the music track.

Will see next bug , good luck . :D

Share:
13,602
speedplane
Author by

speedplane

Docket Alarm indexes hundreds of millions of lawsuits, analyzes what happens in them, and predicts what will happen in future cases. We package this intelligence up into a SaaS service and sell it to lawyers at top law firms. To analyze the law, we deal with lots of interesting NLP and unstructured data problems, and to tackle them, we use some of the latest machine-learning tools. We're always looking for skilled developers with a passion in the intersection of law and technology. If that's you, please get in touch.

Updated on July 23, 2022

Comments

  • speedplane
    speedplane almost 2 years

    I am developing an Android App and I'm using the Android SDK's MediaPlayer to play some videos in my app. When I play the video in my app, about one out of five times, the audio plays without video. It's not a simple coding error because most of the time the video plays perfectly.

    I have considered that a race condition in my code caused the bug. However, I added a number of debug statements and everything seems to be set up properly when the video does not play.

    I have scanned the web and SO trying to find solutions but none have been adequate (see below).

    Has anyone run into this type of problem before? If so, what did you do?

    Similar Questions:

    MediaPlayer Video not played

    android media player shows audio but no video

    android video, hear sound but no video

    Some More Details:

    • I've come across this bug on two phones. On a Samsung Charge video plays 80% of the time and 20% of the time there's audio but no video. On a T-Mobile Comet it's much worse; video only plays about 10% of the time.
    • It's not a problem with the file, I've tried various video files and codecs and get the same issues.
    • It's not a problem with the storage medium. I've tried playing the video when it was stored on internal storage and the sdcard, neither makes a difference. I have even tried reading some of the file before playing it, hoping that the system will cache it, but that doesn't seem to help either.

    Update:

    I've been debugging this and looking through logcat. I've found that when the video works, something like the following appears in logcat:

    09-28 00:09:03.651: VERBOSE/PVPlayer(10875): setVideoSurface(0x65638)
    

    But when video doesn't play, it looks like there's a null entry:

    09-28 00:03:35.284: VERBOSE/PVPlayer(10875): setVideoSurface(0x0)
    

    Update 2:

    When the video fails to play, the function MediaPlayer.OnInfoListener with parameters what==MEDIA_ERROR_UNKNOWN(0x1) and extra==35. I looked through the Android code-base to try to determine what unknown error 35 means. I came across the file pv_player_interface.h, which indicates that error code 35 corresponds to something called a PVMFInfoTrackDisable. I googled that term which brought me to a file called pvmf_return_codes.pdf. That file gave me the following unintelligible explanation:

    4.34. PVMFInfoTrackDisable

    Notification that paticular track is disable. This one is on a per track basis. For uncompressed audio/video formats, during the process of selecting tracks available in content, if the decoder doesn't support the track, a PVMFInfoTrackDisable event is sent. The event, if necessary, will be sent once per track.

    I feel like I've gone a long way, but am no closer to finding an answer... still investigating.

  • speedplane
    speedplane over 12 years
    Thanks, but I'm writing an app, not just trying to play videos on my phone. I can't tell my users to install new ROMS.
  • wayne_bai
    wayne_bai almost 11 years
    I struggled on this issue several days. OnPrepareListener does not means fully ready. I adopt solution(1) and it works. Thanks so much!
  • Nativ
    Nativ almost 10 years
    Doesn't works for me :-(. I don't get 35 error and the height and width are always fine
  • tallpaul
    tallpaul almost 8 years
    Thanks, the second point helped me. I was running into sporadic problem calling .Start() when I repeatedly created new MediaPlayers, the players were occasionally blank (no speakers on my device but I assume the sounds were playing as per OP), it wasn't getting far enough to trigger OnInfo.
  • Dharmishtha
    Dharmishtha over 3 years
    Hii @speedplane , In Samsung device I got MEDIAINFO VIDEO_NOT_PLAYING = 805 and extra code is -12 . Don't know how to solve this problem. please help.