0

Androidメディアプレーヤーを使用してMIDIファイルを再生するアプリがあります。再生の進行状況に基づいて動的に変化するタイマーとしてテキストビューを作成しました。ただ、2秒位で止まってしまうので非常にバグが多いです。

class progressTrack extends AsyncTask<String,Void,String>{

            @Override
            protected String doInBackground(String... arg0) {
                ToggleButton mStartStop;
                int currentPosition=0;
                int total=mediaPlayer.getDuration();
                TextView timer=(TextView) findViewById(R.id.timer);
                while (mediaPlayer != null && currentPosition < total) {
                    try {
                        Thread.sleep(1000);
                        currentPosition = mediaPlayer
                                .getCurrentPosition();
                        double seconds=currentPosition/1000;
                        int time= (int) Math.round(seconds);
                        String timeS=Integer.toString(time);
                        Log.d("Time",timeS);
                        timer.setText(timeS+"s");
                        fSlider.setProgress(currentPosition);
                    } catch (InterruptedException e) {

                    } catch (Exception e) {

                    }
                    OnSeekBarChangeListener listener = new OnSeekBarChangeListener() {
                        public void onStopTrackingTouch(SeekBar seekBar) { }
                        public void onStartTrackingTouch(SeekBar seekBar) { }
                        public void onProgressChanged(SeekBar seekBar, 
                                int progress,
                                boolean fromUser) {
                            if(fromUser) 
                                mediaPlayer.seekTo(progress);
                        }
                    };
                    fSlider.setOnSeekBarChangeListener(listener);
                }

時刻は Logcat では正しく表示されますが、テキストビューでは変更されないのはなぜですか?

解決済み:

protected void onPostExecute(String result) {

                ScheduledExecutorService myScheduledExecutorService = Executors.newScheduledThreadPool(1);
                myScheduledExecutorService.scheduleWithFixedDelay(
                         new Runnable(){
                             @Override
                             public void run() {
                              monitorHandler.sendMessage(monitorHandler.obtainMessage());
                             }}, 
                                  200, //initialDelay
                                  200,
                                  TimeUnit.MILLISECONDS//delay                                    TimeUnit.MILLISECONDS
                         );                                                                                         
            }           
 }

 Handler monitorHandler = new Handler(){

      @Override
      public void handleMessage(Message msg) {
       startUI();
      }

 };

 void startUI(){
        Log.d("UI", "Started player UI");
        ToggleButton mStartStop;
        int currentPosition=0;
        int total=mediaPlayer.getDuration();
        TextView timer=(TextView) findViewById(R.id.timer);
        if(mediaPlayer.isPlaying())
            try {
                currentPosition = mediaPlayer
                        .getCurrentPosition();
                double seconds=currentPosition/1000;
                int time= (int) Math.round(seconds);
                String timeS=Integer.toString(time);
                timer.setText(timeS+"s");
                Log.d("position",timeS);
                fSlider.setProgress(currentPosition);
            }  catch (Exception e) {

            }
            OnSeekBarChangeListener listener = new OnSeekBarChangeListener() {
                public void onStopTrackingTouch(SeekBar seekBar) { }
                public void onStartTrackingTouch(SeekBar seekBar) { }
                public void onProgressChanged(SeekBar seekBar, 
                        int progress,
                        boolean fromUser) {
                    if(fromUser) 
                        mediaPlayer.seekTo(progress);
                }
            };
            fSlider.setOnSeekBarChangeListener(listener);

        mStartStop= (ToggleButton) findViewById(R.id.toggleButton1);
        mStartStop.setOnCheckedChangeListener(pauseListener);

 }
4

1 に答える 1

0

これを別のスレッドで実行し、 runnable to set text を含む runOnUiThreadを介して GUI スレッドにポストバックする必要があります。おそらく例外なくGUIコンポーネントのテキストを設定しているので、今のところこれを行っていないと思います。

完全な方法を投稿してください。完全なメソッドを使用すると、2 秒のマークで停止する理由を突き止めることができます。

于 2013-07-12T18:31:26.443 に答える