MediaPlayer 大约 5 秒后停止播放

发布于 2024-11-12 16:55:42 字数 247 浏览 2 评论 0原文

我目前正在开发一个简单的游戏,现在是时候添加音乐和音效了。我尝试使用 MediaPlayer,就像此处所述:Android 媒体播放器错误

但是我还有另一个问题,MediaPlayer 在大约 5 秒后停止播放音乐。可能是什么原因造成的?

I'm currently developing a simple game and now it's time to add music and sound effect. I tried using MediaPlayer, just like described here: Android media player bug

However I have another problem, the MediaPlayer stop playing the music after about 5 seconds. What is probable causing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

°如果伤别离去 2024-11-19 16:55:42

我也有这个问题。
这可能是由于 MediaPlayer 对象仅存在于方法中。

例如:

//ERROR, stops after 5 sec!
public static void playMusic(int id)
{
  MediaPlayer mediaPlayer = MediaPlayer.create(context, id);
  mediaPlayer.setLooping(true);
  mediaPlayer.start();
}

垃圾收集器很可能会介入并清除 MediaPlayer 对象。

这为我解决了错误:

//mediaPlayer-object will not we cleaned away since someone holds a reference to it!
private static MediaPlayer mediaPlayer;

public static void playMusic(int id)
{
    mediaPlayer = MediaPlayer.create(context, id);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
}

I had this problem too.
It was probably due to having the MediaPlayer-object only existing within a method.

For example:

//ERROR, stops after 5 sec!
public static void playMusic(int id)
{
  MediaPlayer mediaPlayer = MediaPlayer.create(context, id);
  mediaPlayer.setLooping(true);
  mediaPlayer.start();
}

It is most likely that the garbage collector will come in and clean away the MediaPlayer-object.

This fixed the error for me:

//mediaPlayer-object will not we cleaned away since someone holds a reference to it!
private static MediaPlayer mediaPlayer;

public static void playMusic(int id)
{
    mediaPlayer = MediaPlayer.create(context, id);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
}
黑白记忆 2024-11-19 16:55:42

就我而言,这只是因为在对话框中使用 MediaPlayer 并在播放名为 dismiss() 函数的声音后,垃圾收集器将消除 MediaPlayer 对象,并导致 MediaPlayer 停止工作,这是一种更好的方法在这种情况下播放声音是在父类中播放它而不是像这样在对话框中播放它

/* declare myAudio as public */

myAudio = MediaPlayer.create(this, R.raw.my_audio);
CustomDialog cdd = new CustomDialog(MainActivity.this);
cdd.show();
Button playMyMediaSoundBtn = cdd.findViewById(R.id.play_button);
playMyMediaSoundBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
            myAudio.start();
            cdd.dismiss();
        }
     });

In my case that was just because using MediaPlayer in a Dialog and right after playing the sound called dismiss() function therefore the garbage collector would eliminate the MediaPlayer object and it cause MediaPlayer stop working, a better way to play sound in such situation is to play it in the parent class instead of playing it in the Dialog like as this

/* declare myAudio as public */

myAudio = MediaPlayer.create(this, R.raw.my_audio);
CustomDialog cdd = new CustomDialog(MainActivity.this);
cdd.show();
Button playMyMediaSoundBtn = cdd.findViewById(R.id.play_button);
playMyMediaSoundBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
            myAudio.start();
            cdd.dismiss();
        }
     });
痴意少年 2024-11-19 16:55:42

您应该创建一个异步代码,让媒体播放器播放他必须在后台播放的内容。像这样的东西:

final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.nomarxia);


                Handler mHandler = new Handler();
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        mp.start();
                    }
                }, mp.getDuration());

You should create an asychronous code to let media player play what he has to play in the background. Something like this:

final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.nomarxia);


                Handler mHandler = new Handler();
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        mp.start();
                    }
                }, mp.getDuration());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文