iOS:当用户选择新的选择器时,如何自动停止 PickerViewController 上的声音文件?
我有一台具有多种声音的音乐播放器。我的问题是,当我在选择器视图中选择一个对象时,它会自动播放,但是当我选择一个新对象时,旧的声音不会停止。声音将与新声音重叠。我如何自动停止它?这是我的故障排除代码:
H 文件:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface musixGreetingViewController : 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
{
//[self playSelectedMusic:self];
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];
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];
NSString *resultString = [[NSString alloc] initWithFormat:
@"m2",
[musicList objectAtIndex:row]];
musicTitle.text = resultString;
}
-(IBAction)playSelectedMusic:(id)sender{
I want the music play here, how to do so without autoplay the music in the picker view?
}
I have one music player with multiple sounds. My question is when i pick one of the object in the picker view, it will play automatically, but when i select a new one, the old sound won't stop. The sound will overlap with the new one. How do I autostop it? Here is my code for troubleshoot :
H File :
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface musixGreetingViewController : 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 File :
- (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
{
//[self playSelectedMusic:self];
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];
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];
NSString *resultString = [[NSString alloc] initWithFormat:
@"m2",
[musicList objectAtIndex:row]];
musicTitle.text = resultString;
}
-(IBAction)playSelectedMusic:(id)sender{
I want the music play here, how to do so without autoplay the music in the picker view?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将播放器重置为开头:
This will reset the player to the beginning: