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

來源:互聯網
上載者:User

標籤:objective-c   ios   iphone   oc   error   

今天在做一個項目,因為要播放一個音頻,就選擇用 AVAudioPlayer來來做,因為以前用過,也沒有注意太多後來就直接寫,但是寫好之後運行看效果,怎麼也播放不出來,本來是因為本地音頻檔案有問題,後來重新找一個檔案然後在播放,還是出現同樣的問題.


用這段代碼,以前是正常的,現在就不可以了,(網上還有好多開發人員用該方式是沒有問題)

- (void)playRuningDistance:(TrainingPlayAudioType)playType{

      NSString *playFileName = [NSStringstringWithFormat:@"distance%d",playType];

    NSString *filepath = [[NSBundlemainBundle] pathForResource:playFileNameofType:@"mp3"];

    BOOL fileexit = [[NSFileManagerdefaultManager] fileExistsAtPath:filepath];

    NSLog(@"本地問價 %d",fileexit);

    if (fileexit) {

        if (self.playerMusic && [self.playerMusicisPlaying]) {

            return;

        }

        NSError *error;

        self.playerMusic =nil;

        self.playerMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];

        [self.playerMusicprepareToPlay];

        [self.playerMusicplay];

        

        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 = [NSStringstringWithFormat:@"distance%d",playType];

    NSString *filepath = [[NSBundlemainBundle] pathForResource:playFileNameofType:@"mp3"];

    BOOL fileexit = [[NSFileManagerdefaultManager] fileExistsAtPath:filepath];

    NSLog(@"本地問價 %d",fileexit);

    if (fileexit) {

        if (self.playerMusic && [self.playerMusicisPlaying]) {

            return;

        }

        NSError *error;

        self.playerMusic =nil;

        NSData *data = [[NSFileManagerdefaultManager] contentsAtPath:filepath];

        self.playerMusic = [[AVAudioPlayeralloc] initWithData:dataerror:&error];

        [self.playerMusicprepareToPlay];

        [self.playerMusicplay];

        

        NSLog(@"error loading %@",error);

    }

}


結果使用問題解決,希望能協助你們,自己也留作問題解決.


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.