简体   繁体   English

如何正确发布Android MediaPlayer

[英]How to properly release Android MediaPlayer

I'm trying to add a button to my android app where it plays an MP3 when the button is tapped.我正在尝试向我的 android 应用程序添加一个按钮,当点击该按钮时它会播放 MP3。 I've gotten it working, but without a way to release the mediaPlayer object - therefore it keeps playing even after I leave the activity.我已经让它工作了,但没有办法释放 mediaPlayer 对象 - 因此即使在我离开活动后它也会继续播放。 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.如果我在 react() 方法(按下按钮时调用的方法)之外初始化 MediaPlayer 对象,它会导致应用程序在活动打开时强制关闭。 But if I initialize the MediaPlayer within the react() method I then can't use mplayer.release in the onQuit() method.但是如果我在 react() 方法中初始化 MediaPlayer 我就不能在 onQuit() 方法中使用 mplayer.release 。 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;
}

Causes it to force close.导致它强制关闭。

Update: Here is the whole java class.更新:这是整个java类。

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!谢谢你帮助我!

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;
}

In addition, I'd recommend reading up on variable scope in Java .此外,我建议阅读Java 中的变量作用域

This Worked for me well.这对我很有用。

public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;

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

        mediaPlayer = MediaPlayer.create(this, R.raw.beep_warning);

        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    mediaPlayer.start();
                    mediaPlayer.setLooping(true);
                }
                else{
                    mediaPlayer.pause();
                }
                }


        });

    }
    @Override
    protected void onStop() {
        super.onStop();
        mediaPlayer.release();
    }

According to Google docs , you can implement a listener in MediaPlayer like the below example.根据 Google docs ,您可以在MediaPlayer实现一个监听器,如下例所示。

You can Release and Reset automatically when it's complete.完成后,您可以自动释放和重置。

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_one);
mp.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.reset();
        mp.release();
        mp=null;
    }
});
mp.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM