我希望我的应用程序音频在后台运行,我跟着this tutorial但它没有用 . 当我按下主页按钮时我的应用程序音频仍然停止,我意识到它没有调用"applicationDidBecomeActive"或"applicationDidEnterBackground"(即使我已经禁用了设置"Application does not run in background",问题仍然存在) . 过去一周我一直在处理这个问题 .
到目前为止我已经完成了这个步骤:
- 添加了AVFoundation框架并声明
#import <AVFoundation/AVFoundation.h>
- 在音频中设置AVAudioSession
NSString *audioName = [NSString stringWithFormat:@"audio%d", (nimages)];
NSString *soundPath =[[NSBundle mainBundle]pathForResource:audioName ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error = nil;
AVAudioPlayer *audio = nil;
audio = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error];
audio.numberOfLoops = -1;
audio.volume = 0.9;
if (error) {
NSLog(@"%@", [error localizedDescription]);
NSLog(@"Unable to load file");
}else {
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
//Load the audio into memory
[audio prepareToPlay];
}
- 在plist中添加行
Updated:
My app is an audio app which the user can play specific soundtrack of my app even though it enter the background. Is it possible?
3 回答
我认为必须积极播放音频才能实现这一目标 . 如果您的应用程序“实际上不是音频应用程序”,您可以尝试将应用程序设置为VoIP;这不需要主动连接 . 它也不会阻止其他想要使用音频的应用程序 . 只需设置后台模式为“App提供IP语音服务”,然后它将在后台运行一次最多十分钟;你可以连接一个VoIP套接字并将内容推送到它,如果你想让它保持活动或唤醒它 .
我建议你阅读"Apple Human Interface" by clicking here
在允许应用程序在后台工作的情况有限,例如(音频,下载,更新等),您可以在我提供给您的链接中找到所有内容 .
使用它,它将运行其正在运行的代码 .
NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@“%@ / notification.wav”,[[NSBundle mainBundle] resourcePath]]];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];