如何让我的 AVPlayer 在应用程序处于后台时播放? [英] How do I get my AVPlayer to play while app is in background?

查看:24
本文介绍了如何让我的 AVPlayer 在应用程序处于后台时播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了我的功课...一直在这里阅读文档、谷歌搜索、stackoverflowing...但是当用户让应用程序进入后台时,我仍然无法让我的声音保持不变.

I've done my homework... been reading here, the docs, googling, stackoverflowing... but still no luck in making my sound stick when the user makes the app go into the background.

到目前为止我做了什么:在 plist 文件中添加了 UIBackgroundModes、音频.

What I have done so far: Added the UIBackgroundModes, audio to the plist-file.

首先是这段代码:

radioAudio = [[AVAudioSession alloc] init];
[radioAudio setCategory:AVAudioSessionCategoryPlayback error:nil];
[radioAudio setActive:YES error:nil];

然后这个:

NSString *radioURL = @"http://xxx.xxx.xxx/radio.m3u";
radioPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:radioURL]] retain];

但只要用户点击主页按钮,我的声音就会消失.

But as soon as the user hits the home-button, my sound dies.

我也找到了这个,但还没有添加,因为我读过的一些东西说不需要;

I also found this, but not added yet cuase some stuff I've read says it is not needed;

newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
     if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
        [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
bgTaskId = newTaskId;

现在我不知道我应该去哪里让我的 AVPlayer 让收音机唱歌,而用户在手机上做其他事情.我正在使用 4.2 的 iPhone 4 上对此进行测试.为 4.0 构建它.

Right now I have no idea where I should go to make my AVPlayer let the radio sing while the user does other stuff on the phone. I am testing this on an iPhone 4 with 4.2. Building it for 4.0.

有人对我应该怎么做有什么建议吗?

Anyone have any suggestions what I should do?

推荐答案

遇到了同样的问题,但是找到了解决方案..

Had the same problem, but found a solution for this..

看这里:https://devforums.apple.com/message/395049#395049

以上链接内容:

APPNAME 替换为您自己的应用名称!

Replace APPNAME with your own app name!

我在 iOS 4.2.1 上

Im on iOS 4.2.1

目前使用 iOS5 + 6 + 7 测试版

APPNAME-Info.plist中添加UIBackgroundModes,选择App播放音频

Add UIBackgroundModes in the APPNAME-Info.plist, with the selection App plays audio

然后将AudioToolBox框架添加到文件夹frameworks中.

Then add the AudioToolBox framework to the folder frameworks.

APPNAMEAppDelegate.h中添加:

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

看起来像这样:

...
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
...

APPNAMEAppDelegate.m 中添加以下内容:

In the APPNAMEAppDelegate.m add the following:

// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

/* Pick any one of them */
// 1. Overriding the output audio route
//UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
//AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

// 2. Changing the default output audio route
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

进入

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

但在这两行之前:

[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

构建你的项目,看看是否有错误,如果没有,尝试在模拟器的设备上调试,它可以在模拟器上出错.

Build your project and see if theres any error, If not, try debug on Device insted of the Simulator, it CAN bug on simulator.

希望这可以帮助其他有同样问题的人..

Hope this helps others with same problem..

这篇关于如何让我的 AVPlayer 在应用程序处于后台时播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆