如何正确发布Android MediaPlayer [英] How to properly release Android MediaPlayer

查看:96
本文介绍了如何正确发布Android MediaPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Android应用程序中添加一个按钮,当按下按钮时它会播放MP3。
我已经让它工作了,但没有办法释放mediaPlayer对象 - 因此它甚至在我离开活动后仍在继续播放。如果我在react()方法之外初始化MediaPlayer对象(按下按钮时会调用它),这会导致应用程序在打开活动时强制关闭。但是如果我在react()方法中初始化MediaPlayer,那么我就不能在onQuit()方法中使用mplayer.release。我在这里看不到什么?

I'm trying to add a button to my android app where it plays an MP3 when the button is tapped. I've gotten it working, but without a way to release the mediaPlayer object - therefore it keeps playing even after I leave the activity. If I initialize the MediaPlayer object outside of my react() method(what gets called when the button is pressed) it causes the app to force close when the activity is opened. But if I initialize the MediaPlayer within the react() method I then can't use mplayer.release in the onQuit() method. What am I not seeing here?

    public void react(View view) {
    MediaPlayer mediaPlayer = MediaPlayer.create(ToBeOrNot.this, R.raw.achord);
    mediaPlayer.start();
}
protected void onStop(){
    mediaPlayer.release();
    mediaPlayer = null;
}

由于显而易见的原因不起作用

Doesn't work for obvious reasons and

MediaPlayer mediaPlayer = MediaPlayer.create(ToBeOrNot.this, R.raw.achord);
public void react(View view) {
            mediaPlayer.start(); 
}
protected void onStop(){
    mediaPlayer.release();
    mediaPlayer = null;
}

使其强制关闭。

更新:
这是整个java类。

Update: Here is the whole java class.

public class ToBeOrNot extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_to_be_or_not);

        }
MediaPlayer mediaPlayer;

public void react(View view) {
        mediaPlayer = MediaPlayer.create(ToBeOrNot.this, R.raw.achord);
        mediaPlayer.start(); 
}
protected void onStop(){
    mediaPlayer.release();
    mediaPlayer = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.activity_to_be_or_not, menu);
    // Locate MenuItem with ShareActionProvider
   return true;
}

}

我认为它的作用是相对的自我解释。当被叫时,它会显示一些文字和一个按钮,当点击时开始播放录音。当有人按下后退按钮时,它应该返回上一个活动并停止录制。
感谢您的帮助!

I think what it does is relatively self explanatory. When called, it shows some text plus a button that when tapped starts a recording playing. When someone hits the back button, it should go back to the previous activity and stop the recording. Thanks for helping me!

推荐答案

您无法在所有方法之外初始化mediaplayer对象。如果这样做,它会尝试使用尚未创建的上下文。您需要将其声明为类变量(在方法之外),并在其中初始化:

You can't initialize the mediaplayer object outside of all methods. If you do, it tries to use a context which hasn't been created yet. You need to declare it as a class variable(outside the method), and initialize it inside:

MediaPlayer mediaPlayer;

public void react(View view) {
    mediaPlayer = MediaPlayer.create(ToBeOrNot.this, R.raw.achord);
    mediaPlayer.start(); 
}

protected void onStop(){
    mediaPlayer.release();
    mediaPlayer = null;
}

另外,我建议你阅读 Java中的变量范围

这篇关于如何正确发布Android MediaPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆