【问题标题】:How to play audio in notification action in ios8?如何在 ios8 的通知操作中播放音频?
【发布时间】:2014-12-23 04:40:22
【问题描述】:

我正在尝试使用新的 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();
    }];
}
}

【问题讨论】:

    标签: objective-c push-notification apple-push-notifications ios8


    【解决方案1】:

    我也遇到了同样的问题...首先,它必须对背景和声音播放选项做一些事情 - 所以我将背景音频标志添加到 info.plist。

    使用标准 AVAudio stuf,它可以在模拟器上运行,但不能在设备上运行。特殊的钩子是,您想在后台启动音频 - 所以在使用 AVAudioPlayer 之前添加 anger 中的代码:How to use kAudioSessionProperty_OverrideCategoryMixWithOthers,它也可以在设备上运行。

    希望对我有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 2021-02-18
      相关资源
      最近更新 更多