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,它也可以在设备上运行。

希望能对我有所帮助,对我而言有效。