iOS SDK:在后台播放音乐并切换视图

我正在尝试在我的应用程序中播放音乐。 音乐工作正常,但切换viewControllers并返回到主菜单后,我的音乐再次播放! 这意味着几个相同的声音一起玩! 我该如何解决这个问题? 这是我的代码:

- (void)viewDidLoad { NSString *music = [[NSBundle mainBundle] pathForResource:@"1music" ofType:@"mp3"]; myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL]; myMusic.delegate = self; myMusic.numberOfLoops = -1; [myMusic play]; } - (IBAction) scoreView { ScoreViewController *scoreView = [[ScoreViewController alloc] initWithNibName:@"ScoreViewController" bundle:nil]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; [self.view addSubview: scoreView.view]; [UIView commitAnimations]; } 

编辑代码:

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL]; myMusic.delegate = self; myMusic.numberOfLoops = -1; [myMusic play]; } return self; } //toggle button - (IBAction)MusicPlaying:(id)sender { if ((isPlay = !isPlay)) { UIImage *buttonImageNormal = [UIImage imageNamed:@"play.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [MusicButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; [myMusic pause]; }else { UIImage *buttonImageNormal = [UIImage imageNamed:@"pause.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [MusicButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; NSLog(@"Music play"); [myMusic play]; } } 

确保myMusic被正确声明为保留属性,然后合成,然后尝试如下所示:

 -(void) commonInit { NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; self.myMusic = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL] autorelease]; self.myMusic.delegate = self; self.myMusic.numberOfLoops = -1; //You probably don't want to play music on init, rather defer to viewDidLoad. // [self.myMusic play]; } - (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self commonInit]; } return self; } - (id)init { self = [super init]; if (self) { [self commonInit]; } return self; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { [self commonInit]; } return self; } 

然后在你的viewDidLoad中试试这个:

 - (void)viewDidLoad { ... if (!self.myMusic.playing) { [self.myMusic play]; } ... } 

另外,别忘了释放你的播放器dealloc!

如果你有audio播放多个地方,那么一个简单的方法来实现这一点是声明AVAudioPlayer实例在委托与属性合成。如果你想播放这个地方做到这一点: –

 MyAppDelegate *app_delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; if(app_delegate.audioPlayer) { [app_delegate.audioPlayer release]; app_delegate.audioPlayer = nil; } app_delegate.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 

如果你想停止这样做: –

 if(app_delegate.audioPlayer) { if([app_delegate.audioPlayer isPlaying]) { [app_delegate.audioPlayer stop]; [app_delegate.audioPlayer setCurrentTime:0]; } } 

并发挥新的声音文件释放和零如果已经分配相同的球员或再次分配新的声音文件的URL。

首先, viewDidLoad可能不是初始化AVAudioPlayer的好地方,因为系统可能需要卸载一些组件来回收内存(在viewDidUnload )。 你可能应该在你的init方法或类似的方法中创buildAVAudioPlayer

所以发生什么事是你最终在焦点回到你的应用程序时创build第二个AVAudioPlayer ,而你实际上失去了对第一个的引用。 (因此,也泄漏内存。

如果您想在视图加载时开始播放音乐,则应在此之前另外检查其状态:

 - (void)viewDidLoad { ... if (!myMusic.playing) { [myMusic play]; } ... } 

它似乎你正在开发游戏(从你的函数名称后的ViewLoad(ScoreView)。;))

那么根据我的说法,如果你想用Game来实现,AvAudioplayer并不是好select。

使用cocosdenshion,下载最新的cocos2d文件夹,find这9个文件,把它放在你的资源文件夹中,

你完成了!

 #import "SimpleAudioEngine.h" // wherever you want to call in head part of the file. 

Step1呼叫短文件。

 [[SimpleAudioEngine sharedEngine] playEffect:@"blast.mp3"]; 

step2调用后台循环。

 [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.mp3" loop:YES]; 

注:有可用function列表http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_simple_audio_engine.html ,它非常简单和容易!

整个实施将需要,最多20分钟!

在应用程序委托文件中初始化您的播放器并播放您的背景音乐。 只在应用程序委托中设置一个variables来检查音乐播放的真实与否…并享受您的应用程序而无需重新启动音乐……

在你的代码中,你初始化你的播放器在视图中加载和初始化方法…这就是为什么你的音乐重新启动…..只调用一次在应用程序委托applicationDidfinshLaunching()方法….