2011-12-11 91 views
4

我試圖在我的應用程序中播放音樂。音樂工作正常,但切換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

回答

4

確保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

謝謝,我的問題是切換按鈕,他們無法正常工作 –

1

首先,viewDidLoad可能是不被初始化你AVAudioPlayer,因爲系統可能需要卸載一些的好地方,您的組件來回收內存(在viewDidUnload)。您應該在init方法或類似方法中創建AVAudioPlayer

所以發生了什麼事情時,你最終創建第二個AVAudioPlayer當焦點回到你的應用程序,你實際上失去了你的第一個參考。 (因此,也內存泄漏

如果你想開始播放音樂時的視圖加載後,你應該還這樣做之前檢查其狀態:

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

我把我的代碼與筆尖的名字初始化它會起到一個建議......但是音樂不能播放!有什麼問題? –

+0

你將不得不發佈更多的代碼,以及任何調試企業的成果。除非您向我們提供其他信息,否則我們無法從這裏猜出您的代碼出了什麼問題。 –

+0

編輯我的問題和代碼,請查看再次感謝你 –

5

如果你有音頻播放上多個地方那麼一個簡單的方法來實現這一目標是在財產委託聲明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。 –

1

它好像你正在開發的遊戲(從你的函數名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分鐘最多!

1

初始化您的應用程序委託文件的播放和播放背景音樂。只有在設置應用程序委託一個變量,它會檢查播放音樂真的還是假的......,享受你的應用程序而無需重新啓動音樂......

在你的代碼初始化你查看球員做負載和init()方法..這就是爲什麼你的音樂重新啓動.....只是在應用程序代理applicationdidfinshLaunching()方法中只調用一次...