Skip to content

Commit 3e6b1ff

Browse files
authored
refactor AudioEngine-Linux (#19822) (#19946)
* refactor AudioEngine and AudioEngine-linux * map::erase() can handle case if key doesn't exist. * use map::iterator when it has already obtained. * mapChannelInfo[id].channel is nullptr befor resume(). Don't dereference it. * FMOD::System::release() calls close, so calling close before release is not necessary. * use std::map::insert properly. * remove unnecessary null check on _audioEngineImpl * add comment on nullptr dereference
1 parent 77a7205 commit 3e6b1ff

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

cocos/audio/AudioEngine.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ void AudioEngine::remove(int audioID)
344344
it->second.profileHelper->audioIDs.remove(audioID);
345345
}
346346
_audioPathIDMap[*it->second.filePath].remove(audioID);
347-
_audioIDInfoMap.erase(audioID);
347+
_audioIDInfoMap.erase(it);
348348
}
349349
}
350350

@@ -389,16 +389,13 @@ void AudioEngine::uncache(const std::string &filePath)
389389
{
390390
itInfo->second.profileHelper->audioIDs.remove(audioID);
391391
}
392-
_audioIDInfoMap.erase(audioID);
392+
_audioIDInfoMap.erase(itInfo);
393393
}
394394
}
395395
_audioPathIDMap.erase(filePath);
396396
}
397397

398-
if (_audioEngineImpl)
399-
{
400-
_audioEngineImpl->uncache(filePath);
401-
}
398+
_audioEngineImpl->uncache(filePath);
402399
}
403400

404401
void AudioEngine::uncacheAll()

cocos/audio/linux/AudioEngine-linux.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ AudioEngineImpl::AudioEngineImpl()
7272
AudioEngineImpl::~AudioEngineImpl()
7373
{
7474
FMOD_RESULT result;
75-
result = pSystem->close();
76-
ERRCHECKWITHEXIT(result);
7775
result = pSystem->release();
7876
ERRCHECKWITHEXIT(result);
7977
}
@@ -109,7 +107,8 @@ int AudioEngineImpl::play2d(const std::string &fileFullPath, bool loop, float vo
109107
int id = preload(fileFullPath, nullptr);
110108
if (id >= 0) {
111109
mapChannelInfo[id].loop=loop;
112-
mapChannelInfo[id].channel->setPaused(true);
110+
// channel is null here. Don't dereference it. It's only set in resume(id).
111+
//mapChannelInfo[id].channel->setPaused(true);
113112
mapChannelInfo[id].volume = volume;
114113
AudioEngine::_audioIDInfoMap[id].state = AudioEngine::AudioState::PAUSED;
115114
resume(id);
@@ -287,8 +286,7 @@ void AudioEngineImpl::uncache(const std::string& path)
287286
}
288287
mapSound.erase(it);
289288
}
290-
if (mapId.find(path) != mapId.end())
291-
mapId.erase(path);
289+
mapId.erase(path);
292290
}
293291

294292
void AudioEngineImpl::uncacheAll()
@@ -320,10 +318,9 @@ int AudioEngineImpl::preload(const std::string& filePath, std::function<void(boo
320318
}
321319

322320
int id = static_cast<int>(mapChannelInfo.size()) + 1;
323-
if (mapId.find(filePath) == mapId.end())
324-
mapId.insert({filePath, id});
325-
else
326-
id = mapId.at(filePath);
321+
// std::map::insert returns std::pair<iter, bool>
322+
auto channelInfoIter = mapId.insert({filePath, id});
323+
id = channelInfoIter.first->second;
327324

328325
auto& chanelInfo = mapChannelInfo[id];
329326
chanelInfo.sound = sound;

0 commit comments

Comments
 (0)