我有一个带有多个声音应用程序的音乐播放器,使用的是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 ?
}发布于 2012-01-03 18:56:20
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component尝试设置pickerview.delegate = self;
一旦你为pickerview设置了它的代理,我希望你不需要显式地使用>playSelected音乐按钮,它就会被自动调用。
https://stackoverflow.com/questions/8710498
复制相似问题