【问题标题】:iOS : How to avoid overlapping sound/music in PickerViewControlleriOS:如何避免 PickerViewController 中重叠的声音/音乐
【发布时间】:2012-01-04 11:58:21
【问题描述】:

我有一个使用选取器视图的具有多种声音的音乐播放器。我的问题是,当我单击下一首音乐时,前一首音乐将与我选择的音乐重叠。意思是当我滚动选择器视图以选择新对象时,它将播放新音乐/声音,但前一个对象将重叠当前选择。我想停止以前的音乐,这样它就不会重叠。这是代码。

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;

【问题讨论】:

    标签: iphone ios xcode ios4


    【解决方案1】:

    在开始另一个声音之前停止当前播放的声音,你的 AVAudioPlayer 是一个 ivar,所以你可以做到。添加

    [theAudio stop];
    

    【讨论】:

    • 我试过 [theAudio stop];放入 if 语句但仍然不起作用。我如何构建它?
    • 你设置播放器的代理了吗?这可能会帮助你stackoverflow.com/questions/2586284/…
    • 这对链接很有帮助。我需要你的帮助,因为我是新手。它几乎完成了。但我已经完成了这个 self.theAudio = newAudio;它说在类型的对象上找不到属性(theAudio)。我如何声明音频?我喜欢这种方式 AVAudioPlayer *theAudio;否则
    • 没问题!将 AVAudioPlayer *theAudio 更改为 self.theAudio。您当前正在创建另一个变量 local,它“隐藏”您的 ivar。我第一次注意到它,但忘了告诉你。
    • 我在上面做了一些代码修改。而且我不太明白 Change AVAudioPlayer *theAudio for self.theAudio 是什么意思
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多