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

时间:2017-01-01 03:36:53

标签: java android audio runnable

这是我的第一个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();
     }

项目未在模拟器中显示。工具提示显示以下错误:

enter image description here

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

enter image description here

此外,在“运行”窗口中,我收到了:

  

模拟器:模拟器窗口不在视图中并被重新定位

     

模拟器:错误:_factory_client_recv:未知的相机工厂查询   名称在''

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

第一个问题是自我解释,您需要添加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();
 }