如何从服务器的音频流的持续时间在线 [英] How to get the duration of an audio streaming from servers online

查看:134
本文介绍了如何从服务器的音频流的持续时间在线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采集音频的总时长已经成为我的问题。我创建一个从网上播放音频的活动,我用的是Android的媒体播放器。现在的声音能够正常播放,但我面临的问题也相应使搜索栏进步与音频。在搜索栏的设置是否正确,但我已经看到 getDuration 返回0值。
下面是我做了什么。

Collecting the total duration of an audio has become a problem for me. I am creating an activity that plays an audio from online, I use the android media player. Now the audio is able to play correctly, but the problem I am facing is allowing the seek bar progress accordingly with the audio. The seek bar is set correctly but i have seen that the getDuration returns 0 value. Below is what i have done.

public class AudioDetails extends AppCompatActivity
    implements MediaPlayer.OnCompletionListener, SeekBar.OnSeekBarChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audios_details_activity);


    // Mediaplayer
    mp = new MediaPlayer();
    utils = new AudioUtilities();

    // Listeners
    songProgressBar.setOnSeekBarChangeListener(this); // Important
    mp.setOnCompletionListener(this); // Important



    // By default play first song
    playSong(0);

    /**
     * Play button click event
     * plays a song and changes button to pause image
     * pauses a song and changes button to play image
     * */
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // check for already playing
            if(mp.isPlaying()){
                if(mp!=null){

                    mp.pause();
                    // Changing button image to play button
                    btnPlay.setImageResource(R.drawable.btn_play);
                }
            }else{
                // Resume song
                if(mp!=null){
                    mp.start();
                    // Changing button image to pause button
                    btnPlay.setImageResource(R.drawable.btn_pause);
                }
            }

        }
    });



}



/**
 * Function to play a song
 * @param songIndex - index of song
 * */
public void  playSong(int songIndex){
    // Play song
    try {
        mp.reset();
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {

            mp.setDataSource("http://test.com/test/upload/Twale_FLO.mp3");
            mp.prepareAsync();
            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {                        
                    mp.start();
                    totalDuration =mp.getDuration();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }




        // set Progress bar values
        songProgressBar.setProgress(0);
        songProgressBar.setMax(99);

        // Updating progress bar
        updateProgressBar();


    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
       e.printStackTrace();
    }
}

/**
 * Update timer on seekbar
 * */
public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);
}

/**
 * Background Runnable thread
 * */
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        long currentDuration = mp.getCurrentPosition();
        long totalDuration = mp.getDuration();

        // Displaying Total Duration time
       songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));

        // Displaying time completed playing
        songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

        // Updating progress bar
        int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration ));
        Log.d("Progress", "" + progress);
        songProgressBar.setProgress(progress);

        // Running this thread after 100 milliseconds
        mHandler.postDelayed(this, 100);
    }
};

/**
 *
 * */
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

}

/**
 * When user starts moving the progress handler
 * */
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // remove message Handler from updating progress bar
    mHandler.removeCallbacks(mUpdateTimeTask);
}

/**
 * When user stops moving the progress hanlder
 * */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTimeTask);
    int totalDuration = mp.getDuration();
    int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

    // forward or backward to certain seconds
    mp.seekTo(currentPosition);

    // update timer progress again
    updateProgressBar();
}

/**
 * On Song Playing completed
 * if repeat is ON play same song again
 * if shuffle is ON play random song
 * */
@Override
public void onCompletion(MediaPlayer arg0) {

    // check for repeat is ON or OFF
    if(isRepeat){
        // repeat is on play same song again
        playSong(currentSongIndex);
    }  else{
            // play first song
            playSong(0);
            currentSongIndex = 0;
    }
}

@Override
public void onDestroy(){
    super.onDestroy();
    mp.release();
}}

请我怎么能得到的音频文件流从网上通过机器人的总时间?先谢谢了。

Please how can I get the total duration of the audio file streaming from online via android? Thanks in advance.

推荐答案

有一些问题,这个话题。

There are some problem with this topic.


  1. getDuration()不能在所有的API版本。因此,它是不知道什么时候会工作,不用时。

  2. 其他的方式是提取元数据。不幸的是,Android的MediaMetadataRetriever不在线源。它仅适用于SD卡中的文件工作。

  1. The getDuration() does not work on all API versions. So, it is not sure when it will work and when not.
  2. The other way around is to extract metadata. Unfortunately, android MediaMetadataRetriever doesn't work with online source. It works only with sd card files.

幸运的是有可用的库,可以帮助我们。和我与该库的持续时间。因此,这些步骤。

Fortunately there is a library available that can help us. And i got the duration with that library. So, these are steps.


  1. 添加编译com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.3 你的 gradle.build 中依赖

  1. add compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.3' to your gradle.build in dependency

然后,您可以通过以下code得到持续时间

Then you can get duration by below code

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
            mmr.setDataSource("http://owatechinnovations.com/test/upload/Twale_FLO.mp3");
            mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
            mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
            long duration =Long.parseLong(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
            duration=duration/1000;
            long minute=duration/(60);
            long second=duration-(minute*60);
            mmr.release();
            your_text_view.setText(minute+":"+second);

我得到的结果4:184分钟18秒。希望它可以帮助你。

I get the result "4:18" 4 mins 18 secs. hope it helps you

这篇关于如何从服务器的音频流的持续时间在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆