iOS:如何避免PickerViewController中重叠的声音/音乐

时间:2012-01-04 11:58:21

标签: iphone ios xcode ios4

我有一个使用选择器视图的多个声音的音乐播放器。我的问题是,当我点击下一首音乐时,前一首音乐将与我选择的音乐重叠。当我滚动拾音器视图选择一个新对象时,它将播放一个新的音乐/声音,但前一个对象将重叠目前的选择。我想停止以前的音乐,以免它重叠。这是代码。

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 个答案:

答案 0 :(得分:1)

在启动另一个声音之前,只需停止当前正在播放的声音,AVAudioPlayer就是一个ivar,这样你就可以做到。添加

[theAudio stop];