我正在使用 iOS5 。
我正在开发HTTPLivestream项目。我正在使用 MPMovieplayer ,我的问题是当应用程序进入后台时没有音频。我在后台模式中更改plist是audio.But没有结果请帮帮我。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
UIApplication *app = [UIApplication sharedApplication];
backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
});
}
NSString *path =[[NSString alloc] initWithString:@"http://*******/alayam/alayam/playlist.m3u8"];
audioUrl=[NSURL URLWithString:path];
appDelegate.player = [[MPMoviePlayerController alloc] initWithContentURL:audioUrl];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:appDelegate.player];
if ([appDelegate.player respondsToSelector:@selector(loadState)])
{
[appDelegate.player setControlStyle:MPMovieControlModeVolumeOnly];
[appDelegate.player setFullscreen:YES];
[appDelegate.player prepareToPlay];
}
- (void)loadStateDidChange:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:appDelegate.player];
}
先谢谢
答案 0 :(得分:2)
您需要结合ios5中的背景支持设置 -
进行遥控设置//注册您的应用程序以进行远程控制
if([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// allows you to play in the background when app is suspended in iOS5
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
将这两个设置放在应用程序didFinishLaunching delegate
中答案 1 :(得分:1)
尝试在初始化播放器之前激活音频会话,例如在app启动委托中:
- (void)applicationDidFinishLaunching:(UIApplication *)application
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
// other init stuff ...
}
PS:您需要在设备上才能使音频会话正常工作,模拟器不会削减它。
答案 2 :(得分:1)
如果您在模拟器上进行测试,那么它不能在模拟器上运行,请使用设备。如果您正在使用设备而音频不在后台播放,那么您可以将您的完整源代码发送给我khalid0491@gmail.com。我也在我的应用程序中完成了。 我的代码如下。
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
}
并在plist中我创建了一个名为&#34的数组;所需的背景模式"并在数组名称中插入一个项目" App Plays Audio"。