2012-01-04 89 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