iOS:当用户选择新的选择器时,如何自动停止 PickerViewController 上的声音文件?

发布于 2024-12-24 18:09:09 字数 2334 浏览 1 评论 0原文

我有一台具有多种声音的音乐播放器。我的问题是,当我在选择器视图中选择一个对象时,它会自动播放,但是当我选择一个新对象时,旧的声音不会停止。声音将与新声音重叠。我如何自动停止它?这是我的故障排除代码:

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 技术交流群。

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

发布评论

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

评论(1

仄言 2024-12-31 18:09:09

这会将播放器重置为开头:

[theAudio setCurrentTime:0.0];

This will reset the player to the beginning:

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