Android 应用程序在播放声音时崩溃

发布于 2024-11-30 06:07:24 字数 1348 浏览 1 评论 0原文

我正在开发一个免费的字母表应用程序,但我不是 Java 开发人员。我制作了一个 HTML 页面,其中包含大约 150 个 .png 图片和 .mp3 声音文件对。例如,apple.pngapple.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-12-07 06:07:24

据我所知 MediaPlayer.create() 是同步的。因此它会在调用位置阻止 UI 线程,从而创建 ANR = 应用程序未响应错误。要解决此问题,您需要使用异步 MediaPlayer.prepareAsync() 调用。详情请参阅:
http://developer.android.com/reference/android/media/MediaPlayer.html 而

不是:

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(),Uri.parse(url));

更准确地说,您应该执行类似以下操作,

final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(url));
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                });

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 asynchronous MediaPlayer.prepareAsync() calls. For details see:
http://developer.android.com/reference/android/media/MediaPlayer.html

More precisely, instead of:

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(),Uri.parse(url));

you should do something similar to:

final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(url));
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                });
物价感观 2024-12-07 06:07:24

找到了我自己的解决方案来解决这个问题。

我已将所有声音复制到 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();

现在此代码正在运行。感谢您的帮助=)

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 =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文