ios - 进入后台时AVAudioSession问题

标签 ios objective-c iphone sprite-kit avaudiosession

我试图在我的游戏进入后台之前停止音乐播放器,但没有成功。我查看了所有与我遇到的问题相同的问题,但找不到我需要的答案。我正在从主视图 Controller 播放我的游戏音乐:

XYZViewController.h

@import AVFoundation;

@interface XYZViewController : UIViewController 
@property (readwrite)AVAudioPlayer* backgroundMusicPlayer;

编辑添加这段代码,这是我分配和初始化音乐播放器。忘记了,补充之前,抱歉:

XYZViewController.m
- (void)viewDidLoad
{

    [super viewDidLoad];
    [self gameCenterLogin];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;


    //AUDIO BACKground
    NSError *error;
    NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"Astro World music" withExtension:@"mp3"];
    self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    self.backgroundMusicPlayer.numberOfLoops = -1;
    [self.backgroundMusicPlayer prepareToPlay];
    [self.backgroundMusicPlayer play];

[skView presentScene:scene];

}

我的 viewController 中有这两个公共(public)方法来停止和播放我的音乐:

-(void)stopPlayer{
    [self.backgroundMusicPlayer stop];
}
-(void)playMusic{

    [self.backgroundMusicPlayer play];
}

这是我在进入后台之前从 appDelegate.m 所做的,我从我的 viewController 调用方法在停止 AVAUdioSession 之前停止音乐

    - (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.


    XYZViewController* mainController = (XYZViewController*)  self.window.rootViewController;
    [mainController stopPlayer];
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
    NSLog(@"pause music");

但无论我做什么,我总是得到同样的错误:

    RROR:     [0x35aa49dc] AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
2015-01-15 20:09:49.080 Astro World[269:23154] pause music

如有任何帮助,我们将不胜感激!

最佳答案

将此代码插入 AVAudioPlayerDelegate 的方法中。

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
  [audioPlayer stop];
  [audioPlayer prepareToPlay];
}

其中 audioPlayer 是在我的类的 header 中声明的变量。

prepareToPlay 指令应该可以解决您的问题!

这可能对你有帮助:)

关于ios - 进入后台时AVAudioSession问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27971319/

相关文章:

ios - 如何拆除核心数据堆栈?

iOS 蓝牙 UUID 在 8.2 上启动一些应用程序后发生变化

iPhone - 如何将构建配置更改为分发

iphone - 在 UITableView 中创建运行总和

ios - 检测用户是否第一次在 iOS 中打开 View

iphone - 将 iPhone 应用程序转换为通用应用程序?

ios - 无法将渲染回调添加到输出单元

objective-c - 调试时检查 NSImage 引用?

ios - 面向初学者的变量赋值和字符串数组 Obj-c (iOS)

iPhone UIDocumentInteractionController PresentOpenInMenuFromRect 取消按钮没有响应