オーディオファイルを再生するメソッドを呼び出すときにスレッドを開始しています。
コードは最初は問題なく実行されますが、playメソッドを再度呼び出すと、最初に呼び出されたかのようにスレッドを開始する必要があります。スレッドを中断し、停止しようとしましたが、何も機能しないようです。
どうすればスレッドを正しく再起動できますか?
説明に役立つコードを次に示します。
グローバル変数
private Thread thread1;
スレッドコード:
thread1 = new Thread(new Runnable()
{
@Override
public void run()
{
try {
int i=0;
final TextView timeDuration = (TextView) findViewById(R.id.timeDisplay);
final SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar1);
while (running)
{
info();
j = 0;
while(i>0 && running)
{
while(j<duration && running && (playStatus.getValue() == "TRANSITIONING" || playStatus.getValue() == "PLAYING"))
{
seekBar.setMax((int) duration);
seekBar.setProgress(0);
runOnUiThread(new Runnable()
{
public void run()
{
System.out.println("PLAYBACK STATUS: "+playStatus.getValue());
timeDuration.setText(""+(j+1));
seekBar.setProgress(j+1);
if(j==(duration-1))
{
setRunning(false);
}
}
});
Thread.sleep(1 * 1000);
j++;
if(seekBar.getProgress() == seekBar.getMax())
{
runOnUiThread(new Runnable()
{
public void run()
{
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.GONE);
timeDuration.setText(""+"0");
seekBar.setProgress(0);
flag = false;
System.out.println("J VALUE 1: = "+j);
duration = 0;
setRunning(false);
}
});
}
}
}
j = 0;
i++;
Thread.sleep(1 * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
play();
このコードは正常に機能し、トラックを再生します。次に、シークバーをリセットし、playメソッドが再度呼び出されるのを待ちます。
public void play()
{
try
{
thread1.start();
}
catch(Exception e)
{
return;
}
}
これが私に推奨されるsetRunningメソッドです。
public void setRunning(boolean b)
{
this.running = b;
}
誰かがこの問題の解決策を知っているなら、私は本当にそれをいただければ幸いです。