Android媒体播放器不完全寻求进度



我试图使用媒体播放器播放。wav文件。实际上,到目前为止我所做的如下所示。它正在正确地播放更新进度,但问题媒体播放器没有完全寻找。媒体播放器在文件的总持续时间之前停止给出进度,并停止更新进度。我也实现了OnComplete监听器也没有得到回调。这是我的代码,谁能发现我在哪里失踪。

public void playAudioFile() {
    try {
        mMediaPlayer.reset();
        setMediaPlayerSource();
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                Graphbar.setProgress(mMediaPlayer.getDuration());
            }
        });
        bPlay.setBackgroundResource(R.drawable.pause_selector);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Playing");
                updateProgressBar();
            }
        }).start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
    public void updateProgressBar() {
    Graphbar.setProgress(0);
    Graphbar.setMax(mMediaPlayer.getDuration());
    mHandler.postDelayed(mUpdateTimeTask, 0);
}
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        if(mMediaPlayer.isPlaying()){
        long totalDuration = Graphbar.getMax();
        long currentDuration =mMediaPlayer.getCurrentPosition();
        String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
        tvTimeRight.setText(TotalDur);
        tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
        System.out.println(currentDuration+"/"+totalDuration);
        Graphbar.setProgress((int)currentDuration); 
        mHandler.postDelayed(this, 1);
        }
    }
};
private void setMediaPlayerSource() {
        String audioFile = getFilename();
        try {
            mMediaPlayer.setDataSource(audioFile);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

使用mediaplayer.getduration获取总时长。从可运行处理程序中删除不必要的代码,因为它将创建加载到您的查找栏。因为在每秒钟,你都在计算一些东西,并且根据该计算更新布局

我建议你使用android的chronometer视图。

public void playAudioFile() {
    try {
        mMediaPlayer.reset();
        setMediaPlayerSource();
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                Graphbar.setProgress(mMediaPlayer.getDuration());
            }
        });
        bPlay.setBackgroundResource(R.drawable.pause_selector);
        //set this only for once , because for one song total duration is always constant.
     updateProgressBar();
}
    public void updateProgressBar() {
    Graphbar.setProgress(0); 
    Graphbar.setMax(mMediaPlayer.getDuration());
    mHandler.postDelayed(mUpdateTimeTask, 0);
    long totalDuration = mMediaPlayer.getDuration();
    String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
        tvTimeRight.setText(TotalDur);
}
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        if(mMediaPlayer.isPlaying()){
        //long totalDuration = mMediaPlayer.getDuration();=> no need of calculating total time and updating it to seekbar at every second 
        long currentDuration =mMediaPlayer.getCurrentPosition();

        tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
        System.out.println(currentDuration+"/"+totalDuration);
        Graphbar.setProgress((int)currentDuration); 
        mHandler.postDelayed(this, 1);
        }
    }
};
private void setMediaPlayerSource() {
        String audioFile = getFilename();
        try {
            mMediaPlayer.setDataSource(audioFile);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

最新更新