2012-01-04 111 views
0

我有一個使用選取器視圖的多個聲音的音樂播放器。我的問題是當我點擊下一首音樂時,前一首將與我選擇的音樂重疊。當我滾動pickerview選擇一個新的對象時,它會播放一個新的音樂/聲音,但之前的對象會重疊目前的選擇。我想停止以前的音樂,以免重疊。這是代碼。iOS:如何避免在PickerViewController中重疊聲音/音樂

.h文件:

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVAudioPlayer.h> 

@interface PickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, AVAudioPlayerDelegate>{ 

    UIPickerView  *picker; 
    UILabel   *musicTitle; 
    NSMutableArray  *musicList; 
    AVAudioPlayer  *audioPlayer; 


} 


@property (nonatomic, retain) IBOutlet UIPickerView *picker; 
@property (nonatomic, retain) IBOutlet UILabel *musicTitle; 
@property (nonatomic, retain) NSMutableArray *musicList; 

-(IBAction)playSelectedMusic:(id)sender; 


@end 

M檔:

 - (void)viewDidLoad 
     { 
      [super viewDidLoad]; 
      musicList = [[NSMutableArray alloc] initWithObjects:@"m1",@"m2",@"m3",@"m6",@"m4", @"m5",nil]; 
     } 

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 


    { 



     if ([[musicList objectAtIndex:row] isEqual:@"m1"]) 
     { 

      NSString *path = [[NSBundle mainBundle] pathForResource:@"m1" ofType:@"mp3"]; 
      AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

      pickerView.delegate = self; 
      [theAudio play]; 
[theAudio setCurrentTime:0.0]; (our friend from this forum have suggested this but still doens't work) 

      NSString *resultString = [[NSString alloc] initWithFormat: 
             @"m1", 
             [musicList objectAtIndex:row]]; 
      musicTitle.text = resultString; 


     } 


     if ([[musicList objectAtIndex:row] isEqual:@"m2"]) 
     { 

      NSString *path = [[NSBundle mainBundle] pathForResource:@"m2" ofType:@"mp3"]; 
      AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

      pickerView.delegate = self; 
      [theAudio play]; 
      [theAudio setCurrentTime:0.0]; (our friend from this forum have suggested this but still doens't work) 



      NSString *resultString = [[NSString alloc] initWithFormat: 
             @"m2", 
             [musicList objectAtIndex:row]]; 
      musicTitle.text = resultString; 


     } 

典修正案:

NSString *path = [[NSBundle mainBundle] pathForResource:@"you" ofType:@"mp3"]; 
     NSURL *file = [[NSURL alloc] initFileURLWithPath:path]; 

     AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:file error:NULL]; 

     [file release]; 

     self.audioPlayer = theAudio; 
     [theAudio release]; 

     [audioPlayer prepareToPlay]; 
     [audioPlayer setDelegate:self]; 
     [audioPlayer setNumberOfLoops:0]; 
     [audioPlayer stop]; 

我愚蠢的錯誤:

@property (nonatomic, retain) AVAudioPlayer *audioPlayer; 

回答

1

在開始播放另一首歌曲之前,只需停止當前正在播放的聲音,您的AVAudioPlayer就是一個伊娃,所以您可以做到這一點。 Add

[theAudio stop]; 
+0

我曾試過[theAudio stop];放入if語句但仍然無效。我如何構造它? – Amink 2012-01-04 12:35:54

+0

你有沒有設置玩家的代表?這可能會幫助你http://stackoverflow.com/questions/2586284/play-multiple-audio-files-using-avaudioplayer – ferostar 2012-01-04 12:43:54

+0

這是從鏈接很大的幫助。我需要你的青睞,因爲我相當新手。它幾乎完成。但我已經完成了這個self.theAudio = newAudio;它表示在類型的對象上找不到屬性(theAudio)。我如何聲明音頻?我delcare這種方式AVAudioPlayer * theAudio;否則 – Amink 2012-01-04 14:01:09