需要一个if语句停止MediaPlayer对象只有当它打 [英] Need an if statement to stop MediaPlayer object only if it's playing

查看:306
本文介绍了需要一个if语句停止MediaPlayer对象只有当它打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个定时器,这样,当它完成后,音乐响起。不过,这首歌没有永远停止播放,如果用户presses后退按钮。于是,我就重写生命周期方法的onPause(),它工作,所以如果定时器练完和音乐竟是打 - 然后使用后退按钮停止音乐。

但是,当用户试图preSS其设备的后退按钮的之前计时器结束(和音乐还未开始),然后我得到一个空指针异常的onPause()方法。

有什么可以导致此?我怎样写code,说:如果MediaPlayer的播放,然后停止并释放它,否则什么都不做。这正是我需要的概念。 (或者有可能是一个更好的方式来写呢?)

这里是定时器的 onFinishes()方法,这使得歌曲开始播放,定时结束时,我的code段为MediaPlayer的。 mpCease 仅仅是一个全局变量重新presents MP ,这样我就可以用它外 onFinish())。

 公共无效onFinish(){
                mTextField.setText(大脑!);
                //这里调用一些媒体功能
                MediaPlayer的MP = MediaPlayer.create(ZombieThemedActivity.this,R.raw.zombie_sound);
                mp.start();
                mpCease = MP;                //这将禁用暂停/播放按钮时计时器结束
                按钮disablePausePlay =(按钮)findViewById(R.id.btnPause);
                disablePausePlay.setEnabled(假);
        }

这里是的onPause方法到目前为止(它尚未完成)编写的,它的作品真的很好停止音乐时,后退按钮是pssed(只有当它已经扮演)$ P $。

  @覆盖
    保护无效的onPause(){
            super.onPause();
        如果(mpCease.isPlaying()){
            mpCease.stop();
            mpCease.release();
        }否则如果(!mpCease.isPlaying()){
            //它应该没事做mpCease或其他任何
        }
    }

LogCat中(当后退按钮是pressed 计时器完成后,它崩溃之前)。

 致命异常:主要
    了java.lang.RuntimeException:无法暂停活动{com.azurespot.disastertimer.app/com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity}:显示java.lang.NullPointerException
            在android.app.ActivityThread.performPauseActivity(ActivityThread.java:2838)
            在android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
            在android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
            在android.app.ActivityThread.access $ 800(ActivityThread.java:130)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1212)
            在android.os.Handler.dispatchMessage(Handler.java:99)
            在android.os.Looper.loop(Looper.java:137)
            在android.app.ActivityThread.main(ActivityThread.java:4745)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:511)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            在dalvik.system.NativeStart.main(本机方法)
     显示java.lang.NullPointerException:产生的原因
            在com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity.onPause(ZombieThemedActivity.java:157)
            在android.app.Activity.performPause(Activity.java:5106)
            在android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1225)
            在android.app.ActivityThread.performPauseActivity(ActivityThread.java:2825)
在android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
在android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
在android.app.ActivityThread.access $ 800(ActivityThread.java:130)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1212)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4745)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
在dalvik.system.NativeStart.main(本机方法)


解决方案

刚刚摆脱 MP的 mpCease 。然后检查,如果这是

  @覆盖
保护无效的onPause(){
        super.onPause();
    如果(mpCease!= NULL)//这里添加一个空检查
    {
        如果(mpCease.isPlaying()){
            mpCease.stop();
            mpCease.release();
        //如果此什么也不做则不需要其他
        }其他{
            //它应该没事做mpCease或其他任何
        }
    }

您甚至不必摆脱 MP 的只是检查是否 mpCease 无效。你不初始化它,直到 onFinish() A CountDownTimer 的(我认为)。所以,如果他们preSS回来,它已经完成之前,那显然是

I have created a timer, so that when it finishes, music plays. But the song does not ever stop playing if the user presses the back button. So I tried to override the lifecycle method onPause(), which worked, so if the timer had finished and the music was actually playing - then using the back button stopped the music.

However, when a user tries to press the back button of their device before the timer finishes (and the music has not started yet), then I get a null pointer exception in the onPause() method.

What can be causing this? How do I write code that says "if MediaPlayer is playing, then stop and release it, otherwise do nothing". That is what I need conceptually. (Or there might be a better way to write it?)

Here is my code snippet for the MediaPlayer in the timer's onFinishes() method, which makes the song start playing, when the timer finishes. (mpCease is just a global variable that represents mp, so I can use it outside of onFinish() ).

public void onFinish() {
                mTextField.setText("BRAINS!!");
                // call some media function here
                MediaPlayer mp = MediaPlayer.create(ZombieThemedActivity.this, R.raw.zombie_sound);
                mp.start();
                mpCease = mp;

                // This disables the pause/play button when timer ends
                Button disablePausePlay = (Button)findViewById(R.id.btnPause);
                disablePausePlay.setEnabled(false);
        }

Here is the onPause method written so far (it's not complete), which works really well to stop the music when the back button is pressed (only if it's already playing).

@Override
    protected void onPause(){
            super.onPause();
        if (mpCease.isPlaying()) {
            mpCease.stop();
            mpCease.release();
        } else if (!mpCease.isPlaying()) {
            // it should do nothing to mpCease or to anything else
        }
    }

LogCat (when back button is pressed before timer is finished, it crashes).

FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to pause activity {com.azurespot.disastertimer.app/com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2838)
            at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
            at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
            at android.app.ActivityThread.access$800(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity.onPause(ZombieThemedActivity.java:157)
            at android.app.Activity.performPause(Activity.java:5106)
            at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1225)
            at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2825)
            at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
            at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
            at android.app.ActivityThread.access$800(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

解决方案

Just get rid of mp and replace it with mpCease. Then check if that is null

@Override
protected void onPause(){
        super.onPause();
    if (mpCease != null) //add a null check here
    {
        if (mpCease.isPlaying()) { 
            mpCease.stop();
            mpCease.release();
        // if this does nothing then the else isn't needed
        } else {
            // it should do nothing to mpCease or to anything else
        }
    }

You don't even have to get rid of mp but just check if mpCease is null. You don't initialize it until onFinish() of (I assume) a CountDownTimer. So, if they press back before it has finished then obviously it is null.

这篇关于需要一个if语句停止MediaPlayer对象只有当它打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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