我正在开发通话录音应用程序。我设法在ListView中显示音频文件,并能够在MediaPlayer中播放。知道我在SeekBar Handler中遇到了问题。当我播放音频文件时,MediaPlayer和SeekBar工作正常,但当我关闭我的应用程序时,SeekBar的Handler仍然有效。我怎么能阻止处理程序。
private void playSong(String string)
throws IllegalArgumentException, SecurityException,
IllegalStateException, IOException {
// seekBar
setContentView(R.layout.dialog_activity);
startButton = (Button) findViewById(R.id.play_button);
stopButton = (Button) findViewById(R.id.pause_button);
seekBar = (SeekBar) findViewById(R.id.seek_bar);
startButton.setOnClickListener(MainActivity.this);
stopButton.setOnClickListener(MainActivity.this);
// MediaPlayer
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(string);
mp.prepare();
mp.start();
seekBar.setMax(mp.getDuration());
try {
seekUpdation();
} catch (Exception e) {
Toast.makeText(MainActivity.this,
"SeekUpdation " + e.toString(), Toast.LENGTH_SHORT)
.show();
}
}
Runnable run = new Runnable() {
@Override
public void run() {
try {
seekUpdation();
} catch (Exception e) {
Log.d("Runnable ", "error in Runnable =" + e.toString());
}
}
};
这是处理程序在关闭应用程序后保持活动状态。
public void seekUpdation() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
Toast.makeText(MainActivity.this, "Uploaded" + seekHandler,
Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:0)
我自己找到了答案。
我们必须从此
更改代码 Runnable run = new Runnable() {
@Override
public void run() {
try {
seekUpdation();
} catch (Exception e) {
Log.d("Runnable ", "error in Runnable =" + e.toString());
}
}
};
public void seekUpdation() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
Toast.makeText(MainActivity.this, "Uploaded" + seekHandler,
Toast.LENGTH_SHORT).show();
}
});
}
到这个
Runnable run = new Runnable() {
public void run() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(this, 1000);
}
};
private void seekUpdation() {
seekBar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
Toast.makeText(MainActivity.this, "Uploaded" + seekHandler,
Toast.LENGTH_SHORT).show();
}