2017-08-04 118 views
0

音頻不在後臺使用AVAudioPlayer iOS 9在前臺播放時播放。使用AVAudioPlayer在後臺播放音頻iOS 9

我已經設置所需的背景模式應用程序播放音頻或數據流通過AirPlay的音頻/視頻」中的Info.plist

我還檢查音頻,背景能力

使用的代碼:

@property (nonatomic, strong) AVAudioPlayer *ringtonePlayer; 


    NSString *ringtonePath = [[NSBundle mainBundle] pathForResource:@"incoming" ofType:@"wav"]; 
    if ([ringtonePath length] <= 0) { 
     NSLog(@"Can't find incoming sound file"); 
     return; 
    } 

    self.ringtonePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:ringtonePath] error:nil]; 
    self.ringtonePlayer.delegate = self; 
    self.ringtonePlayer.numberOfLoops = -1; 


    NSError *error = nil; 
    if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
               error:&error]) { 
     NSLog(@"Unable to reroute audio: %@", [error localizedDescription]); 
    } else { 
     [[AVAudioSession sharedInstance] setActive:YES error:&error]; 
    } 

    self.ringtonePlayer.volume = 1.0f; 
    [self.ringtonePlayer play]; 

請幫助,提前致謝。

回答

0

我使用下面的代碼在後臺打開,並且不需要修改info.plist文件。

func setupPlayBackground(){ 

     do { 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 

     } catch let error { 

      print(error.localizedDescription) 

     } 

    }