The play sound does not work when rotate the mobile

a 夏天 提交于 2019-12-25 11:17:19

问题


I have my project to play sound. It worked well but when I rotate the mobile the sound doesn't work also it worked well when the app start. Also there isn't any error.

@JavascriptInterface
public void playsound(String value ) {
    if (value.equals("on")) {
        mp= MediaPlayer.create(mContext,R.raw.sound);
        mp.start();
    } else {  
        mp.stop();
    }
}

回答1:


When your device is rotated, the current Activity gets recreated. You likely need to hook into the Activity lifecycle events in order to properly manage the playing of your sound. See the onPause and onResume methods explained here: http://developer.android.com/guide/components/activities.html#Lifecycle.




回答2:


You should create the MediaPlayer instance in onCreate and start it in onResume(). Alternatively you can make your program work in only either portrait or landscape mode. This can be set in the manifest.



来源:https://stackoverflow.com/questions/17350357/the-play-sound-does-not-work-when-rotate-the-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!