首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS :我的声音文件无法在PickerViewController中播放

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

Stack Overflow用户
提问于 2012-01-03 18:01:42
回答 1查看 466关注 0票数 0

我有一个带有多个声音应用程序的音乐播放器,使用的是pickerView控制器,但当我尝试构建该应用程序时,声音不出来。有人能帮我排查代码故障吗?以下是我的故障排除代码。

.H文件

代码语言:javascript
复制
#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文件

代码语言:javascript
复制
     - (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 ?

}
EN

回答 1

Stack Overflow用户

发布于 2012-01-03 18:56:20

代码语言:javascript
复制
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component

尝试设置pickerview.delegate = self;

一旦你为pickerview设置了它的代理,我希望你不需要显式地使用>playSelected音乐按钮,它就会被自动调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8710498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档