从 UIPicker 更改音频文件

发布于 2024-11-15 16:59:54 字数 1531 浏览 4 评论 0原文

我有一个 UIPickerView 设置并运行良好。当用户选择一行时,我希望启动一个音频文件,但是如果用户在第一个音频文件停止播放之前选择另一行,我希望它停止第一个音频文件,只让新的音频文件播放。 我目前可以使用下面的代码来做到这一点,但是两个文件同时播放,我找不到停止第一个文件的方法。

我是不是走错路了? (我是 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];


    }

}

I have a UIPickerView set up and running fine. When the user selects a row I want one audio file to start but then if the user selects another row before the first audio file has stopped playing I want it to stop the first and just let the new audio file play.
I can do it at present with the code below but both files play at the same time and I cannot find a way of stopping the first one.

Am I going about the wrong way? (I am new to SDK coding)

Any ideas would be appreciated
Thanks

-(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];


    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-11-22 16:59:54

我还没有用您的代码对此进行测试,但是当我需要这样做时,我添加一个简单的检查来查看
如果已经有一个 AVAudioPlayer 正在播放,请在重新分配之前停止它。 Apple 的 AVAudioPlayer 类参考 有一个也有很多有用的信息。

// stop sound if already playing

if (audioPlayer && audioPlayer.isPlaying) {

    [audioPlayer stop];

}

I haven't tested this with your code, but when I need to do this I add a simple check to see
if there is already an AVAudioPlayer that is playing and stop it before re-allocating. Apple's AVAudioPlayer Class Reference has a whole host of useful info too.

// stop sound if already playing

if (audioPlayer && audioPlayer.isPlaying) {

    [audioPlayer stop];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文