iOS的背景音乐问题 [英] iOS background music problems
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/8467533/ios-sdk-playing-music-on-the-background-and-switching-views\">iOS SDK:打在背景音乐和切换视图
块引用>大家好即时通讯目前正在使用此code在我的应用程序来播放背景音乐时,其推出的。但是当我切换视图我SecondView并返回到我的firstView音乐开始了和重复当前播放的音乐它不断重复的音乐,当我返回到我的firstView,当我从SecondView切换回我的firstView我怎么能有音乐开始了。
FirstViewController.m
- (无效)viewDidLoad中{
* NSString的路径= [[一个NSBundle mainBundle] pathForResource:@鼓ofType:@MP3];
theAudio = [[AVAudioPlayer页头] initWithContentsOfURL:[NSURL fileURLWithPathath]错误:NULL];
theAudio.delegate =自我;
theAudio.numberOfLoops = -1;
[theAudio玩]
解决方案您可以使用NSUserDefaults的保存音乐播放状态,因此该视图加载时,音乐将只如果它有没有玩过。
如果([[NSUserDefaults的standardUserDefaults] stringForKey:@乐] ==无){
* NSString的路径= [[一个NSBundle mainBundle] pathForResource:@鼓ofType:@MP3];
theAudio = [[AVAudioPlayer页头] initWithContentsOfURL:[NSURL fileURLWithPathath]错误:NULL];
theAudio.delegate =自我;
theAudio.numberOfLoops = -1;
[theAudio玩]
[NSUserDefaults的standardUserDefaults]的setObject:@ - forKey:@音乐]; }您必须退出应用程序时,重置回此键为零。否则,如果玩家退出应用程序,该应用程序已重新启动@音乐下一次串不会是零。
转到 projectAppDelegate.m 文件:
- (无效)applicationDidEnterBackground:(UIApplication的*){应用
[NSUserDefaults的standardUserDefaults]的setObject:无forKey:@音乐]; }如果音乐正在整个应用程序打,然后把音乐播放codeS,如下图所示是安全的。不要忘了申报AVAudioPlayer * theAudio在projectAppDelegate.h文件。
- (无效)applicationDidBecomeActive:(UIApplication的*){应用
* NSString的路径= [[一个NSBundle mainBundle] pathForResource:@鼓ofType:@MP3];
theAudio = [[AVAudioPlayer页头] initWithContentsOfURL:[NSURL fileURLWithPathath]错误:NULL];
theAudio.delegate =自我;
theAudio.numberOfLoops = -1;
[theAudio玩]
[NSUserDefaults的standardUserDefaults]的setObject:@ - forKey:@音乐]; }我可能会在这里错过了一些位。只要运行一些测试,使其你喜欢的工作方式。我只是想在我的应用程序,它为我工作。
Possible Duplicate:
iOS SDK : playing music on the background and switching viewsHi guys im currently using this code to play background music in my app when its launched .But when i switch views to my SecondView and return back to my Firstview the music starts over and overlaps the music that is currently playing and it keeps overlapping music when i return back to my Firstview, how can i have the music start over when i switch back to my Firstview from my SecondView.
FirstViewController.m
- (void)viewDidLoad { NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; theAudio.delegate = self; theAudio.numberOfLoops = -1; [theAudio play];
解决方案You can use NSUserDefaults to save the music playing status, so when the view is loaded, the music will only be played if it hasn't already.
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; theAudio.delegate = self; theAudio.numberOfLoops = -1; [theAudio play]; [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
You need to reset this key back to nil when quitting the app. Otherwise, if the player quit the app, and next time when the app is restarted the @"music" string wouldn't be nil.
Go to the projectAppDelegate.m file:
- (void)applicationDidEnterBackground:(UIApplication *)application { [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; }
If the music is being played throughout the app, then put the music playing codes as shown below to be safe. Don't forget to declare the AVAudioPlayer *theAudio in projectAppDelegate.h file.
- (void)applicationDidBecomeActive:(UIApplication *)application { NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; theAudio.delegate = self; theAudio.numberOfLoops = -1; [theAudio play]; [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
I might have missed a few bits here. Just run a few tests to make it the way you like to work. I just tried on my app and it worked for me.
这篇关于iOS的背景音乐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!