App plays background audio not working

有些话、适合烂在心里 提交于 2019-12-24 12:12:00

问题


Want to have this option of App plays audio in background mode even when app is closed by pressing the home button.

With the below code only able to handle remote control events when home button is pressed twice. But not able to play app audio in background when app is closed. So how can i play app's audio of type mp3 in background mode which is audible content in my app when home button is pressed to close the app.

In the Info.plist file i have added option

Required background modes App plays audio

- (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];
}}

What i am missing in the code that app's audio is not playing in the background when home button is pressed.

Appreciate any help.

Thanks


回答1:


You will have to set up the Audio session in the following method of AppDelegate.

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

And then please make the Audio session as an Instance object so that it can be called as self.youraudiosession.




回答2:


Just to be more clear add below method to 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;
        }



回答3:


Add this code to 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);
    }
}

Add config the same image below:



来源:https://stackoverflow.com/questions/16240741/app-plays-background-audio-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!