gpt4 book ai didi

ios - iOS模拟器无法播放声音效果

转载 作者:行者123 更新时间:2023-12-01 16:36:00 31 4
gpt4 key购买 nike

我知道这个问题被问过999次。所有答案都表明,请转到“系统偏好设置”>“声音”>“声音效果”,并确保选中“播放用户界面声音效果”,或者取消选中并重新选中它。

这对我不起作用。我检查了它,但是声音效果仍然没有播放。

更多信息:

  • 我正在尝试使用SystemSoundID播放声音效果。
  • 我以前使用AVAudioPlayer来产生声音效果,但它确实起作用。
  • 在模拟器中从Safari在YouTube上播放,并播放了视频声音。
  • 我已将AudioToolbox.framework链接到我的项目。
  • 我从here下载了Apple示例代码。

  • 这也没有发挥,所以我很确定我的代码没有错。
    6.我在Mac OS X 10.10.1和XCode 6.1.1的优胜美地上运行Mac。

    由于我目前没有开发者帐户,因此尚未在实际设备上进行过测试。我不想付我一生的99美元积蓄,只是为了发现我没有检查的地方有一个盒子。

    还有什么我应该尝试的吗?

    我的代码:

    SoundEffect.h
    #import <AudioToolbox/AudioServices.h>

    @interface SoundEffect : NSObject
    {
    SystemSoundID soundID;
    }

    - (id)initWithSoundNamed:(NSString *)filename withExtension:(NSString*) extension;

    - (void)play;

    @end

    SoundEffect.m
    #import "SoundEffect.h"

    @implementation SoundEffect

    - (id)initWithSoundNamed:(NSString *)filename withExtension:(NSString *)extension
    {
    if ((self = [super init]))
    {
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
    if (fileURL != nil)
    {
    NSLog(@"fileURL found");
    SystemSoundID theSoundID;
    OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
    if (error == kAudioServicesNoError)
    soundID = theSoundID;
    }
    }
    return self;
    }

    - (void)dealloc
    {
    AudioServicesDisposeSystemSoundID(soundID);
    }

    - (void)play
    {
    if([[NSUserDefaults standardUserDefaults] boolForKey:soundEffectsKey]) {
    NSLog(@"Play sound effect");
    AudioServicesPlaySystemSound(soundID);
    }else {
    NSLog(@"Sound effects are disabled");
    }
    }

    注意:我创建了这个对象,并在项目中需要的任何地方播放它。当我尝试播放声音时,会同时记录“找到文件URL”和“播放声音效果”。

    资料来源: Play a short sound in iOS

    最佳答案

        SystemSoundID customSound;

    NSString *soundFile = [[NSBundle mainBundle]pathForResource:<#customSoundName#> ofType:<#soundFormat#>];
    NSURL *soundURL = [NSURL fileURLWithPath:openMenu];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, & customSound);
    AudioServicesPlaySystemSound(customSound);

    这是我的作品

    关于ios - iOS模拟器无法播放声音效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28221175/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com