0

I am making a media player application in which i have a Mp3 file URL that i need to http stream down over Android MediaPlayer.

Now My Streaming is working very fine . Song is getting downloaded and Played Successfully. But i need to show the Downloading and Song Progress over a ProgressBar/SeekBar. As soon i got my initial data buffer filled with some song data my streamer plays the song.

But i need to know the total duration of song so that i can set the Progress Bar accordingly. As soon my Media Player starts playing the song. I call

mediaPlayer.getDuration() for getting the file duration .. But it is not giving me the right data untill the whole song get downloaded....

This is bad . as it seems getDuration() Method of MediaPlayer is showing the duration on the basis of downloaded content instead of checking the header of Mp3 file.....

I am following the tutorial for Http streaming audio song from the following Link

http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/

Any Help would be appreciated. Thanks

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72

3 Answers3

3

To get the duration of mp3 file streaming over http you can use MediaPlayer class.

   String url = mediaUrl; // your URL here
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);

        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {

                int length = mediaPlayer.getDuration(); // duration in time in millis

                String durationText = DateUtils.formatElapsedTime(length / 1000)); // converting time in millis to minutes:second format eg 14:15 min


                seekbar.setMax(length);// use this to set seekbar length and then update every second

            }
        });


    } catch (Exception e) {
        e.printStackTrace();
    }

To start playing the mp3 you can call

 mediaPlayer.start();

And don't forget to call release() in onDestroy()

 mediaPlayer.release();
Priyankchoudhary
  • 786
  • 9
  • 12
0

The tutorial you use is very old. You should not download mp3. You can just pass the url to MediaPlayer and it will do all the job. And it will give you the correct duration.

MediaPlayer mp=new MediaPlayer();
mp.setDataSource(url_to_mp3);
mp.prepare();
mp.start();
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • Yes its working , but the first time it stop buffering and start playing the song taking 45seconds – Rohit Sharma Nov 22 '10 at 12:52
  • 11-22 18:20:08.445: DEBUG/PlayerDriver(31): buffering (35) 11-22 18:20:08.565: DEBUG/PlayerDriver(31): buffering (36) 11-22 18:20:09.765: DEBUG/PlayerDriver(31): buffering (37) 11-22 18:20:09.930: WARN/AudioFlinger(31): write blocked for 51 msecs 11-22 18:20:11.315: DEBUG/PlayerDriver(31): buffering (38) – Rohit Sharma Nov 22 '10 at 12:53
  • How to low the buffering rate so that song can get start playing soon or later after 10 sec or more – Rohit Sharma Nov 22 '10 at 12:54
0

On a lower internet speed such default buffing could take a lot of time... The Pocket Journey tutorial is having a lower size buffer and starts playing the song immediately..

Default MediaPlayer is taking 45 sec or more to end first buffer...

How to slow that so that song can be started as soon as possible...

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72