iOS: мой звуковой файл не может воспроизводиться в PickerViewController - PullRequest
0 голосов
/ 03 января 2012

У меня есть один музыкальный проигрыватель с несколькими звуковыми приложениями, использующими контроллер 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 Ответ

0 голосов
/ 03 января 2012
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component

попробуйте установить pickerview.delegate = self;

будет вызван автоматически, как только вы установите его делегата для представления выбора, вам не нужно явно нажимать кнопку> playSelected music.

...