iOS8& xcode6不再播放声音

时间:2014-10-15 20:18:06

标签: audio ios8 xcode6 audiotoolbox

我遇到了iOS 8和XCode6重大更新后出现的问题。

当我尝试用AudioToolBox播放声音时,扬声器没有任何声音。我正在使用模拟器。

我有两个播放声音的函数变体。

-(void)playSound:(NSString *)fileName
{
    SystemSoundID soundEffet = [self soundEffect];
    NSURL *soundURL = [[NSBundle mainBundle] URLForResource:fileName withExtension:@"mp3"];
    NSString *URLString = [soundURL absoluteString];
    if ([[NSFileManager defaultManager] fileExistsAtPath:URLString])
    {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &soundEffet);
        AudioServicesPlaySystemSound(soundEffet);
    } else {
        NSLog(@"error, file not found: %@", fileName);
    }
}

这在路径检查时存在文件失败。

-(void)playSound:(NSString *)fileName
{
    SystemSoundID soundEffet = [self soundEffect];
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:URLString])
    {
        NSURL *pathURL = [NSURL fileURLWithPath:path];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &soundEffet);
        AudioServicesPlaySystemSound(soundEffet);
    } else {
        NSLog(@"error, file not found: %@", fileName);
    }
}

这个人什么都不玩。

我非常确定我使用的文件名是正确的。

有没有人对改变了什么有任何想法,以致这些不再有效?最重要的是:我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

所以我明白了! (提起鞭打的kermit武器。)Reinhard Manner的评论指出了我正确的方向。 我确实最终使用了AVAudioPlayer。这就是我最终的目标。

-(void)playSound:(NSString *)fileName
{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType: @"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
    soundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    if (soundPlayer != nil)
    {
        [soundPlayer play];
    } else {
        NSLog(@"could not play with file %@", fileName);
    }
}

这里没有包含在文件顶部合成的soundPlayer,并在.h文件中创建了一个属性。

我仍然不知道为什么AudioToolbox停止工作。但这是有效的,我会随之而去。

此外,Reinhard Manner,您可能希望下次将这类事物作为答案发布,以便您获得更多信用!

相关问题