3

是否有人在通过 iOS 设备的外部扬声器使用 iOS Sprite Kit 通过 SKAction playSoundFileNamed 播放音频时遇到问题?我有以下代码可以通过耳机毫无问题地播放 M4A 文件;但是,当我拔下耳机单步执行代码时,不会播放任何音频。我有另一个不使用此方法的应用程序,它可以正常运行。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKAction *sound = [SKAction playSoundFileNamed:@"blast.m4a" waitForCompletion:NO];
        [self runAction:sound];
 }
4

2 回答 2

7

I had a similar issue but was able to fix it by setting AudioSession category to Playback. This causes the audio to be routed through the phone's speaker when headphones aren't plugged in.

// at the top of AppDelegate.m
#import <AVFoundation/AVFoundation.h>

// in application:didFinishLaunchingWithOptions:
NSError *error;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];

You can find more information about this in Apple's Audio Session Programming Guide.

于 2013-11-28T17:18:17.293 回答
0

只是为了更新,我今天安装了 IOS 7.0.4 并重新测试,现在我在外部扬声器上有音频,所以我假设这是 IOS 7.0.3 中的一个错误,因为这是我对设备所做的唯一更改.

于 2013-11-26T00:53:45.357 回答