MapMapM 2013-04-28 06:43 采纳率: 0%
浏览 1108

应用无法播放背景音频

需要应用能实现在隐藏后播放音频的效果。在按下home键后也能继续播放。

下面的代码只能在home键按两下后进行一些远程控制,背景中播放音频就不行。,我需要的是在应用隐藏背景模式下播放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];
}}

请高手指教一些,非常感谢!

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-23 17:49
    关注

    要使应用在后台播放音频,您需要在 iOS 应用的 info.plist 文件中添加一个键为 "Required background modes" 的数组,并在数组中添加一个字符串值 "App plays audio"。这将告诉 iOS 您的应用需要在后台播放音频。

    然后,您需要使用 AVAudioSession 类配置音频会话以在后台播放音频。您可以在您的应用启动时进行设置,或者在需要时激活音频会话。


    首先,您需要获取单例的 AVAudioSession 对象:

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    

    然后,您需要将音频会话的类别设置为 "playback",以便在后台播放音频:

    NSError *error = nil;
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
    if (error) {
        // handle error
    }
    

    最后,您需要激活音频会话:

    [audioSession setActive:YES error:&error];
    if (error) {
        // handle error
    }
    

    现在,您就可以在后台使用音频播放器播放音频了。


    注意:在后台播放音频可能会增加应用的电池使用,因此您应当谨慎使用此功能。

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题