【问题标题】:Show Progress Bar while audio file is Playing?播放音频文件时显示进度条?
【发布时间】:2018-12-11 22:33:53
【问题描述】:

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

12-11 14:32:17.330 6190-6216/com.example.android.app E/AndroidRuntime: 致命异常: 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();

【问题讨论】:

标签: android listview progress-bar android-mediaplayer


【解决方案1】:

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

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

【讨论】:

  • 当我更换它时,我收到了这个错误 12-11 15:08:34.926 6775-6805/com.example.android.appE/AndroidRuntime: FATAL EXCEPTION: Thread-2 Process: com.example。 android.app, PID: 6775 java.lang.IllegalStateException at android.media.MediaPlayer.isPlaying(Native Method) at com.example.android.app.MainActivity$4.run(MainActivity.java:371) at java.lang.Thread .run(Thread.java:764)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多