0

我有一个使用 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 ?

}
4

1 回答 1

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

尝试设置 pickerview.delegate = self;

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

于 2012-01-03T10:56:19.590 回答