android中的异步任务问题
我想做歌曲的代码,只有在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();
}
}
i want to do code for song is should play for 5 seconds only after 5 seconds player should stop the song play. i use AsyncTask in following way it does not stop after 5 seconds and i do not know where to write code for player stop coding in the following. the task.execute coding just delayed for 5 seconds to start the activity. please help me.
in the following player is playing the song but not stop.
my code:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置此任务后,您不应该关闭播放器。相反,应在任务创建后 5 秒调用
player.close()
。这可以通过使用 Handler 对象在您的doInBackground()
实现中发布一个 Runnable 对象来停止播放器来完成。You shouldn't be closing the player after you set this task. Instead,
player.close()
should be called 5 seconds after the task is created. This could be done by using a Handler object to post a Runnable object in your implementation ofdoInBackground()
that stops the player.