简体   繁体   English

打开其他活动时,背景上的android Media Player无法停止播放

[英]android media player on background not stop playing while open other activity

My application has 3 options(Buttons) in my main activity and a media player that plays a song when the application is launched. 我的应用程序在我的主要活动中有3个选项(按钮),媒体播放器在应用程序启动时播放歌曲。 The media player starts correctly when the application is launched, but If i press a button to start a new activity while the audio is still playing, the application crushes (unfortunately app has stopped). 启动应用程序时,媒体播放器会正确启动,但是如果我在音频仍在播放时按按钮开始新的活动,则应用程序崩溃(不幸的是,应用程序已停止)。 If i press "OK" in the message it opens the new activity and media player stops. 如果我在消息中按“确定”,则它将打开新活动,媒体播放器停止。

My aim is to start the new activity and stop the media player (song). 我的目的是开始新活动并停止媒体播放器(歌曲)。

Could anyone help me with these issues.? 有人可以帮我解决这些问题吗?

TextView  logoname;
Button autismlogo,visionlogo,hearinglogo;
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
MediaPlayer player; 


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


logoname = (TextView)findViewById(R.id.logotext);
autismlogo = (Button)findViewById(R.id.autismbutton);
visionlogo = (Button)findViewById(R.id.visionbutton);
hearinglogo = (Button)findViewById(R.id.hearingbutton);



final MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.welcome); 
player.start();

// ---SENSORS-------- 

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new ShakeEventListener();   

mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {   
  public void onShake() {
      Intent vision = new Intent(getApplicationContext(),Vision_main.class);
    startActivity(vision);
  }
});


// ----ON CLICK EVENTS -----------
autismlogo.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {


        Intent autism = new Intent(getApplicationContext(),Autism_main.class);
        startActivity(autism);

    }

});

visionlogo.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {



        Intent vision = new Intent(getApplicationContext(),Vision_main.class);
        startActivity(vision);

    }
});

hearinglogo.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        Intent hearing = new Intent(getApplicationContext(),Hearing_main.class);
        startActivity(hearing);     
    }
});

}

public void onResume() {
    super.onResume();

    mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);
}

public void onPause() {
    super.onPause();
    player.stop();
    mSensorManager.unregisterListener(mSensorListener);
}

Log Cat error 记录猫错误

E/AndroidRuntime(27610): FATAL EXCEPTION: main E/AndroidRuntime(27610): Process: com.giorgospapadopoulos.move4all, PID: 27610 E/AndroidRuntime(27610): java.lang.RuntimeException: Unable to pause activity {com.giorgospapadopoulos.move4all/com.giorgospapadopoulos.move4all.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference E/AndroidRuntime(27610): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3260) E/AndroidRuntime(27610): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219) E/AndroidRuntime(27610): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3194) E/AndroidRuntime(27610): at android.app.ActivityThread.access$1000(ActivityThread.java:151) E/AndroidRuntime(27610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314) E/AndroidRuntime(27610): at android.os.Handler.dispatchMessage(Handler.java:102) E/Andro E / AndroidRuntime(27610):致命异常:main E / AndroidRuntime(27610):进程:com.giorgospapadopoulos.move4all,PID:27610 E / AndroidRuntime(27610):java.lang.RuntimeException:无法暂停活动{com.giorgospapadopoulos .move4all / com.giorgospapadopoulos.move4all.MainActivity}:java.lang.NullPointerException:尝试在null对象引用E / AndroidRuntime(27610)上调用虚拟方法'void android.media.MediaPlayer.stop()':在android上。 app.ActivityThread.performPauseActivity(ActivityThread.java:3260)E / AndroidRuntime(27610):在android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219)E / AndroidRuntime(27610):在android.app.ActivityThread.handlePauseActivity( ActivityThread.java:3194)E / AndroidRuntime(27610):位于android.app.ActivityThread.access $ 1000(ActivityThread.java:151)E / AndroidRuntime(27610):位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java :1314)E / AndroidRuntime(27610):位于android.os.Handler.dispatchMessage(Handler.java:102)E / Andro idRuntime(27610): at android.os.Looper.loop(Looper.java:135) E/AndroidRuntime(27610): at android.app.ActivityThread.main(ActivityThread.java:5254) E/AndroidRuntime(27610): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(27610): at java.lang.reflect.Method.invoke(Method.java:372) E/AndroidRuntime(27610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) E/AndroidRuntime(27610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) E/AndroidRuntime(27610): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference E/AndroidRuntime(27610): at com.giorgospapadopoulos.move4all.MainActivity.onPause(MainActivity.java:220) E/AndroidRuntime(27610): at android.app.Activity.performPause(Activity.java:6101) E/AndroidRuntime(27610): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1310) E/AndroidRuntime(27610): at android.app.Activi idRuntime(27610):在android.os.Looper.loop(Looper.java:135)E / AndroidRuntime(27610):在android.app.ActivityThread.main(ActivityThread.java:5254)E / AndroidRuntime(27610):在java.lang.reflect.Method.invoke(本机方法)E / AndroidRuntime(27610):在java.lang.reflect.Method.invoke(Method.java:372)E / AndroidRuntime(27610):在com.android.internal .os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)E / AndroidRuntime(27610):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)E / AndroidRuntime(27610):由引起:java.lang.NullPointerException:尝试在空对象引用E / AndroidRuntime(27610)上调用虚拟方法'void android.media.MediaPlayer.stop()':com.giorgospapadopoulos.move4all.MainActivity.onPause(MainActivity.java :220)E / AndroidRuntime(27610):位于android.app.Activity.performPause(Activity.java:6101)E / AndroidRuntime(27610):位于android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1310)E / AndroidRuntime (27610):位于android.app.Activi tyThread.performPauseActivity(ActivityThread.java:3246) E/AndroidRuntime(27610): ... 11 more tyThread.performPauseActivity(ActivityThread.java:3246)E / AndroidRuntime(27610):...还有11个

Your error is quite clear. 您的错误很清楚。 You get a NullPointerException at line 120 in your onPause() method. 您在onPause()方法的第120行获得NullPointerException That's because you haven't created the player object and you try to invoke one of it's methods. 这是因为您尚未创建player对象,而是尝试调用它的方法之一。

You have declared it as a global variable but you haven't created it. 您已将其声明为全局变量,但尚未创建。 You create a different player object inside your onCreate() method but that's just a local variable. 您可以在onCreate()方法中创建另一个player对象,但这只是一个局部变量。

  1. Firstly in line 首先排队

final MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.welcome);

not declare MediaPlayer player as local, it should be global. 不要将MediaPlayer播放器声明为本地播放器,它应该是全局播放器。

  1. Use this in onPause() onPause()使用它

    if(player!=null){ player.stop(); if(player!= null){player.stop(); } }

  2. in onResume() onResume()

    if( player!=null){ player.start(); if(player!= null){player.start(); } }

by above code player will play music when app in foreground, when app in background player stop playing music and when app again comes in foreground state player will play music. 通过上述代码播放器将在前景中的应用程序播放音乐,背景播放器中的应用程序停止播放音乐以及应用程序再次进入前景状态时播放音乐。

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

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