0

사용자가 잠금 화면에서 오디오를 재생할 수 있도록 새로운 ios8 알림 작업을 사용하려고합니다.ios8에서 알림 작업으로 오디오를 재생하는 방법은 무엇입니까?

데이터를 백그라운드에서 다운로드 할 수 있지만 avaudio 플레이어가 재생되지 않는 것 같습니다.

도움이 될 것입니다. 처음에는 와서, 그 배경과 사운드 재생 옵션으로 뭔가를해야합니다 ...

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString  *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler 
{ 
//handle the actions 
if ([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){ 
    Message *newMessage = [Message rawMessageToInstance:[userInfo valueForKey:@"message"]]; 
    AVAudioSession* session = [AVAudioSession sharedInstance]; 
    BOOL success; NSError* error; 
    success = [session setCategory:AVAudioSessionCategoryPlayback 
          error:&error]; 
    if (!success) 
     NSLog(@"AVAudioSession error setting category:%@",error); 
    [session setActive:YES error:nil]; 
    NSLog(@"%lu",newMessage.identifier); 
    [ApiUtils downloadAudioFileAtURL:[newMessage getMessageURL] success:^void(NSData *data) { 

     AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil]; 
     [player setVolume:2]; 
     [player play]; 
     [self performSelector:@selector(completeNotif:) withObject:completionHandler afterDelay:[player duration]]; 
    } failure:^(){ 
     completionHandler(); 
    }]; 
} 
} 

답변

0

저도 같은 문제가 있었다 - 그래서 나는에 배경 오디오 플래그를 추가 : 여기

코드입니다 info.plist.

표준 AVAudio에서는 시뮬레이터에서 작동하지만 기기에서는 작동하지 않습니다. 특별한 훅은 백그라운드에서 오디오를 시작하려는 것입니다. 따라서 AVAudioPlayer로 시작하기 전에 분노의 코드 (How to use kAudioSessionProperty_OverrideCategoryMixWithOthers)를 추가하면 장치에서도 작동합니다.

호프가 도움이 되었으면 좋겠다.

관련 문제