关于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