Android,按下按钮时音乐播放滞后
我在一个非常简单的应用程序中有两个按钮,这是单击其中任何一个按钮时的代码:
public void button_clicked1(View v)
{
text1.setText("1"+width);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_a);
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
public void button_clicked2(View v)
{
text1.setText("2"+height);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_b);
/*
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
mp.start();
}
每个原始钢琴音符大约为 20 秒...但是当我按下其中一个按钮时,我希望它播放立即文件...但我发现有一个滞后(并且滞后越来越大),有时如果我快速按下两个按钮几次...它会强制退出:(
I have two buttons in a very simple app, this is my code when either of those buttons are clicked:
public void button_clicked1(View v)
{
text1.setText("1"+width);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_a);
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
public void button_clicked2(View v)
{
text1.setText("2"+height);
mp = MediaPlayer.create(GameScreen.this, R.raw.piano_b);
/*
try {
mp .prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
mp.start();
}
Each of those raw piano notes is around 20 secs... but when i press either one of the button i want it to play the file immediately... but I find there is a lag (and the lag keeps getting bigger) and sometimes if i press both the buttons quick a few times... it force quits :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,当加载原始音频文件(正确编码)时,您不需要调用准备,只需:
这应该可以做到。
According to the docs, when loading a raw audio file (properly encoded), you don't need to call prepare, just:
This should do it.