如何在iPhone的后台播放音频?

编程入门 行业动态 更新时间:2024-10-05 21:20:07
本文介绍了如何在iPhone的后台播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在设置的特定时间播放音频文件.为此,我在info.plist的"UIBackgroundModes"中添加了音频,在didEnterBackground中的backgroundtaskidentifier中添加了音频.它可以在模拟器中工作,但不能在设备中工作.解决这个问题.

I'm trying to play audio file at particular time which i have set.for that I have added audio in "UIBackgroundModes" in info.plist and backgroundtaskidentifier in didEnterBackground.Its working in simulator but not in device.Can anyone help to solve this.

预先感谢...

推荐答案

您可以使用以下方法查看此参考应用音频流和背景.

You can check out this reference app with streaming and backgrounding of audio.

带有声音的本地通知示例. 更多详细信息.. >

Example of a local notification w/ attached sound. More detail here.

- (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"Application entered background state."); // bgTask is instance variable NSAssert(self->bgTask == UIInvalidBackgroundTask, nil); bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{ dispatch_async(dispatch_get_main_queue(), ^{ [application endBackgroundTask:self->bgTask]; self->bgTask = UIInvalidBackgroundTask; }); }]; dispatch_async(dispatch_get_main_queue(), ^{ while ([application backgroundTimeRemaining] > 1.0) { NSString *friend = [self checkForIncomingChat]; if (friend) { UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif) { localNotif.alertBody = [NSString stringWithFormat: NSLocalizedString(@"%@ has a message for you.", nil), friend]; localNotif.alertAction = NSLocalizedString(@"Read Message", nil); localNotif.soundName = @"alarmsound.caf"; localNotif.applicationIconBadgeNumber = 1; [application presentLocalNotificationNow:localNotif]; [localNotif release]; friend = nil; break; } } } [application endBackgroundTask:self->bgTask]; self->bgTask = UIInvalidBackgroundTask; }); }

更多推荐

如何在iPhone的后台播放音频?

本文发布于:2023-11-28 03:16:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   音频   如何在   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!