Android Java:更新搜索栏以在播放音频文件时显示进度

Android Java: Update seekbar to show progress as audio file plays

这是我的第一个 Android/Java 应用程序。我正在使用第一个答案 here 尝试启动重复任务,更新搜索栏 ("timeSlider") 以显示音频文件播放的进度。这是我的代码(删除了一些不相关的行):

 private int timeSliderInterval = 1000; // 1 second
 private Handler timeSliderHandler;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_play);
     Intent intent = getIntent();
     Runnable doUpdateTimeSlider = new Runnable() {
         @Override
         public void run() {
             timeSliderHandler.postDelayed(doUpdateTimeSlider, timeSliderInterval);
             updateTimeSlider();
         }
     };

     void startUpdateTimeSlider() {
         doUpdateTimeSlider.run();
     }

     void stopUpdateTimeSlider() {
         timeSliderHandler.removeCallbacks(doUpdateTimeSlider);
     }

     final SeekBar timeSlider = (SeekBar) findViewById(R.id.timeSlider);

     if (timeSlider != null) {
         timeSliderHandler = new Handler();
         startUpdateTimeSlider();
     }

     @Override
     public void onDestroy() {
         super.onDestroy();
         stopUpdateTimeSlider();
     }

该项目未在模拟器中显示。工具提示显示这些错误:

此外,startUpdateTimeSliderstopUpdateTimeSlider 函数在工具提示中显示此错误:

此外,在 运行 window 中,我得到:

emulator: emulator window was out of view and was recentered

emulator: ERROR: _factory_client_recv: Unknown camera factory query name in ' '

任何帮助将不胜感激!

第一期自行解释,需要添加final修饰符。

final Runnable doUpdateTimeSlider = ...

第二个 - 移动 startUpdateTimerSlider() 方法,现在它在 onCreate() 方法中

看起来你错过了 }:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_play);
 Intent intent = getIntent();
 Runnable doUpdateTimeSlider = new Runnable() {
     @Override
     public void run() {
         timeSliderHandler.postDelayed(doUpdateTimeSlider, timeSliderInterval);
         updateTimeSlider();
     }
 };
}//<--------HERE

 void startUpdateTimeSlider() {
     doUpdateTimeSlider.run();
 }