getDuration()为Android App中的一个文件提供0输出

时间:2015-05-12 04:49:44

标签: android audio mediacontroller

我的应用从数据库加载两个音频文件并将它们存储在一个数组中。用户可以通过从单选按钮组中选择一个来播放其中任何一个。两者都是mp3。一个正在播放,它已经过去了,总持续时间正确显示。但是相同的功能显示其他的00:00总持续时间。在这种情况下,搜索栏也会将其进度更新为100%,但经过的时间正确显示且音频播放正常。有人请告诉我这是什么问题?为什么会发生这种情况..我该如何解决?

audio_urdu的时间很好..错误与audio_eng有关。

    private void updateView(int i) throws JSONException 
{
    idx=0;
    _imgBtnPlay.setClickable(false);
    _imgBtnStop.setClickable(false);

    JSONObject jObject=null;
    jObject=Jarray.getJSONObject(i);
    audioUrl_eng=jObject.getString("audio_eng");
    audioUrl_urdu=jObject.getString("audio_urdu");

    lbl_tDuration.setText("00:00");
    lbl_cDuration.setText("00:00");
    lbl_loading.setText("Loading audio files...");
    loadAudio(audioUrl_eng);
}

// Loading audio files from URL
private void loadAudio(String url) 
{
    // TODO Auto-generated method stub
    mMediaPlayer=new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try
    {
        mMediaPlayer.setDataSource(url);
        mMediaPlayer.prepareAsync();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch (IllegalArgumentException e) 
    {
        e.printStackTrace();
            }
    catch (IllegalStateException e) 
    {
        e.printStackTrace();
    }

    mMediaPlayer.setOnPreparedListener(this);
}

// Notify when audio is ready to be played
@Override
public void onPrepared(MediaPlayer mp) 
{
    // TODO Auto-generated method stub

     audioPlayerList[idx]=mp;
     idx++;
     if (idx == 1)
     {
         audioPlayer = mp;
         lbl_tDuration.setText(mSecToTimer(mp.getDuration()));
         lbl_cDuration.setText(mSecToTimer(mp.getCurrentPosition()));
         updateSeekBar();

         loadAudio(audioUrl_eng);
     }
     if (idx == 2)
     {
         // Enabling the media control buttons
         _imgBtnPlay.setClickable(true);
         _imgBtnStop.setClickable(true);
         rdGrp.setClickable(true);

         lbl_loading.setText("");
         idx = 0;
     }
}

public void onClick(View v) 
{
    switch(v.getId())
    {
        // calling search function
        case R.id.imgBtnSearch:
            onSearchRequested();
            break;

        // when play/pause button is tapped
        case R.id.imgBtnPlay:
            if (audioPlayer.isPlaying())
            {
                    audioPlayer.pause();
                    _imgBtnPlay.setImageResource(R.drawable.ic_action_play);
            }
            else if (audioPlayer!=null)
            {
                audioPlayer.start();
                _imgBtnPlay.setImageResource(R.drawable.ic_action_pause);
                durationHandler.postDelayed(updateSeekBarTime, 100);
            }
            break;

        // when stop button is tapped
        case R.id.imgBtnStop:
            audioPlayer.pause();
            _imgBtnPlay.setImageResource(R.drawable.ic_action_play);
            resetProgress();
            break;

        default:
                break;
    }

// Updating the seek bar's time after every 100 milliseconds
public void updateSeekBar() 
{
    durationHandler.postDelayed(updateSeekBarTime, 100);
}

// Updating the progress of seek bar
private Runnable updateSeekBarTime = new Runnable()
{
    public void run()
    {
        long tDuration = audioPlayer.getDuration();
        long cDuration = audioPlayer.getCurrentPosition();

        lbl_tDuration.setText(mSecToTimer(tDuration));
        lbl_cDuration.setText(mSecToTimer(cDuration));

        int progress = (int) getProgressPercentage(cDuration, tDuration);
        _seekbar.setProgress(progress);
        durationHandler.postDelayed(this, 100);
    }
};

// Converting milliseconds into min:sec format
public String mSecToTimer(long ms)
{
    String finalTimerString = "";
    String secString = "";
    String minString = "";

    // Convert total duration into minutes and seconds
    int min = (int)(ms % (1000*60*60)) / (1000*60);
    int sec = (int) ((ms % (1000*60*60)) % (1000*60) / 1000);

    // Prepending 0 to seconds if it is one digit
    if(sec < 10)
        secString = "0" + sec;
    else
        secString = "" + sec;

    // Prepending 0 to minutes if it is one digit
    if(min < 10)
        minString = "0" + min;
    else
        minString = "" + min;

    finalTimerString = minString + ":" + secString;
    return finalTimerString;
}

// calculating the percentage progress of seek bar
public int getProgressPercentage(long cDuration, long tDuration)
{
    Double percentage = (double) 0;
    long cSeconds = (int) (cDuration / 1000);
    long tSeconds = (int) (tDuration / 1000);

    percentage =(((double)cSeconds)/tSeconds)*100;
    return percentage.intValue();
}

// Converting progress of seek bar into time duration in milliseconds
public int progressToTimer(int progress, int tDuration) 
{
    int cDuration = 0;
    tDuration = (int) (tDuration / 1000);
    cDuration = (int) ((((double)progress) / 100) * tDuration);
    return cDuration * 1000;
}

// Reseting the progress of seek bar when stop button is tapped
public void resetProgress() 
{
    audioPlayer.seekTo(0);
    lbl_cDuration.setText(mSecToTimer(0));
    _seekbar.setProgress(0);
}

我有一个英文音频和一个乌尔都语。两者都在数组audioPlayerList中。用户可以使用单选按钮选择不同的语言。并且idx是变量,它告诉要播放哪个音频文件。 audio_eng位于索引0(idx = 0)上,audio_urdu位于索引1(idx = 1)上。音频选为audioPlayer = audioPlayerList[idx]

用于单选按钮选择的

代码是:

rdGrp.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) 
        {
            // Find which radio button is selected
            if (audioPlayer!=null)
            {
                if(audioPlayer.isPlaying())
                    audioPlayer.pause();
                _imgBtnPlay.setImageResource(R.drawable.ic_action_play);
                resetProgress();

                if (checkedId == R.id.rdEng) 
                        audioPlayer = audioPlayerList[0];
                else if (checkedId == R.id.rdUrdu)
                        audioPlayer = audioPlayerList[1];
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

文件'audio_eng'的持续时间可能不到1秒。在计算百分比时,您将毫秒转换为秒,从而导致0总长度。这就是为什么你从一开始就将进度条设置为100%(实际上,在这种情况下可能会抛出异常 - 你检查过了吗?)。

计算时percentage尝试不将xDuration转换为xSeconds,但会在方法getProgressPercentage中自行划分持续时间。

我找不到你得到这样结果的其他原因