iOS:我的声音文件无法在 PickerViewController 中播放

Posted

技术标签:

【中文标题】iOS:我的声音文件无法在 PickerViewController 中播放【英文标题】:iOS : My sound file can't play in PickerViewController 【发布时间】:2012-01-03 10:01:42 【问题描述】:

我有一个带有多个声音应用程序的音乐播放器,它使用 pickerView 控制器,但是当我尝试构建应用程序时,声音没有发出。任何人都可以帮我解决我的代码问题吗?这是我的故障排除代码。

.H 文件

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController : 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:@"music1",@"music2",@"music3",@"music4",@"music5",
                        @"music6",nil];
        

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
          inComponent:(NSInteger)component
    



        if ([[musicList objectAtIndex:row] isEqual:@"music1"])
        


            NSURL *path = [[NSBundle mainBundle] URLForResource:@"music1" withExtension:@"mp3"];
            AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:NULL];


            theAudio.delegate = self;
            [theAudio play];

            NSString *resultString = [[NSString alloc] initWithFormat:
                                      @"music1",
                                      [musicList objectAtIndex:row]];
            musicTitle.text = resultString;


        

-(IBAction)playSelectedMusic:(id)sender

    how do i call the didSelectRow & put it here ?


【问题讨论】:

【参考方案1】:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component

尝试设置pickerview.delegate = self;

一旦你为pickerview设置了它的委托,就会自动调用你不需要>playSelected音乐按钮,我希望。

【讨论】:

我尝试设置 pickerview.delegate = self;我在构建时收到此错误。找不到函数指针 CreateAudio 我尝试设置 pickerview.delegate = self;我在构建时收到此错误。在我的控制台中它说:Cannot find function pointer CreateAudio for factory in CFBundle/CFPlugIn /Audio/Plug-Ins/HAL/usb-audio.com.plugin>(捆绑,未加载) 请查看此链接discussions.apple.com/thread/2220669?start=0&tstart=0 您好,问题现已解决。但是,我需要动作按钮。我该怎么做? -(IBAction)playSelectedMusic:(id)sender 我如何调用 didSelectRow 并把它放在这里?

以上是关于iOS:我的声音文件无法在 PickerViewController 中播放的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 无法在后台播放声音

iOS:静音模式下无法播放我的声音

Cordova Local Notification 为 iOS 和 Android 播放本地声音

AVAudioPlayer mp3 文件无法播放

iOS 7 无法播放系统声音

如何在 iOS 中列出声音文件?