从 UIPicker 更改音频文件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有用您的代码对此进行测试,但是当我需要这样做时,我添加一个简单的检查来查看
如果已经有一个 AVAudioPlayer 正在播放,请在重新分配之前停止它。 Apple 的 AVAudioPlayer 类参考 有一个也有很多有用的信息。
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.