gpt4 book ai didi

objective-c - iOS 7 SDK 不遵守背景音频

转载 作者:太空狗 更新时间:2023-10-30 03:11:31 24 4
gpt4 key购买 nike

我在 Google 和 StackOverflow 上做了很多研究。我找到的所有答案都不适用于 iOS 7。我开始使用 Xcode 5 在 iOS 7 SDK 中编写新的应用程序。

我想要做的就是在应用程序中播放应用程序包中存储的文件中的音频(而不是来自音乐库)。我想要在后台播放音频并在屏幕锁定时控制(除了控制中心)。

我将 APPNAME-Info.plistUIBackgroundModes 设置为音频。它不处理应用程序委托(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com