關於ios進入后台界面后 播放聲音解決方案


1 最近我在做環信視頻通話時,遇到了一個新功能就是APP在后台的時候能對方能視頻或者音頻過來的時候 能夠播放聲音 根據查詢相關資料得到如下解決辦法

    NSError *error;
    AVAudioSession *audionSession = [AVAudioSession sharedInstance];
    [audionSession setCategory:AVAudioSessionCategoryPlayback error:&error];
    [audionSession setActive:YES error:&error];
    NSLog(@"%@",error);
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"callRing" ofType:@"mp3"];
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:musicPath];
    self.self.ringPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];

實例化 AVAudioPlayer時需要使用 initWithData  創建 然后加上  就能實現在后台的時候收到相關消息能夠播放聲音了

  NSError *error;
    AVAudioSession *audionSession = [AVAudioSession sharedInstance]; [audionSession setCategory:AVAudioSessionCategoryPlayback error:&error]; [audionSession setActive:YES error:&error]; 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM