码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-08 18:08:37      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:objective-c   ios   iphone   oc   error   

今天在做一个项目,因为要播放一个音频,就选择用 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 allocinitWithData: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);

    }

}


结果使用问题解决,希望能帮助你们,自己也留作问题解决.


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

标签:objective-c   ios   iphone   oc   error   

原文地址:http://blog.csdn.net/capacity_bo/article/details/44943005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!