OpenAL iPhone:无法播放任何声音

发布于 2025-01-04 18:51:16 字数 1418 浏览 1 评论 0原文

我试图通过参考 本教程

我已经从中创建了一个整体代码来进行初步测试,但无法播放声音。我已经尝试了很多东西,但无法播放声音。非常感谢任何帮助。 谢谢。

这是我的代码:

ALCdevice* device;
device = alcOpenDevice(NULL);

ALCcontext* context;
alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

NSString* path = [[NSBundle mainBundle] pathForResource:@"mg" ofType:@"caf"];
NSURL* pathURL = [NSURL fileURLWithPath:path];

AudioFileID audioID;
OSStatus audioStatus = AudioFileOpenURL((CFURLRef)pathURL, kAudioFileReadPermission, 0, &audioID);


UInt32 fileSize = 0;
UInt64 outDataSize;
UInt32 propSize = sizeof(UInt64);
OSStatus res = AudioFileGetProperty(audioID, kAudioFilePropertyAudioDataByteCount, &propSize, &outDataSize);
fileSize = (UInt32)outDataSize;

unsigned char* outData = malloc(fileSize);
OSStatus res2 = AudioFileReadBytes(audioID, false, 0, &fileSize, outData);

AudioFileClose(audioID);

ALuint bufferID;
alGenBuffers(1, &bufferID);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 44100);

ALuint sourceID = 2;
alGenSources(1, &sourceID);
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
alSourcei(sourceID, AL_LOOPING, AL_FALSE);
free(outData);
outData = NULL;

alSourcePlay(sourceID);

I'm trying to get to play a simple sound file through OpenAL by referring this tutorial:

I've created a monolithic code from it to test initially, but cant get the sound to play. I've been trying a lot of stuff but I can't get the sound to play. Any help is much appreciated.
Thanks.

Here is my code:

ALCdevice* device;
device = alcOpenDevice(NULL);

ALCcontext* context;
alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

NSString* path = [[NSBundle mainBundle] pathForResource:@"mg" ofType:@"caf"];
NSURL* pathURL = [NSURL fileURLWithPath:path];

AudioFileID audioID;
OSStatus audioStatus = AudioFileOpenURL((CFURLRef)pathURL, kAudioFileReadPermission, 0, &audioID);


UInt32 fileSize = 0;
UInt64 outDataSize;
UInt32 propSize = sizeof(UInt64);
OSStatus res = AudioFileGetProperty(audioID, kAudioFilePropertyAudioDataByteCount, &propSize, &outDataSize);
fileSize = (UInt32)outDataSize;

unsigned char* outData = malloc(fileSize);
OSStatus res2 = AudioFileReadBytes(audioID, false, 0, &fileSize, outData);

AudioFileClose(audioID);

ALuint bufferID;
alGenBuffers(1, &bufferID);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 44100);

ALuint sourceID = 2;
alGenSources(1, &sourceID);
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
alSourcei(sourceID, AL_LOOPING, AL_FALSE);
free(outData);
outData = NULL;

alSourcePlay(sourceID);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2025-01-11 18:51:16

刚刚发现!
我一直在使用:

alcCreateContext(device, NULL);

而不是:

context = alcCreateContext(device, NULL);

alGetError() 有助于指出这一点。

Just found out!
I have been using:

alcCreateContext(device, NULL);

instead of:

context = alcCreateContext(device, NULL);

The alGetError() helped in pointing this out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文