2012-03-28 94 views
1

可能重复:目前
iOS SDK : playing music on the background and switching viewsiOS的背景音乐问题

嗨,大家好即时通讯使用此代码在我的应用程序来播放背景音乐时,它的推出。但当我切换视图我SecondView并返回到我的Firstview,音乐重新开始并覆盖当前正在播放的音乐,并且当我返回到我的Firstview时它会保持重叠的音乐,当我从我的第二个视图切换回我的Firstview时,如何让音乐重新开始。

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]; 
+0

'theAudio'是什么? – 2012-07-10 11:29:47

回答

0

您重新分配并重新创建每次显示视图时的AVAudioPlayer。如果可能,你应该避免这种情况作为您的问题的解决方案,取决于您的代码,请致电viewDidDisappearviewDidUnload事件中的[theAudio stop]。这会在视频进入后台时停止音频播放器。

+0

是的,你可以在viewDidDisappear或viewWillDisappear中调用[theAudio stop]。但不是在viewDidUnload,因为它只有当任何内存警告提出时调用,否则... – 2012-03-28 08:40:19

+0

以及我需要背景音乐跨所有视图播放,并需要音乐重新启动时,我打我的首页按钮回到我firstview。我尝试了ViewDidDisappear下的[音频停止],但它在切换视图时停止音乐,我不需要该选项。 – 2012-03-29 22:29:36

1

两个选项,

1-当第一个视图消失时停止播放器。

[theAudio stop] in `viewWillDisappear` 

2-如果你不想再回来玩。 在appDelegate类中声明theAudio。并在分配之前检查它是否被分配。

if(!appDelegate.theAudio) 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    appDelegate.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
} 
else 
{ 

[theAudiostop]; 
    [theAudio play]; 
} 
+0

以及我需要背景音乐播放所有视图,并需要音乐重新启动时,我打我的首页按钮回到我的firstview。目前我加入[theAudio prepareToPlay];到 - (void)viewWillDisappear:(BOOL)animated {..当我点击主页按钮回到我的FirstView音乐继续播放,它工作正常,但我想要背景音乐重新启动时,我打我的主页按钮回到我的FirstView。 – 2012-03-29 22:37:13

3

您可以使用NSUserDefaults的保存音乐播放状态,这样的观点在加载时,音乐将只如果有尚未发挥。

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"]; } 

当退出应用程序时,您需要将此键重置为零。否则,如果玩家退出应用程序,并且下次重新启动应用程序时,@“music”字符串不会为零。

转到projectAppDelegate.m文件:

- (void)applicationDidEnterBackground:(UIApplication *)application { 
     [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; } 

如果音乐被整个应用播放,然后把音乐,如下图所示是安全的打码。不要忘记在projectAppDelegate.h文件中声明AVAudioPlayer * theAudio。

- (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"]; } 

我可能在这里错过了几位。只需运行一些测试,使其成为您喜欢的工作方式。我只是尝试了我的应用程序,它为我工作。