从UIPicker更改音频文件

| 我设置了“ 0”并运行良好。当用户选择一行时,我希望一个音频文件开始,但是如果用户在第一个音频文件停止播放之前选择了另一行,我希望它停止第一个音频文件,然后播放新的音频文件。 我现在可以使用下面的代码来实现,但是两个文件同时播放,而我找不到停止第一个文件的方法。 我要走错路了吗? (我是SDK编码的新手) 任何想法,将不胜感激 谢谢
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{
    return [list count];
}


-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [list objectAtIndex:row];


}

-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *string = [NSString stringWithFormat:@\"%@\", [list2 objectAtIndex:row]];
    pickLabel.text = string;


    if(row == 1){



        NSString *path = [[NSBundle mainBundle]

                          pathForResource:@\"del08sample\"ofType:@\"mp3\"];

        AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]

                                   initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

        [theAudio play];


    }

    else if (row == 2){



        NSString *path = [[NSBundle mainBundle]

                          pathForResource:@\"icgi03sample\"ofType:@\"mp3\"];

        AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]

                                   initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];


        [theAudio play];


    }

}
    
已邀请:
        我尚未使用您的代码进行测试,但是当我需要执行此操作时,我添加了一个简单的检查以查看 如果已经有一个AVAudioPlayer正在播放,请先停止播放,然后再重新分配。 Apple的AVAudioPlayer类参考也包含了大量有用的信息。
// stop sound if already playing

if (audioPlayer && audioPlayer.isPlaying) {

    [audioPlayer stop];

}
    

要回复问题请先登录注册