2012-02-18 192 views
0

我試圖做一個簡單的基於AVAudioPlayer的應用程序來播放按下按鈕時播放2個不同的音樂,有2個視圖,第一個是主視圖包含2個按鈕,每個按鈕設置一首歌,命名爲整數1.MP3,2.MP3 .....等)和here'e代碼播放第二個聲音

#import "podHome.h" 
#import "podMusic.h" 
#import "ArabAppDelegate.h" 


@implementation podHome 

@synthesize song1; 
@synthesize tabi; 
int CurrentPlay; 
NSString *Currenttxt; 


-(IBAction)uae{ 
    CurrentPlay=1; 
    [email protected]"uae"; 
    podMusic *newContro=[[podMusic alloc] init]; 
    [newContro setCurrentPlay1:CurrentPlay setCurrentText:Currenttxt]; 

    ArabAppDelegate *theDelegate = (ArabAppDelegate*)[[UIApplication sharedApplication] delegate]; 
    tabi = theDelegate.tabcontrolPod; 
    tabi.selectedIndex = 1; 
    [newContro release]; 
} 

-(IBAction)libya{ 
    CurrentPlay=2; 
    [email protected]"uae"; 
    podMusic *newContro=[[podMusic alloc] init]; 
    [newContro setCurrentPlay1:CurrentPlay setCurrentText:Currenttxt]; 

    ArabAppDelegate *theDelegate = (ArabAppDelegate*)[[UIApplication sharedApplication] delegate]; 
    tabi = theDelegate.tabcontrolPod; 
    tabi.selectedIndex = 1; 
    [newContro release]; 
} 

這些絲束(IBActions)被鏈接到兩個按鈕上它們中的一個按壓時,它會改變到另一查看並開始播放歌曲

- (void)viewWillAppear:(BOOL)animated { 

    if((played == 1)&&(isBacked==FALSE)){ 

    NSString *filePath = [[NSBundle mainBundle] pathForResource:texting 
                 ofType:@"txt"]; 

    NSString *filenameString = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:nil]; 
    CurrentTex.text = filenameString; 
    AudioSessionInitialize(NULL, NULL, NULL, NULL); 
     UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; 
     AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory); 
     AudioSessionSetActive(YES); 

     playBtnBG = [[UIImage imageNamed:@"play-pod.png"] retain]; 
     pauseBtnBG = [[UIImage imageNamed:@"pause-pod.png"] retain]; 
     [playButton setBackgroundImage:pauseBtnBG forState:UIControlStateNormal]; 

     [self registerForBackgroundNotifications]; 

     updateTimer = nil; 

     duration.adjustsFontSizeToFitWidth = YES; 
     currentTime.adjustsFontSizeToFitWidth = YES; 
     progressBar.minimumValue = 0.0; 
     NSString *path=[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",CurrentPlay] ofType:@"mp3"]; 
     self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
     [player stop]; 
     //self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];  
     if (self.player) 
     { 
      [self updateViewForPlayerInfo:player]; 
      [self updateViewForPlayerState:player]; 
      player.numberOfLoops = 0; 
      player.delegate = self; 
     } 
     [self startPlaybackForPlayer:player]; 
     fileName.text= [[NSString alloc]initWithFormat:@"%@", songName]; 
     // [fileURL release]; 
//  CurrentPlay = 0; 
       isBacked = TRUE; 
    } 

     [super viewWillAppear:animated]; 
} 

- (void)setCurrentPlay1:(int)varP setCurrentText:(NSString *)varT{ 

    CurrentPlay = varP; 
    texting = varT; 
    played = 1; 
    isBacked = FALSE; 
} 

但問題是,當歌曲正在播放和回到家中查看nd按下另一首歌曲的按鈕,同時開始播放第一首歌曲,我認爲第一首應該停止開始另一首歌曲,我應該如何釋放才能做到這一點?

+0

那裏有趣的功能名稱.... :) – esqew 2012-02-18 00:31:36

回答

0

我的猜測是你有兩個AVAudioPlayer實例,當你設置他們都玩,他們都做!

解決辦法之一就是告訴其他玩家在激活新玩家時停下來,但隨着玩家人數的增加,這會很快變得麻煩。

取而代之的是,您最好只設置一個播放器,並根據按下的按鈕更改它的歌曲。這樣,兩個音樂曲目就不會同時播放。

0

在你的第一個按鈕動作寫這樣的。

if(player2.isPlaying) 
{ 
[player2 stop]; 
} 

以同樣的方式在這樣的第二個按鈕動作中做同樣的事情。

if(player1.isPlaying) 
{ 
[player1 stop]; 
}