gpt4 book ai didi

ios - 播放简单的声音

转载 作者:行者123 更新时间:2023-11-28 19:50:00 26 4
gpt4 key购买 nike

我需要在 Action 发生时播放声音, Action 结束时我已经有了回调并且工作正常,现在我想向它添加声音。我添加了 AudioToolbox 作为框架,这是我尝试使用的代码:

SystemSoundID playSoundID;
NSURL *clockSound = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"clock" ofType:@"mp3"]]; //the path for my resource
AudioServicesCreateSystemSoundID((__bridge CFURLRef)clockSound, &playSoundID);
AudioServicesPlaySystemSound(playSoundID);

我没有收到任何错误,但没有播放声音

最佳答案

AudioServicesPlaySystemSound() 函数无法播放任何 MP3 文件。该功能仅支持.caf.aac.wav这几种文件格式,并且声音必须在30秒或更短。

Sound files that you play using this function must be: No longer than 30 seconds in duration In linear PCM or IMA4 (IMA/ADPCM) format Packaged in a .caf, .aif, or .wav file

https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html#//apple_ref/c/func/AudioServicesPlaySystemSound

所以如果clock.mp3的时长小于30秒,转换文件格式就可以播放了。

mp3 转换为 caf,例如使用 afconvert 命令,如下所示:

$ afconvert -f caff -d ima4 clock.mp3 clock.caf

如果没有(声音超过30秒),请改用AVFoundation.frameworkAVPlayer。请参阅参议员评论的链接。

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player play];

关于ios - 播放简单的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29703445/

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