Ios 开放集固定卷

Ios 开放集固定卷,ios,iphone,sprite-kit,openal,Ios,Iphone,Sprite Kit,Openal,我使用OpenAL在游戏中播放背景音乐和音效,播放背景音乐的代码如下 - (void) playSoundBack { alGenSources(1, &sourceIDBack); NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"wav"]; NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];

我使用OpenAL在游戏中播放背景音乐和音效,播放背景音乐的代码如下

- (void) playSoundBack
{


alGenSources(1, &sourceIDBack);

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"wav"];
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];

AudioFileID afid;
OSStatus openAudioFileResult = AudioFileOpenURL((__bridge CFURLRef)audioFileURL, kAudioFileReadPermission, 0, &afid);

if (0 != openAudioFileResult)
{
    NSLog(@"An error occurred when attempting to open the audio file %@: %ld", audioFilePath, openAudioFileResult);
    return;
}

UInt64 audioDataByteCount = 0;
UInt32 propertySize = sizeof(audioDataByteCount);
OSStatus getSizeResult = AudioFileGetProperty(afid, kAudioFilePropertyAudioDataByteCount, &propertySize, &audioDataByteCount);

if (0 != getSizeResult)
{
    NSLog(@"An error occurred when attempting to determine the size of audio file %@: %ld", audioFilePath, getSizeResult);
}

UInt32 bytesRead = (UInt32)audioDataByteCount;

void *audioData = malloc(bytesRead);

OSStatus readBytesResult = AudioFileReadBytes(afid, false, 0, &bytesRead, audioData);

if (0 != readBytesResult)
{
    NSLog(@"An error occurred when attempting to read data from audio file %@: %ld", audioFilePath, readBytesResult);
}

AudioFileClose(afid);

ALuint outputBuffer;
alGenBuffers(1, &outputBuffer);

alBufferData(outputBuffer, AL_FORMAT_STEREO16, audioData, bytesRead, 44100);

if (audioData)
{
    free(audioData);
    audioData = NULL;
}


alSourcef(sourceIDBack, AL_GAIN, 0.1f);

alSourcei(sourceIDBack, AL_BUFFER, outputBuffer);
alSourcei(sourceIDBack, AL_LOOPING, AL_TRUE);
alSourcePlay(sourceIDBack);
}


用另一个类似的代码块,我在游戏中玩了一些特效。所有的声音都播放了,但音量有问题。。。背景音乐音量变化,似乎不是绝对值。。。当其他效果播放时,背景音乐音量较低,正如我使用Alu GAIN设置的那样,但是当没有其他效果播放时,背景音乐声音太大(或者至少这是我的感觉…)。为什么音乐有这种行为?像0.1这样的音量设置是相对的吗?是否可以设置一个固定且通用的值?

首先要检查的是,在计算机上定期播放音频文件时,音频文件的声音是否正确,如果不正确,请使用Audacity尝试正常化音量。请注意,声音听起来会有所不同(包括音量),这取决于内容(有些音频听起来更响亮,尽管从技术上讲,它是相同的音量/dba)、设备(与计算机扬声器),以及您是在听内置扬声器还是耳塞/耳机。谢谢,我已尝试将所有声音正常化,现在所有声音都变大了,但问题仍然存在…当只播放背景音乐时,“背景音乐音量”会变大。。。