开发者

How to get mp3 file duration Http streamed over Android MediaPlayer

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 开发者_如何学Gogot 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


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


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


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜