How to know the Duration of audio song before streaming?

假装没事ソ 提交于 2019-12-07 06:36:33

问题


I am making an application for Stream audio song. In Custom media player, i have to show the total duration of that audio file. If a audio song will be SDCard , i can know its duration using

MediaPlayer player;

public double duration() {
        return player.getDuration();
    }

But in my case audio file is on the server, so i have to stream it.

What is the best approach?


回答1:


This might help you: Example of Streaming mp3 with android media player class . The respective data is probably available, once prepare() is called for the file to be streamed.




回答2:


I think this will give you the length of the file to be streamed. After the declaring the MediaPlayer mp object

 mp.start();
 totalDur=mp.getDuration();//will give you the duration in milliseconds.

For further reference please go through the following link: http://developer.android.com/reference/android/media/MediaPlayer.html#getDuration%28%29




回答3:


with below code you can get duration without using any library

player = new MediaPlayer();
player.setDataSource(YOUR_URL);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();

        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                player.start();
            }
        });


来源:https://stackoverflow.com/questions/7008282/how-to-know-the-duration-of-audio-song-before-streaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!