应用播放背景音频不起作用 [英] App plays background audio not working

查看:104
本文介绍了应用播放背景音频不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使此应用要通过按下主屏幕按钮关闭,也希望该应用在后台模式下播放音频.

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

使用以下代码,仅在按两次主屏幕按钮时才能处理远程控制事件.但关闭应用程序后,无法在后台播放应用程序音频.因此,如何在后台模式下播放应用程序中mp3类型的音频?当按下主屏幕按钮关闭应用程序时,这是我应用程序中的可听内容.

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.

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

In the Info.plist file i have added option

必需的背景模式App播放音频

- (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.

感谢任何帮助.

谢谢

推荐答案

您将必须通过以下AppDelegate方法设置音频会话.

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

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

然后将音频会话作为实例对象,以便可以将其称为self.youraudiosession.

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

这篇关于应用播放背景音频不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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