【发布时间】:2011-05-21 05:20:50
【问题描述】:
我想为歌曲仅在 5 秒播放器停止播放歌曲后才播放 5 秒编写代码。我以以下方式使用 AsyncTask,它不会在 5 秒后停止,我不知道在哪里为播放器停止编码编写代码。 task.execute 编码只是延迟了 5 秒以启动活动。请帮我。 在下面的播放器正在播放歌曲但没有停止。 我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text=(TextView)findViewById(R.id.text);
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Log.e("bpm", "in run method");
processor = new BPM2SampleProcessor();
processor.setSampleSize(1024);
EnergyOutputAudioDevice output = new EnergyOutputAudioDevice(processor);
output.setAverageLength(1024);
try {
player = new Player(new FileInputStream("/sdcard/taxi.mp3"), output);
player.play();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
}
Log.e("bpm"," bpm is "+processor.getBPM());
return null;
}
protected void onProgressUpdate(Void... params)
{
text.setText("bpm is "+processor.getBPM());
}
};
try {
task.execute((Void)null).get(5, TimeUnit.SECONDS);
player.close();
} catch(Exception e) {
e.printStackTrace();
}
}
【问题讨论】: