简体   繁体   English

停止并在Mediaplayer中播放mp3

[英]Stop and play mp3 in mediaplayer

I have placed two mp3 songs in raw folder. 我在原始文件夹中放置了两首mp3歌曲。 I have used two buttons to play them. 我使用了两个按钮来播放它们。 I am calling it as below: 我这样称呼它:

public class Songs extends Activity implements OnClickListener {

    private Button mBtn1, mBtn2, mBtn3, mBtn4, mBtn5, mBtn6, mBtn7, mBtn8, mBtn9, mBtn10;
    MediaPlayer mp;
    boolean playing = false;


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

        mBtn1 = (Button) findViewById(R.id.button1);
        mBtn2 = (Button) findViewById(R.id.button2);

        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        else if (v.getId() == mBtn1.getId()) {

            if (playing) {
                mp.stop();
                mp.pause();
                playing = false;
            } else {
                MediaPlayer mp = MediaPlayer.create(Songs.this, R.raw.chahun);
                mp.start();
                playing = true;
                //  mp.release();
            }
        } else if (v.getId() == mBtn2.getId()) {

            if (playing) {
                mp.stop();
                mp.pause();
                playing = false;
            } else {
                MediaPlayer mp = MediaPlayer.create(Songs.this, R.raw.sunn);
                mp.start();
                playing = true;
                //  mp.release();
            }

        }
    }
} 

But I think it is giving error while stopping the song. 但我认为停止播放歌曲时出现错误。 I'm getting error as following: 我收到如下错误:

 05-21 11:49:23.006: E/AndroidRuntime(15436): FATAL EXCEPTION: main
    05-21 11:49:23.006: E/AndroidRuntime(15436): java.lang.NullPointerException
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at com.qwinix.lucia.Songs.onClick(Songs.java:99)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at android.view.View.performClick(View.java:3517)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at android.view.View$PerformClick.run(View.java:14155)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at android.os.Handler.handleCallback(Handler.java:605)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at android.os.Handler.dispatchMessage(Handler.java:92)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at android.os.Looper.loop(Looper.java:154)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at android.app.ActivityThread.main(ActivityThread.java:4624)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at java.lang.reflect.Method.invoke(Method.java:511)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    05-21 11:49:23.006: E/AndroidRuntime(15436):    at dalvik.system.NativeStart.main(Native Method)

Can anyone please solve this? 谁能解决这个问题? Thanks 谢谢

you forget to initialize mp instance of MediaPlayer . 您忘记初始化MediaPlayer的mp实例。 so you will need to initialize mp instead of creating new one on Button Click as: 因此,您将需要初始化mp而不是在Button Click上创建新的mp

if (v.getId() == mBtn1.getId()) {

    if (playing) {
            .....
    } else {
        mp = MediaPlayer.create(Songs.this, R.raw.chahun);
            ....
    }
} else if (v.getId() == mBtn2.getId()) {

    if (playing) {
            ......
    } else {
        mp = MediaPlayer.create(Songs.this, R.raw.sunn);
            .....
    }

}

}

You have'nt read the documentation properly that's why you are facing this exception 您尚未正确阅读文档,这就是您遇到此异常的原因

 if (playing) {

     if (mp != null) {
         mp.pause();
     }    
 }

If you media is already playing . 如果您的媒体已经在播放。 First of all apply a check for its null condition then don't call stop() method make it pause by calling pause(). 首先,对它的null条件进行检查,然后不要调用stop()方法使其通过调用pause()暂停。

Reason of the exception 异常原因

Before calling stop() method we have to pause the media first. 在调用stop()方法之前,我们必须先暂停媒体。

Refer the official docs 参考官方文档

public void stop ()

Stops playback after playback has been stopped or paused. 停止或暂停播放后停止播放。

It is clearly saying has been paused means either you have to pause the play back by calling Pause() or it gets stopped after completion. 显然,已暂停表示您必须通过调用Pause()暂停播放,或者在完成后停止播放。

http://developer.android.com/reference/android/media/MediaPlayer.html#stop%28%29 http://developer.android.com/reference/android/media/MediaPlayer.html#stop%28%29

Sugesstion -- One of the goods method for debugging which I really follow is as soon as I am getting exception I started finding all the possible causes for that exception wether I have done all the things correct or not. 挂起 -我真正遵循的一种调试商品方法是,一旦我遇到异常,无论我是否做了所有正确的事情,我都开始寻找导致该异常的所有可能原因。 If not getting solved then I use to read the documents of the methods from the use of which the exception is generated. 如果没有解决,那么我会使用生成异常的方法读取文档。

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

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