应用播放背景音频不工作

时间:2013-04-26 16:07:30

标签: iphone objective-c audio ios6 background

即使按下主页按钮关闭应用程序,也希望此选项的应用程序在后台模式下播放音频。

以下代码仅在按两次主页按钮时才能处理远程控制事件。但是,当应用关闭时,无法在后台播放应用音频。那么我怎样才能在后台模式下播放应用类型为mp3的音频,当我按下主页按钮关闭应用时,这是我应用中的可听内容。

在Info.plist文件中,我添加了选项

所需的背景模式应用播放音频

- (void) setupAudioSession {

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

 // Specify that this object is the delegate of the audio session, so that this object's endInterruption method will be invoked when needed.

[audioSession setDelegate: self];

// Assign the Playback category to the audio session.

NSError *audioSessionError = nil;

//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

[audioSession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError];

if (audioSessionError != nil) {

    NSLog (@"Error setting audio session category.");
    return;
}

// Activate the audio session
[audioSession setActive: YES  error: &audioSessionError];

if (audioSessionError != nil) {

    NSLog (@"Error activating audio session during initial setup.");
    return;
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
 }

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
 }

- (BOOL)canBecomeFirstResponder {
return YES;
 }

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
    if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
        //[player play];

        [self playAction];
   // } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
    //    [player pause];
    }  else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) {
        [self rewButtonPressed];

    } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack)
        [self ffwButtonPressed:nil];
}}

在按下主页按钮时,应用程序的音频未在后台播放的代码中缺少的是什么。

感谢任何帮助。

由于

3 个答案:

答案 0 :(得分:1)

您必须使用以下AppDelegate方法设置音频会话。

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

然后请将Audio会话作为Instance对象,以便可以将其称为self.youraudiosession。

答案 1 :(得分:0)

为了更清楚,请将以下方法添加到appDelegate.m

- (void) setupAudioSession {

         AVAudioSession *audioSession = [AVAudioSession sharedInstance];

        // Specify that this object is the delegate of the audio session, so that this object's endInterruption method will be invoked when needed.

        [audioSession setDelegate: self];

        // Assign the Playback category to the audio session.

        NSError *audioSessionError = nil;

        [audioSession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError];

        if (audioSessionError != nil) {

             NSLog (@"Error setting audio session category.");
             return;
        }

        // Activate the audio session
        [audioSession setActive: YES  error: &audioSessionError];

        if (audioSessionError != nil) {

             NSLog (@"Error activating audio session during initial setup.");
             return;
         }
        }

Now call  [self setupAudioSession]; from within didFinishLaunchingWithOptions.

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

          [self setupAudioSession];

          //cont. with usual code…
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          // Override point for customization after application launch.
          self.viewController = [[YOURVIEWCONTROLLER alloc] initWithNibName:@"YOURVIEWCONTROLLER" bundle:nil];

          self.window.rootViewController = self.viewController;
          [self.window makeKeyAndVisible];

          return YES;
        }

答案 2 :(得分:0)

将此代码添加到AppDelegate.m

#import <AVFoundation/AVFoundation.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                             error:&setCategoryError];
    if (!ok) {
        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }
}

在下面添加配置相同的图片: enter image description here