这种情况会在不同设备上不时发生。相同的iphone 6s具有相同的ios,并且在一个设备中它工作正常,但另一个没有
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive: YES error: nil];
NSError *error;
NSData *data = [DATA_MANAGER tmpPathToDecryptedAudio:unit];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error: &error];
self.player = player;
[self.player play];
答案 0 :(得分:0)
可能是您缺少对 prepareToPlay 的调用。你可以试试这个
NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:FILE_PATH]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
if(error) {
NSLog(@"Audio Player error : %@",error.localizedDescription);
return;
}
audioPlayer.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];