2012-02-18 19 views
0

ボタンを押したときに2つの異なる音楽を再生する簡単なAVAudioPlayerベースのアプリケーションを実行しようとしていますが、2つのビューがあります。最初はホームビューに2つのボタンがあり、 1.mp3、2.mp3 .....など)とhere'eコード2番目のサウンドを再生する

#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)は、そのうちの一つを押したときに、それは他に変更される2つのボタンにリンクされています表示して曲

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

の再生を開始するが、問題は、曲が演奏されたときにホームビューaに戻っだということです他の曲のボタンを押して、それは最初のものと同時に演奏し始めます、私は最初に他の曲をやめるべきだと思います。

+0

興味深い機能名があります。:) – esqew

答えて

0

私の推測では、AVAudioPlayerのインスタンスが2つあり、両方を再生するように設定しているとします。

1つの解決策は、新しいプレイヤーをアクティブにするときに他のプレイヤーに停止するように指示することですが、プレイヤーの数が増えるにつれてすぐに面倒になります。

代わりに1人のプレイヤーを設定して、どのボタンが押されたかに応じて曲を変更するほうがよいでしょう。そうすれば、2つの音楽トラックが同時に演奏されることはありません。

0

最初のボタンアクションでは、このように記述します。

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

同様に、このような2番目のボタンアクションで同じことをします。

if(player1.isPlaying) 
{ 
[player1 stop]; 
} 
関連する問題