19

NOTE: I'm not working at the company where I worked when I posted this question, therefore, even though some great answers might come in, I won't actually be testing them since I don't have a reason to (other than promoting the community; which might cause me to do it one day).

If however, some answer to the problem is promoted by many other comunity members, I might opt to select it as the correct answer for the problem experienced so many years after I originally posted the question.

In the meantime, I hope some of the answers may help some of you also experiencing this problem. Yay for Stack Overflow!


Our application has to stream music from an online source (I'm not at liberty to disclose that source).

Why does it take over 2 minutes to start streaming on the S3?

I've been able to figure out that the Media Player object goes into the Error state due to MEDIA_ERROR_UNKOWN - great. Doesn't help me much. So upon handling this in onError with an OnErrorListener, I reset the Media Player object and then call startPlaying which does the rest - setting the data source, etc.

Members:

private ProgressBar playSeekBar;
private ImageView ivPlay;
private ImageView ivPause;
private ImageView ivBuffer;
private MediaPlayer mPlayer;
private ImageView ivInfo;
private AudioManager audio;

Initializing the Media Player (and Visualizer - which is not the issue for the purpose of this question)

private void initialMediaPlayerAndVisualizer() {
      Log.d(TAG, "Initial Media Player and Visualizer");
    
      playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
      playSeekBar.setMax(100);
      playSeekBar.setVisibility(View.GONE);
                          
      mPlayer = new MediaPlayer();   
      
      Log.d(TAG, "Create onErrorListener");
      MediaPlayer.OnErrorListener errorListener = new MediaPlayer.OnErrorListener() {
        
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d(TAG, "OnError - Error code: "+what+" Extra code: "+extra);
            
            switch(what){
            case -1004:
                Log.d("Streaming Media", "MEDIA_ERROR_IO");
                break;
            case -1007:
                Log.d("Streaming Media", "MEDIA_ERROR_MALFORMED");
                break;
            case 200:
                Log.d("Streaming Media", "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK");
                break;
            case 100:
                Log.d("Streaming Media", "MEDIA_ERROR_SERVER_DIED");
                break;
            case -110:
                Log.d("Streaming Media", "MEDIA_ERROR_TIMED_OUT");
                break;
            case 1:
                Log.d("Streaming Media", "MEDIA_ERROR_UNKNOWN");
                break;
            case -1010:
                Log.d("Streaming Media", "MEDIA_ERROR_UNSUPPORTED");
                break;
            
            }

            switch(extra){
            case 800:
                Log.d("Streaming Media", "MEDIA_INFO_BAD_INTERLEAVING");
                break;
            case 702:
                Log.d("Streaming Media", "MEDIA_INFO_BUFFERING_END");
                break;
            case 701:
                Log.d("Streaming Media", "MEDIA_INFO_METADATA_UPDATE");
                break;
            case 802:
                Log.d("Streaming Media", "MEDIA_INFO_METADATA_UPDATE");
                break;
            case 801:
                Log.d("Streaming Media", "MEDIA_INFO_NOT_SEEKABLE");
                break;
            case 1:
                Log.d("Streaming Media", "MEDIA_INFO_UNKNOWN");
                break;
            case 3:
                Log.d("Streaming Media", "MEDIA_INFO_VIDEO_RENDERING_START");
                break;
            case 700 :
                Log.d("Streaming Media", "MEDIA_INFO_VIDEO_TRACK_LAGGING");
                break;
            
            }
            
            Log.d("Streaming Media", "Reset media player");
            mPlayer.reset();
            // We need to link the visualizer view to the media player so that it displays something
              mVisualizerManager = new VisualizerManager(context); //(VisualizerView) findViewById(R.id.visualizerView);
              
              //Send the visualizerContainer to the Renderer
              visualizerRenderer = new VisualizerRenderer(arrayVisualizer);
              mVisualizerManager.addRenderer(visualizerRenderer);
              
              try {
                startPlaying();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return true;
        }
    };
    
    Log.d(TAG, "Set error listener on Media Player object");          
     mPlayer.setOnErrorListener(errorListener);
     
      // We need to link the visualizer view to the media player so that it displays something
      mVisualizerManager = new VisualizerManager(context); //(VisualizerView) findViewById(R.id.visualizerView);
      
      //Send the visualizerContainer to the Renderer
      visualizerRenderer = new VisualizerRenderer(arrayVisualizer);
      mVisualizerManager.addRenderer(visualizerRenderer);
      
      try {
        startPlaying();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Start playing:

private void startPlaying () throws IllegalStateException, IOException, UnsupportedOperationException
  {
    
    if(mPlayer != null){
        
          ivPause.setVisibility(View.INVISIBLE);
          ivPlay.setVisibility(View.INVISIBLE);
          ivBuffer.setVisibility(View.VISIBLE);
        
        mVisualizerManager.link(mPlayer);
        
        mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
        
        try {
            mPlayer.setDataSource(theSource);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "Illegal Argument Exception: "+e);
            e.printStackTrace();
        } catch (IllegalStateException e) {
            Log.d(TAG, "Illegal State Exception: "+e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "IO Exception: "+e);
            e.printStackTrace();
        }
      
      mPlayer.prepareAsync();

      mPlayer.setOnPreparedListener(new OnPreparedListener() {
          public void onPrepared(MediaPlayer mp) {
              ivBuffer.setVisibility(View.INVISIBLE);
              
              bars.setVisibility(View.INVISIBLE);
              visualizerContainer.setVisibility(View.VISIBLE);
              ivInfo.setImageResource(R.drawable.img_radio_info_online);
              ivPause.setVisibility(View.VISIBLE);
              ivPlay.setVisibility(View.INVISIBLE);
              
              //Mute the video if the phone is muted.
              if ((audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT) ||  (audio.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE)) {
                  mp.setVolume(0, 0);
                  
                  Toast toast = Toast.makeText(context, R.string.device_muted, Toast.LENGTH_LONG);
                  toast.show();
              }
                
              mPlayer.start();
          }
      });
      
    }else{
        Log.d(TAG, "Media player is null.");
        initialMediaPlayerAndVisualizer();
    }
    
  
  
}

Stop playing:

private void stopPlaying()
      {
      bars.setVisibility(View.VISIBLE);
      visualizerContainer.setVisibility(View.INVISIBLE);

      ivInfo.setImageResource(R.drawable.img_radio_info_offline);
      
      ivPlay.setVisibility(View.VISIBLE);
      ivPause.setVisibility(View.INVISIBLE);
      
      mPlayer.stop();
      
      mVisualizerManager.release();
      mPlayer.release();
      
      mPlayer = null;
}

A little logs:

05-21 16:26:23.600: I/Buffering(3921): 0

05-21 16:26:23.600: I/MediaPlayer(3921): Info (703,156)
05-21 16:26:23.600: I/MediaPlayer(3921): Info (701,0)
05-21 16:26:23.610: D/VisualizerRenderer(3921): Render columns
05-21 16:26:23.715: V/MediaPlayer(3921): message received msg=100, ext1=1, ext2=-110
05-21 16:26:23.715: E/MediaPlayer(3921): error (1, -110)
05-21 16:26:23.715: V/MediaPlayer(3921): callback application
05-21 16:26:23.715: V/MediaPlayer(3921): back from callback
05-21 16:26:23.725: D/VisualizerRenderer(3921): Render columns
05-21 16:26:23.725: E/MediaPlayer(3921): Error (1,-110)
05-21 16:26:23.725: D/MAIN(3921): OnError - Error code: 1 Extra code: -110
05-21 16:26:23.725: D/Streaming Media(3921): MEDIA_ERROR_UNKNOWN
05-21 16:26:23.725: D/Streaming Media(3921): Reset media player
05-21 16:26:23.725: V/MediaPlayer-JNI(3921): reset
05-21 16:26:23.725: V/MediaPlayer(3921): reset
05-21 16:26:23.730: D/VisualizerRenderer(3921): Divisions: 9
05-21 16:26:23.730: D/VisualizerManager(3921): Added te renderer
05-21 16:26:23.730: V/MediaPlayer-JNI(3921): get_session_id()
05-21 16:26:23.735: D/VisualizerManager(3921): Media player and visualizer linked
05-21 16:26:23.735: D/VisualizerManager(3921): Set capture listener
05-21 16:26:23.735: D/VisualizerManager(3921): Set on visualizer complete listener
05-21 16:26:23.740: V/MediaPlayer(3921): setVideoSurfaceTexture
05-21 16:26:23.740: V/MediaPlayer(3921): prepareAsync
05-21 16:26:25.285: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:25.285: V/MediaPlayer(3921): buffering 0
05-21 16:26:25.285: V/MediaPlayer(3921): callback application
05-21 16:26:25.285: V/MediaPlayer(3921): back from callback
05-21 16:26:25.330: I/Buffering(3921): 0
05-21 16:26:25.390: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:25.390: V/MediaPlayer(3921): buffering 0
05-21 16:26:25.390: V/MediaPlayer(3921): callback application
05-21 16:26:25.390: V/MediaPlayer(3921): back from callback
05-21 16:26:25.425: I/Buffering(3921): 0
05-21 16:26:25.490: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:25.490: V/MediaPlayer(3921): buffering 0
05-21 16:26:25.490: V/MediaPlayer(3921): callback application
05-21 16:26:25.490: V/MediaPlayer(3921): back from callback
...

05-21 16:26:52.585: I/Buffering(3921): 0
05-21 16:26:53.570: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:53.570: V/MediaPlayer(3921): buffering 0
05-21 16:26:53.570: V/MediaPlayer(3921): callback application
05-21 16:26:53.570: V/MediaPlayer(3921): back from callback
05-21 16:26:53.585: I/Buffering(3921): 0
05-21 16:26:54.570: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:54.570: V/MediaPlayer(3921): buffering 0
05-21 16:26:54.570: V/MediaPlayer(3921): callback application
05-21 16:26:54.570: V/MediaPlayer(3921): back from callback
05-21 16:26:54.570: V/MediaPlayer(3921): message received msg=200, ext1=702, ext2=0
05-21 16:26:54.570: W/MediaPlayer(3921): info/warning (702, 0)
05-21 16:26:54.570: V/MediaPlayer(3921): callback application
05-21 16:26:54.570: V/MediaPlayer(3921): back from callback
05-21 16:26:54.590: I/Buffering(3921): 0
05-21 16:26:54.590: I/MediaPlayer(3921): Info (702,0)
05-21 16:26:55.575: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:55.575: V/MediaPlayer(3921): buffering 0
05-21 16:26:55.575: V/MediaPlayer(3921): callback application
05-21 16:26:55.575: V/MediaPlayer(3921): back from callback
05-21 16:26:55.575: I/Buffering(3921): 0
05-21 16:26:56.575: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:56.575: V/MediaPlayer(3921): buffering 0
05-21 16:26:56.575: V/MediaPlayer(3921): callback application
05-21 16:26:56.575: V/MediaPlayer(3921): back from callback
05-21 16:26:56.585: I/Buffering(3921): 0
05-21 16:26:57.575: V/MediaPlayer(3921): message received msg=3, ext1=0, ext2=0
05-21 16:26:57.575: V/MediaPlayer(3921): buffering 0
05-21 16:26:57.575: V/MediaPlayer(3921): callback application
05-21 16:26:57.575: V/MediaPlayer(3921): back from callback
05-21 16:26:57.600: I/Buffering(3921): 0
05-21 16:26:57.930: V/MediaPlayer-JNI(3921): stop
05-21 16:26:57.930: V/MediaPlayer(3921): stop
05-21 16:26:57.930: D/VisualizerManager(3921): Released the visualizer
05-21 16:26:57.930: V/MediaPlayer-JNI(3921): release
05-21 16:26:57.930: V/MediaPlayer(3921): setListener
05-21 16:26:57.930: V/MediaPlayer(3921): disconnect
05-21 16:26:57.935: V/MediaPlayer(3921): destructor
05-21 16:26:57.935: V/MediaPlayer(3921): disconnect
Community
  • 1
  • 1
marienke
  • 2,465
  • 4
  • 34
  • 66

4 Answers4

17

The answer to this question turned out to be an issue on Android firmware installed on Samsung S III devices running Android 4.1.2.

It seemed to have been something relating to the source of the stream, because some sources eventually played on the device, but the one we needed, never played.

If you can get your stream from another source, it should work.

So if you're developing an application for a specific company/purpose and have some control over the source of the stream, or can communicate with people in control of the source of the stream, get them to change the source of the stream to something that'll work on a Samsung S III running Android 4.1.2.

Other than that all that will solve this is a firmware upgrade.

marienke
  • 2,465
  • 4
  • 34
  • 66
  • are you playing stream using Activity or Service ? – Mehul Joisar Aug 26 '13 at 06:54
  • 1
    marienke, we are having the same problem with the s3. Can i ask you how you figured out it was a firmware issue? (source?) and when it's possible to change the source of the stream, what does it need to be change to? another domain? another encoding type? can you ellaborate? Thanks. I was going crazy. – Juan Carlos Ospina Gonzalez Feb 06 '14 at 14:46
  • 1
    Piterwilson, we contacted Samsung in the UK and they confirmed the firmware issue. Sadly a firmware update wasn't due for a long time here in SA, so we had to cut that part of functionality. I'm not sure about the stream time. I'll have to investigate. Try to search for a few online radio stations and see which work and which don't. – marienke Apr 16 '14 at 08:14
  • I am on the s4 and this just started all of a sudden. wth? Thanks alot though. – frostymarvelous Sep 04 '14 at 00:34
  • Running 4.3 on S3 and still same issue. Sigh. – Codebeat Dec 29 '16 at 20:43
  • Samsung S3 with version 4.4.4 trying to play audio from amazon source giving me error while changing source to this http://techslides.com/demos/samples/sample.mp3 this source is playing audio fine. – Erum Jan 02 '18 at 10:03
7

For everyone struggling with this problem here's the solution:

Android MediaPlayer takes long time to prepare and buffer

EDIT : The previous solution is not very complete because it is possible sometimes to hear the player 'stutter' when it pauses and resumes.

A 100% java answer that is slightly more elegant involves using a MediaCodec instance to turn the mp3 into PCM data to feed to an AudioTrack instance.

I have posted full source code and explanation here : http://www.piterwilson.com/blog/2014/03/15/mediacodec-mediaextractor-and-audiotrack-to-the-rescue/

Community
  • 1
  • 1
  • Thanks for this Piterwilson. We haven't tried to implement this solution because we don't want a solution where we basically have to rewrite the MediaPlayer class - it defeats the point. But it's a good reference. Thanks. – marienke Feb 10 '14 at 07:20
  • 1
    The best that i could do while still using MediaPlayer is to create a class that serves as wrapper and that listens for onError, onInfo, onPrepared, onError and onCompletion and have this class expect the failure to happen and act accordingly. You still see the UI freeze up at some point, but at least it recovers gracefully. – Juan Carlos Ospina Gonzalez Feb 11 '14 at 09:03
3

I resolved this issue by using library
compile 'com.devbrackets.android:exomedia:3.0.1'

Vivek
  • 31
  • 4
  • Thank you for your answer. I'm not on the project anymore (it's been a few years), but if someone else can agree with this, I'll mark it the correct answer. :) – marienke Aug 19 '16 at 07:50
  • I was getting issue to play high quality video from default media player/videoview. But, when i use this library, then it play very smoothly. thanks Vivek. – Rakesh Nov 22 '17 at 06:22
0

A definite solution for some getting -1004 on older Samsung phones:

In my case, I'm using a Samsung Galaxy Pocket and had a mp3 file on our CDN, which wouldn't play, i.e.

http://domain/path/audiofile

When downloading the file to the phone, however, it plays in the native player. Not in my app and not in browsers, though. Turns out, the media player service needed a file extension to hint the media type and couldn't auto-detect based on file headers.

I solved it by giving the file location an actual extension.

http://domain/path/audiofile.mp3

Solution

  1. Either give the file an extension where it's hosted (as above) or
  2. Programmatically, download the file manually, give it the correct extension on the sdcard and then play from sdcard as the source.
dyson returns
  • 3,376
  • 4
  • 16
  • 20