这是我点击按钮上的代码
- (void)playButton:(id)sender
{
NSLog(@"Play Button Clicked!!!...");
audioPlayer = [self setUpAudioPlayer:[songs objectAtIndex:i]];
}
-(AVAudioPlayer *)setUpAudioPlayer:(NSString *)filename
{
NSLog(@"File name : %@",filename);
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"caf"];
NSLog(@"File path = %@",path);
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
//audioPlayer.volume = 0.5;
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(@"Song is playing....");
return [audioPlayer autorelease];
}
这里的歌曲是NSMutableArray,其中包含文件名...所有事情都与NSLog正确...但没有声音来,没有错误来......代码有什么问题.. ???
答案 0 :(得分:3)
您的音频播放器可能会在开始播放后立即发布。在调用setUpAudioPlayer
后尝试保留它。
答案 1 :(得分:1)
很可能你需要初始化一个音频会话,有些像这样
- (void)setupAudioSession { AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error = nil; [session setCategory: AVAudioSessionCategoryPlayback error: &error]; if (error != nil) NSLog(@"Failed to set category on AVAudioSession"); // AudioSession and AVAudioSession calls can be used interchangeably OSStatus result = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self); if (result) NSLog(@"Could not add property listener! %d\n", result); BOOL active = [session setActive: YES error: nil]; if (!active) NSLog(@"Failed to set category on AVAudioSession"); }