iOS SDK:在背景上播放音乐并切换视图

问题描述:

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

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

    } 

} 
+0

我有,尝试从您的appdelegate文件播放音乐,即使你切换视图 – Leena

确保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!

+0

谢谢,我的问题是切换按钮,他们无法正常工作 –

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

所以发生了什么事情时,你最终创建第二个AVAudioPlayer当焦点回到你的应用程序,你实际上失去了你的第一个参考。 (因此,也内存泄漏

如果你想开始播放音乐时的视图加载后,你应该还这样做之前检查其状态:

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

我把我的代码与笔尖的名字初始化它会起到一个建议......但是音乐不能播放!有什么问题? –

+0

你将不得不发布更多的代码,以及任何调试企业的成果。除非您向我们提供其他信息,否则我们无法从这里猜出您的代码出了什么问题。 –

+0

编辑我的问题和代码,请查看再次感谢你 –

如果你有音频播放上多个地方那么一个简单的方法来实现这一目标是在财产委托声明AVAudioPlayer实例synthesized.If你要玩这个随时随地做到这一点: -

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分配。

+0

+1。如果你打算使用AVAudioPlayer,这是要走的路。应该只在应用程序中出现一次的东西应该是单身人士,而且你已经有了一个完美的东西:你的AppDelegate。 –

它好像你正在开发的游戏(从你的函数名viewLoad(ScoreView后);))

嘛,据我,AvAudioplayer如果你想与游戏实现不是好的选择。

使用cocosdenshion,下载最新的cocos2d文件夹,找到这些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]; 

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

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

初始化您的应用程序委托文件的播放和播放背景音乐。只有在设置应用程序委托一个变量,它会检查播放音乐真的还是假的......,享受你的应用程序而无需重新启动音乐......

在你的代码初始化你查看球员做负载和init()方法..这就是为什么你的音乐重新启动.....只是在应用程序代理applicationdidfinshLaunching()方法中只调用一次...