您的位置:首页 > 大数据 > 人工智能

AVAudioPlayer 的 Error Domain=NSOSStatusErrorDomain Code=-10875 "未能完成操作。(“OSStatus”错误 -10875。)"

2015-04-08 16:54 405 查看
今天在做一个项目,因为要播放一个音频,就选择用 AVAudioPlayer来来做,因为以前用过,也没有注意太多后来就直接写,但是写好之后运行看效果,怎么也播放不出来,本来是因为本地音频文件有问题,后来重新找一个文件然后在播放,还是出现同样的问题.

用这段代码,以前是正常的,现在就不可以了,(网上还有好多开发人员用该方式是没有问题)

- (void)playRuningDistance:(TrainingPlayAudioType)playType{
      NSString *playFileName = [NSString
stringWithFormat:@"distance%d",playType];

    NSString *filepath = [[NSBundle
mainBundle] pathForResource:playFileName
ofType:@"mp3"];
    BOOL fileexit = [[NSFileManager
defaultManager] fileExistsAtPath:filepath];
    NSLog(@"本地问价 %d",fileexit);
    if (fileexit) {
        if (self.playerMusic && [self.playerMusic
isPlaying]) {
            return;
        }
        NSError *error;
        self.playerMusic =
nil;
        self.playerMusic =
[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
        [self.playerMusic
prepareToPlay];
        [self.playerMusic
play];
        
        NSLog(@"error loading %@",error);
    }
}

后来打印 error : Error Domain=NSOSStatusErrorDomain Code=-10875 "未能完成操作。(“OSStatus”错误 -10875。)"

后再在往常差有的说,需要加入以下代码:

    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive:YES error:nil];
如果不添加会没有声音.但是加入了还是解决不了问题,

后来看官方文档,说建议用   [[AVAudioPlayer alloc] initWithData:data error:&error];
替代[[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:&error];

- (void)playRuningDistance:(TrainingPlayAudioType)playType{
     NSString *playFileName = [NSString
stringWithFormat:@"distance%d",playType];

    NSString *filepath = [[NSBundle
mainBundle] pathForResource:playFileName
ofType:@"mp3"];
    BOOL fileexit = [[NSFileManager
defaultManager] fileExistsAtPath:filepath];
    NSLog(@"本地问价 %d",fileexit);
    if (fileexit) {
        if (self.playerMusic && [self.playerMusic
isPlaying]) {
            return;
        }
        NSError *error;
        self.playerMusic =
nil;
        NSData *data = [[NSFileManager
defaultManager] contentsAtPath:filepath];
        self.playerMusic = [[AVAudioPlayer
alloc] initWithData:data
error:&error];
        [self.playerMusic
prepareToPlay];
        [self.playerMusic
play];
        
        NSLog(@"error loading %@",error);
    }
}

结果使用问题解决,希望能帮助你们,自己也留作问题解决.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  objective-c ios iphone oc error
相关文章推荐