gpt4 book ai didi

ios - 在 iOS 中简单地播放声音

转载 作者:行者123 更新时间:2023-11-29 10:36:24 25 4
gpt4 key购买 nike

我正在尝试在单击按钮时播放声音。首先,我在头文件中导入了 AudioToolbox,然后将代码添加到按下按钮时触发的 IBAction,如下所示:

    - (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)button1:(id)sender {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"mySong" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}

我什至在方法中添加了一个 NSLog 以确保它正在触发。这是。我也在字符串中的 foo 路径中查看它是否会崩溃,它确实发生了。我确保我也将短声音文件拖放到应用程序中。测试了我的扬声器,通过在模拟器上访问 youtube 在设备模拟器上测试了音频,一切。我不知道我做错了什么。

当我将代码更改为:

- (IBAction)button1:(id)sender {


AVAudioPlayer *testAudioPlayer;

// *** Implementation... ***

// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;

// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];


if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}}

我收到错误:警告:137:错误“!dat”试图设置(空)音频设备的采样率

最佳答案

您好,根据您的问题,您能否检查您的项目文件夹中是否存在音频文件。您可能忘记勾选“如果需要复制项目”,因此它可能不在您的项目文件中。

您还可以播放设备中提供的声音。访问下面的链接会有帮助。

https://github.com/TUNER88/iOSSystemSoundsLibrary

我已经测试了您的代码,它工作正常并且没有崩溃。这就是我所做的。 enter image description here

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

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