gpt4 book ai didi

objective-c - 在应用程式中播放声音

转载 作者:行者123 更新时间:2023-12-01 18:30:18 25 4
gpt4 key购买 nike

这是我在应用程序中播放声音的方法

NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/sound.wav"];

SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);

上面的代码在模拟器中效果很好,但在设备上却不行。你能推荐我一个简单的
播放声音的方式?我知道AVAudioPlayer,但我正在寻找更简单的方法。像上面的代码。

最佳答案

这就是我的方法,并且在Simulator和Real Device上都可以正常工作

SystemSoundID sounds[10];
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"applause" ofType:@"mp3"];
CFURLRef soundURL = (__bridge CFURLRef)[NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundURL, &sounds[0]);
AudioServicesPlaySystemSound(sounds[0]);

希望这可以帮助

关于objective-c - 在应用程式中播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752228/

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