- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Google 和 StackOverflow 上做了很多研究。我找到的所有答案都不适用于 iOS 7。我开始使用 Xcode 5 在 iOS 7 SDK 中编写新的应用程序。
我想要做的就是在应用程序中播放应用程序包中存储的文件中的音频(而不是来自音乐库)。我想要在后台播放音频并在屏幕锁定时控制(除了控制中心)。
我将 APPNAME-Info.plist
键 UIBackgroundModes
设置为音频。它不处理应用程序委托(delegate)中的事情;一切都在 ViewController 中完成
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
在实现的 viewDidAppear:
方法中,我调用了 super,然后调用了以下代码:
// Once the view has loaded then we can register to begin receiving controls and we can become the first responder
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
在我实现的 viewWillDisappear:
方法中,我有以下代码:
// End receiving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
我还实现了 canBecomeFirstResponder
方法,该方法返回 YES。接下来,我实现了 remoteControlReceivedWithEvent:
方法:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
// If it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlPlay) {
[self playPauseAudio:self];
} else if (event.subtype == UIEventSubtypeRemoteControlPause) {
[self playPauseAudio:self];
} else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
[self playPauseAudio:self];
}
}
}
让我感到困惑的是,这个完全相同的设置在 iOS 6 上运行良好。在 iOS 7 上,它不起作用。它曾经在 iOS 6 中如此简单。iOS 7 SDK 中发生了一些根本性的变化。我错过了什么?
最佳答案
我设法解决了这个问题,为了避免被另一个可怜的人拉头发,这里是:
首先确保您的 Info.plist 正确地将音频列为背景模式。
(如果您不知道我在说什么,请选择 YOURAPPNAME-Info.plist,选择它。单击加号并添加一个名为 UIBackgroundModes
的新键并展开它。添加一个名为音频
。)
您需要引用任何正在创建音频的播放对象。因为我只播放音频而 AVplayer 不遵守背景音频,所以在你的 View Controller 的标题中使用它:
@property (nonatomic, retain) MPMoviePlayerController *audioPlayer;
在实现中,执行以下操作:
[super viewDidAppear:animated];
//Once the view has loaded then we can register to begin recieving controls and we can become the first responder
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
和
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//End recieving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
添加两个方法
//Make sure we can recieve remote control events
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void) registerForAudioObjectNotifications {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver: self
selector: @selector (handlePlaybackStateChanged:)
name: MixerHostAudioObjectPlaybackStateDidChangeNotification
object: audioObject];
}
现在所有重要的代码 - 这使您的应用程序能够从“控制中心”和锁定屏幕控制音频:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playOrStop: nil];
break;
default:
break;
}
}
}
您可以在此处添加许多类型的事件类型并调用任何方法。
典型的事件是:
UIEventSubtypeRemoteControlPlay = 100, //Parent EVENT
// All below are sub events and you can catch them using switch or If /else.
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
要调试帮助,您可以使用:
MPMoviePlayerController *mp1= (MPMoviePlayerController *)[notification object];
NSLog(@"Movie State is: %d",[mp1 playbackState]);
switch ([mp1 playbackState]) {
case 0:
NSLog(@"******* video has stopped");
break;
case 1:
NSLog(@"******* video is playing after being paused or moved.");
break;
case 2:
NSLog(@"******* video is paused");
break;
case 3:
NSLog(@"******* video was interrupted");
break;
case 4:
NSLog(@"******* video is seeking forward");
break;
case 5:
NSLog(@"******* video is seeking Backwards");
break;
default:
break;
就是这样 - 希望它能帮助一些人! - 这在 iOS 7 和 iOS 6 上完美运行,带有 Storyboard 应用程序以及使用耳机和所有新控制中心进行控制。
关于objective-c - iOS 7 SDK 不遵守背景音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18982292/
根据 PEP 257命令行脚本的文档字符串应该是它的用法信息: The docstring of a script (a stand-alone program) should be usable a
我有一个使用 exit 的批处理文件命令返回退出代码。 在某些情况下,该批处理文件可以从命令行以交互方式调用,或者在其他情况下,可以使用 Exec 作为 MSBuild 项目的一部分运行。任务。 如果
以下内容使 nginx 不传递到 PHP-FPM fastcgi 服务器,因为我创建了一个位置 block : location = /test.php { auth_basic "Nope.
java bean 命名标准是否有可能有助于实现封装? 访问修饰符不是有助于封装吗? 最佳答案 没有。命名标准不会做、开放或限制任何事情。对于封装,您当然需要访问修饰符。最好是两者都使用。即使您有一段
在 mysql Ver 14.14 Distrib 5.1.66 上,对于未知的 linux-gnu (x86_64),存在未遵守/遵守 MySQL 外键约束的问题。 数据库/表结构从未被修改过,又名
收到一封来自 Apple 的关于应用更新截止日期的电子邮件 更多信息 - link 作为开发人员需要在 iOS13 SDK 的代码库中进行哪些更改我已经更改了目标并构建了应用程序,除此之外我们还必须实
我有一个生成 IAsyncEnumerable 的异步迭代器方法(数字流),每 200 毫秒一个数字。此方法的调用者使用流,但希望在 1000 毫秒后停止枚举。所以一个CancellationToke
我是 Swift 开发的新手,在尝试做一些教程时遇到了困难(我知道,它们是一步一步的,但显然我一定在某个地方搞砸了,现在我无法弄清楚为什么它不起作用。我不太擅长调试,并且对该语言的基础缺乏扎实的理解。
有点愚蠢的问题。例如我读过,在 C11 中添加了对线程的支持。然后,例如我的STM32F4的GCC编译器说,它支持C11标准。那么,这是否意味着现在没有操作系统我将获得线程支持???但这怎么可能呢?
我的网站使用 zurb Foundation。它已经得到了自己的验证。这是a link到文档。 我使用以下代码来验证并提交我的表单数据。 $('#myForm').on('valid', functi
我知道 ansible 支持这种形式的模板中的循环: {% for host in groups['all'] %} "{{ host }}"{% if not loop.last %},{% e
我很困惑 time-based cache policies使用 HttpWebRequest 时工作。 我正在调用 WebAPI 的 GET 方法,该方法返回 JSON 内容以及类似于以下内容的 C
我正在使用 zurb foundation 5 和 abide js 检查我的表单。我必须检查 DATE1 是否早于或等于 DATE2 如何使用 abidejs 检查它是否有效? 最佳答案 简短的回答
我有一个非常简单的 javascript 构造函数,如下所示: function DisplayedTableData() { this.rowData = []; } SonarQube 提
我有一个 div 嵌套在两个 div 中。最外层和最内层的 div 都有固定的宽度。我想将最外层的 div's width 设置为 0,这样它的所有子项也都将为 0,或者至少不可见。 但是当我将最外面
我们使用 GSLB 进行地理分布和负载平衡。每个服务都分配了一个固定的域名。通过一些 DNS 魔术,域名被解析为最接近负载最少的服务器的 IP。为了使负载均衡发挥作用,应用服务器需要遵守来自 DNS
我正在编写一个向 REST API 服务发出频繁请求的 Android 应用程序。此服务的硬请求限制为每秒 2 个请求,之后它将返回 HTTP 503,没有其他信息。我想成为一名优秀的开发人员并限制我
ScalaTest 的输出是彩色的以区分不同的状态,但这些最终作为控制代码和 [34m Annoying garbage 有没有办法说服 Eclipse 控制台尊重或忽略这些序列,或者插入一个 Vie
我对 Node.js 比较陌生,我试图通过编写一个简单的模块来更熟悉它。该模块的目的是获取 ID、抓取网站并返回包含数据的字典数组。 网站上的数据分散在各个页面上,而每个页面都通过 URI 中的不同索
我正在通过 gradle/maven 使用 slf4j-android 1.6.1-RC1,当我在模拟器中运行应用程序时,当我在 Android Studio 0.3.5 下调用 Log.debug
我是一名优秀的程序员,十分优秀!