2012-01-03 61 views
0

我有一個使用pickerView控制器的多聲音應用程序的音樂播放器,但是當我嘗試構建應用程序時,聲音不出來。任何人都可以幫我解決我的代碼問題?這是我的故障排除代碼。iOS:我的聲音文件無法在PickerViewController中播放

.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 ? 

} 

回答

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

嘗試設置pickerview.delegate =自我;

將自動調用,一旦你設置它的代表pickerview你不需要> playSelected音樂按鈕明確我希望。

+0

我嘗試設置pickerview.delegate = self;並且在構建時收到此錯誤。找不到函數指針CreateAudio – Amink 2012-01-03 11:45:39

+0

我嘗試設置pickerview.delegate = self;並且在構建時收到此錯誤。在我的控制檯它說:無法找到CFBundle/CFPlugIn工廠的函數指針CreateAudio(捆綁,未加載) – Amink 2012-01-03 12:01:20

+0

請親切看到此鏈接https ://discussions.apple.com/thread/2220669?start = 0&tstart = 0 – raghul 2012-01-03 12:10:24