0

MediaPlayer で曲の進行状況に応じてシークバーを更新しようとしています。私はスレッドを使用してそのタスクを実行しています。

最初に、スレッド内で Thred を使用して UI を更新しようとしましたが、アプリがクラッシュし、元のスレッドのみがビューに接続できると言います。

次に、実行可能なスレッド内のハンドラーで更新しようとしました。これは正常に機能しますが、シークバーを更新していません。ログを取得すると、ループがハンドラー内にないことがわかります。どこに問題があるのか​​わからない。SeekBar の更新を手伝ってください。

コード:

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        try {
            if ((musicPath != mIRemoteService.getPath())) {
                System.out.println("..... MUSIC CHANGE.....");
                setOrUpdateData();
                updateFragmentLayout();     

            }

            // Displaying Current Duration time/Progress

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    try {
                        songCurrentDurationLabel.setText(""+ Utilities.milliSecondsToTimer(mIRemoteService.position()));
                        songProgressBar.setProgress((int)mIRemoteService.position());
                        System.out.println("Runnnnn.........");
                        //songProgressBar.invalidate();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        System.out.println("Runnnnn......... EXCEPTION....");
                    }

                }
            });
            System.out.println("Runnnnn.........MAIN....");

            if (!(mIRemoteService.isPlayerRunning())) {
                btnPlay.setImageDrawable(getResources().getDrawable(R.drawable.play_now_playing));
                mHandler.removeCallbacks(mUpdateTimeTask);
                System.out.println("Runnnnn.........MAIN....IF");

            }else{
                System.out.println("Runnnnn.........MAIN....ELSE");
                mHandler.post(mUpdateTimeTask);     
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
4

1 に答える 1

3

ハンドラーまたはスレッドのいずれかを使用できます。スレッドを使用すると、UI スレッドに変更を投稿する必要があります。

private class UpdateSeekBar extends Thread
{
    @Override
    public void run()
    {
        super.run();

        while (null != mp && mp.isPlaying() && this.isAlive())
        {
            //final int min = (mp.getCurrentPosition() / 1000) / 60;
            //final int sec = (mp.getCurrentPosition() / 1000) % 60;

            MainActivity.this.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        songCurrentDurationLabel.setText("" + Utilities.milliSecondsToTimer(mIRemoteService.position()));
                        songProgressBar.setProgress((int) mIRemoteService.position());
                        System.out.println("Runnnnn.........");
                        // songProgressBar.invalidate();
                    }
                    catch (RemoteException e)
                    {
                        e.printStackTrace();
                        System.out.println("Runnnnn......... EXCEPTION....");
                    }
                }
            });

            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}
于 2014-12-03T08:42:57.800 に答える