gpt4 book ai didi

ios - 使用 Objective C 在设备上播放声音

转载 作者:行者123 更新时间:2023-12-01 17:50:45 27 4
gpt4 key购买 nike

我已经在这里查看了很多关于这个问题的答案,但无法找到答案。

这是我的问题,我试图在按下按钮时播放声音,这里是文件所在的位置 File Location

这是我的代码

- (void)playSoundWithOfThisFile:(NSString*)fileName {

NSString *soundFilePath = [NSString stringWithFormat:fileName,
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;

[player play];

当我运行代码时,值如下

fileName = Mummy.mp3

`soundFilePath = Mummy.mp3` (I've had it Sounds/Mummy.mp3 and this doesn't work either)

soundFileURL = Mummy.mp3 -- file:///

player = nil

如有任何帮助,我们将不胜感激。如果我捕获到它,则该错误只是一个没有信息的一般错误

最佳答案

尝试使用下面的代码

SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"test" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);

不要忘记导入

#import <AudioToolbox/AudioToolbox.h>

关于ios - 使用 Objective C 在设备上播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189914/

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