簡體   English   中英

iOS:我的聲音文件無法在PickerViewController中播放

[英]iOS : My sound file can't play in PickerViewController

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

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

嘗試設置pickerview.delegate = self;

一旦您將其委托人設置為pickerview,它將自動被調用,我不需要顯式的> playSelected音樂按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM