0

我正在嘗試使用新的ios8通知操作來讓用戶從其鎖定屏幕播放音頻。如何在ios8的通知動作中播放音頻?

我設法在後臺下載數據,但avaudioplayer似乎沒有玩。

任何幫助將不勝感激!

下面是代碼:

- (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 stuf,它可以在模擬器上工作,但不能在設備上工作。特別的掛鉤是,你想在後臺啓動音頻 - 因此在開始使用AVAudioPlayer之前添加來自憤怒的代碼:How to use kAudioSessionProperty_OverrideCategoryMixWithOthers,它也可以在設備上運行。

希望能對我有所幫助,對我而言有效。