简体   繁体   English

Android在活动离开/停止时停止mediaplayer,但在方向更改时不停止

[英]Android stopping mediaplayer on activity leave/stop but not on orientation change

I am using MediaPlayer to play some sound in my app, I want this playback to stop when user leaves activity in which sound is being played. 我正在使用MediaPlayer在我的应用程序中播放一些声音,我希望当用户离开正在播放声音的活动时停止播放。 So I did this by calling mediaplayer.stop() in my activities onStop() method. 因此,我通过在我的活动onStop()方法中调用mediaplayer.stop()来实现此目的。

public void onStop() {
 super.onDestroy();
 mediaPlayer.stop();
 mediaPlayer.reset();
}

but now when device rotation changes even then sound playback stops,because onStop() method is called, which I don't want, playback should continue when device orientation changes. 但是现在,即使设备旋转发生变化,甚至声音播放停止,因为我都不希望使用onStop()方法(我不希望这样做),所以当设备方向发生变化时,播放应该继续。 How can this be done. 如何才能做到这一点。

只需将此ligne添加到您要解决的<Activity> Manifest.xml

android:configChanges="keyboardHidden|orientation|screenSize"

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

相关问题 Android:停止重新创建方向更改活动 - Android: Stop Recreating the activity on orientation change Android:在活动被破坏时停止Mediaplayer - android: stopping mediaplayer when the activity is destroyed Android MediaPlayer.stop()暂停而不是停止 - Android MediaPlayer.stop() pauses instead of stopping 当我离开我的应用程序时如何停止 MediaPlayer,但当我移动到另一个 Activity (Android Studio) 时不能 - How can I stop MediaPlayer when I leave my application, but not when I move to another Activity (Android Studio) 在Android中更改活动时如何停止MediaPlayer - How to stop a MediaPlayer when changing activity in Android Android:在通话期间停止小部件的MediaPlayer活动 - Android: Stopping MediaPlayer activity of widget during Phone Calls 使用侧键盘停止方向更改(Android) - Stopping orientation change with side-keyboard (Android) Android:在活动屏幕方向更新期间保持MediaPlayer运行 - Android: Keep MediaPlayer running during Activity screen orientation update 如何在方向变化上停止整个活动娱乐 - How to stop whole activity recreation on orientation change Android MediaPlayer,更改活动时SeekBar异常 - Android MediaPlayer, SeekBar Exception when change activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM