简体   繁体   English

如何在流式传输之前知道音频歌曲的持续时间?

[英]How to know the Duration of audio song before streaming?

I am making an application for Stream audio song.我正在申请 Stream 音频歌曲。 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如果一首音频歌曲是 SDCard,我可以使用它知道它的持续时间

MediaPlayer player;

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

But in my case audio file is on the server, so i have to stream it.但在我的情况下,音频文件在服务器上,所以我必须 stream 它。

What is the best approach?最好的方法是什么?

This might help you: Example of Streaming mp3 with android media player class .这可能会对您有所帮助: 使用 android 媒体播放器 class 的流式 mp3 示例 The respective data is probably available, once prepare() is called for the file to be streamed.一旦为要流式传输的文件调用 prepare() ,相应的数据可能是可用的。

I think this will give you the length of the file to be streamed.我认为这将为您提供要流式传输的文件的长度。 After the declaring the MediaPlayer mp object在声明 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如需进一步参考,请通过以下链接 go: http://developer.android.com/reference/android/media/MediaPlayer.html#getDuration%28%

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM