change UIBackgroundModes audio at runtime

三世轮回 提交于 2019-12-07 17:04:20

问题


I have an app that plays music, and I want to enable the background modes. My audio category is set to kAudioSessionCategory_MediaPlayback and if I add the audio string in my UIBackgroundModes inside my info.plist file it keeps playing back the audio. So far so good.

Now I want to give this option to my users, to choose if the audio plays in the background or not. I tried getting the file and deleting the key but so far nothing happens. What I did was (in a selector handled by a UIButton) :

    -(void) disableBackgroundAudio:(UIButton*)button{
        NSDictionary *plistDict = [[NSBundle mainBundle] infoDictionary];
        [plistDict setValue:@"" forKey:@"UIBackgroundModes"];
    }

If i print this value in the console, before and after, I can see that it had "audio" before, and after this is called, I get a blank string. However, if I then push the home button the audio is always playing no matter if I disabled the audio.

My guess is that this is probably not the way to update (although I'm not sure if it's even possible) the info.plist file. Is this possible at all? After all I think giving the users a choice is better for my specific app, and I've seen other apps doing it.

Any help would be greatly appreciated.


回答1:


The Info.plist is a part of you application's bundle. All files in this bundle are immutable.

Keep the audio value there and just pause the music when application enters background (in case the user turned that option off).




回答2:


Why would you want to solve it this way? If the user does not want to hear background audio, simply shut off the music when the application goes in the background...You probably shouldnt be modifying the apps plist at runtime anyway..




回答3:


Can't you just stop audio when your app entered the background if the user doesn't want background running? Furthermore if you want your app to fully exit if backgrounded when the user doesn't want background running then call the exit(...) Function.

But in answer to your question, no you can't change the background modes (or anything in the info.plist) at runtime.



来源:https://stackoverflow.com/questions/14760370/change-uibackgroundmodes-audio-at-runtime

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