Android 应用程序在播放声音时崩溃
我正在开发一个免费的字母表应用程序,但我不是 Java 开发人员。我制作了一个 HTML 页面,其中包含大约 150 个 .png
图片和 .mp3
声音文件对。例如,apple.png
和 apple.mp3
是一对,而且还会有更多。
我正在使用 webview 显示带有图片的网页,并了解用户何时尝试听到声音。这是我当前使用的代码:
index.html:
...<a href="mp3/apple.mp3"><img src="apple.png"></a>...
alphabetActivity.java:
...public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".mp3")){
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(url));
Toast.makeText(HelloWebviewActivity.this, url, Toast.LENGTH_SHORT).show();
mediaPlayer.start();
} else {
Toast.makeText(HelloWebviewActivity.this, "not mp3", Toast.LENGTH_SHORT).show();
}
return true;
}...
所有声音都存储在 assets/www/mp3
中。
。
但有一个问题:每次我单击图片时,我的应用程序都会崩溃并显示 Forced close...
消息。有什么办法让它发挥作用吗?
找到了我自己的解决方案来解决这个问题。
我已将所有声音复制到 res/raw 文件夹,将 index.html 中的链接从“mp3/apple.mp3”更改为“apple”并使用此代码:
if(mPlayer!=null){
mPlayer.stop();
mPlayer.release();}
int id = getResources().getIdentifier(url.substring(26), "raw", getPackageName());;
mPlayer = MediaPlayer.create(getApplicationContext(), id);
mPlayer.start();
现在此代码正在运行。感谢您的帮助=)
I'm developing a free alphabet application but I'm not a Java developer. I've made an HTML page where there are about 150 .png
pictures and .mp3
sound file pairs. For example, apple.png
and apple.mp3
would be a pair, and there are going to be more.
I'm using webview to display the webpage with pictures and to know when the user is trying to hear the sound. Here is the code I am currently using:
index.html:
...<a href="mp3/apple.mp3"><img src="apple.png"></a>...
alphabetActivity.java:
...public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".mp3")){
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(url));
Toast.makeText(HelloWebviewActivity.this, url, Toast.LENGTH_SHORT).show();
mediaPlayer.start();
} else {
Toast.makeText(HelloWebviewActivity.this, "not mp3", Toast.LENGTH_SHORT).show();
}
return true;
}...
All sounds are stored at assets/www/mp3
.
.
But there is a problem: every time I click on a picture, my application crashes with a Forced close...
message. Is there any way to make it work?
Found my own solution for this problem.
I've copied all the sounds to res/raw folder, changed links in index.html from "mp3/apple.mp3" to "apple" and used this code:
if(mPlayer!=null){
mPlayer.stop();
mPlayer.release();}
int id = getResources().getIdentifier(url.substring(26), "raw", getPackageName());;
mPlayer = MediaPlayer.create(getApplicationContext(), id);
mPlayer.start();
Right now this code is working. Thanks for help =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知 MediaPlayer.create() 是同步的。因此它会在调用位置阻止 UI 线程,从而创建 ANR = 应用程序未响应错误。要解决此问题,您需要使用异步
MediaPlayer.prepareAsync()
调用。详情请参阅:http://developer.android.com/reference/android/media/MediaPlayer.html 而
不是:
更准确地说,您应该执行类似以下操作,
As far as I know
MediaPlayer.create()
is synchronous.Thus it blocks the UI thred in the place of invocation, creating ANR = Application Not Responsive error. To fix that you need to use asynchronousMediaPlayer.prepareAsync()
calls. For details see:http://developer.android.com/reference/android/media/MediaPlayer.html
More precisely, instead of:
you should do something similar to:
找到了我自己的解决方案来解决这个问题。
我已将所有声音复制到 res/raw 文件夹,将 index.html 中的链接从“mp3/apple.mp3”更改为“apple”并使用此代码:
现在此代码正在运行。感谢您的帮助=)
Found my own solution for this problem.
I've copied all the sounds to res/raw folder, changed links in index.html from "mp3/apple.mp3" to "apple" and used this code:
Right now this code is working. Thanks for help =)