本文介绍了处理Sprite Kit中的中断-无法通过[SKAction playSoundFileNamed:获得声音效果]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理Sprite Kit中的中断-无法通过[SKAction playSoundFileNamed:获得中断后的声音效果(如打电话)

Handling interruptions in Sprite Kit - can't get sound effects via [SKAction playSoundFileNamed: to work after interruption (like a phone call)

通过将AVAudioPlayerDelegate添加到我的GameScene.h中,然后添加到我的GameScene.m中,可以使背景音乐在中断后重启.

Was able to get background music to restart after interruption by adding AVAudioPlayerDelegate to my GameScene.h then adding to my GameScene.m

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
        [_backgroundAudioPlayer pause];

}

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    [_backgroundAudioPlayer prepareToPlay];
    [_backgroundAudioPlayer play];

}

我尝试添加

[[AVAudioSession sharedInstance] setActive:NO error:nil];

当中断开始时AppDelegate和GameScene都设置为setActive:YES,当中断没有运气结束时,声音效果在中断结束后不会产生声音

Both in AppDelegate and GameScene when interruptions starts and setActive:YES when interruption ends with no luck, sound effects generate no sound after interruption ends

推荐答案

从各种讨论中看​​,这似乎是一个错误,有时可能会引起人们的注意.简而言之,如果您遇到这些问题,并且看起来确实如此,那么可以直接跳过SKAction playSoundFileName并完全使用AVAudioPlayer.

From various discussions this seems to be a bug which can show its head on occasion. The short of it is that if you are experiencing these issues, and it appears you are, it comes down to skipping the SKAction playSoundFileName and going purely with AVAudioPlayer.

您将必须:

  1. 仅将AVAudioPlayer用于所有音频,因为它经过设计和测试可以处理中断.
  2. 将NSData用作与AVAudioPlayer一起使用的声音容器
  3. 每个声音都应具有自己的临时AVAudioPlayer实例,仅用于声音寿命.

有关如何执行此操作的详细说明,请参见 http://iknowsomething.com/ios-sdk-spritekit-sound/

There is a detailed explanation of how to do this on http://iknowsomething.com/ios-sdk-spritekit-sound/

这篇关于处理Sprite Kit中的中断-无法通过[SKAction playSoundFileNamed:获得声音效果]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:24