1

我正在尝试使用媒体播放器播放 .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();
        }
    }
4

1 回答 1

0

总持续时间使用 mediaplayer.getduration。从可运行处理程序中删除不必要的代码 ,因为它会为您的搜索栏创建负载。因为每秒钟你都在计算一些东西并根据该计算更新布局。

我建议你使用android 的计时器视图。

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();
        }
    }
于 2013-04-27T11:47:10.233 回答