AVPlayer得到错误(Domain = NSOSStatusErrorDomain Code = 1954115647)但只能从锁定屏幕,从app这个工作没有问题

时间:2017-08-15 14:23:33

标签: ios objective-c avaudioplayer

这种情况会在不同设备上不时发生。相同的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];

1 个答案:

答案 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];