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

落爺英雄遲暮 提交于 2019-12-12 10:24:53

问题


This is my first Android/Java app. I am using the first answer here to try to initiate a repeating task, updating a seekbar ("timeSlider") to show progress as an audio file plays. Here is my code (eliminating a few irrelevant lines):

 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();
     }

The project does not display in the emulator. Tooltips show these errors:

In addition, the startUpdateTimeSlider and stopUpdateTimeSlider functions are showing this error in tooltips:

Also, in the Run window, I'm getting:

emulator: emulator window was out of view and was recentered

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

Any help will be greatly appreciated!


回答1:


First issue is self explained, you need to add final modifier.

final Runnable doUpdateTimeSlider = ...

Second one - move startUpdateTimerSlider() method, now its inside onCreate() method

Looks like you missed }:

@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();
 }


来源:https://stackoverflow.com/questions/41413177/android-java-update-seekbar-to-show-progress-as-audio-file-plays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!