在播放音频文件时显示进度栏吗?

时间:2018-12-11 13:42:34

标签: android listview progress-bar android-mediaplayer

希望为我的问题找到解决方案,所以当单击ListView项并且播放音频文件时,我有一个进度条显示,问题是当音频文件完成播放应用程序时崩溃,并在logcat中出现此错误:

  

12-11 14:32:17.330 6190-6216 / com.example.android.app E / AndroidRuntime:FATAL EXCEPTION:Thread-2       流程:com.example.android.app,PID:6190       java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean android.media.MediaPlayer.isPlaying()'           在com.example.android.app.MainActivity $ 4.run(MainActivity.java:371)           在java.lang.Thread.run(Thread.java:764)

这是进度条的代码:

 Runnable _progressUpdater;
private void createProgressParentThread() {

    _progressUpdater = new Runnable() {
        @Override
        public void run() {

            while(mMediaPlayer.isPlaying()) {
                try
                {
                    int current = 0;
                    int total = mMediaPlayer.getDuration();
                    progressBarParent.setMax(total);
                    Log.d("ThangTB", "total:"+total);
                    progressBarParent.setIndeterminate(false);

                    while(mMediaPlayer!=null  && current<total){
                        try {
                            Thread.sleep(200); //Update once per second
                            current = mMediaPlayer.getCurrentPosition();
                            //Removing this line, the track plays normally.
                            progressBarParent.setProgress(current);
                        } catch (InterruptedException e) {

                        } catch (Exception e){

                        }
                    }
                }
                catch(Exception e)
                {
                    //Don't want this thread to intefere with the rest of the app.
                }
            }
            if (!mMediaPlayer.isPlaying()) {
                try {
                    Log.d("ThangTB", "callllllllllllllllll");
                    GONELAYOUT();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    Thread thread = new Thread(_progressUpdater);
    thread.start();
}


public void GONELAYOUT(){
    this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            progressBarParent.setProgress(0);
            linearLayout_contentProgress.setVisibility(View.GONE);
        }
    });
}

public void VISIBLELAYOUT(){
    this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            progressBarParent.setProgress(0);
            linearLayout_contentProgress.setVisibility(View.VISIBLE);
        }
    });
}

然后我在onItemClick内调用这些方法:

                mMediaPlayer.start();
                VISIBLELAYOUT();
                createProgressParentThread();

1 个答案:

答案 0 :(得分:0)

尝试从371行开始替换此代码块,

while(mMediaPlayer != null && mMediaPlayer.isPlaying()) {
int current = 0; 
int total = mMediaPlayer.getDuration();
progressBarParent.setMax(total); 
Log.d("ThangTB", "total:"+total);
progressBarParent.setIndeterminate(false);