Skip to content

Commit 977fc4d

Browse files
JohnCoconuthuangwei1024
authored andcommitted
refactor AudioEngine-Linux (cocos2d#19822)
* 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 189b1df commit 977fc4d

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
@@ -349,7 +349,7 @@ void AudioEngine::remove(int audioID)
349349
it->second.profileHelper->audioIDs.remove(audioID);
350350
}
351351
_audioPathIDMap[*it->second.filePath].remove(audioID);
352-
_audioIDInfoMap.erase(audioID);
352+
_audioIDInfoMap.erase(it);
353353
}
354354
}
355355

@@ -394,16 +394,13 @@ void AudioEngine::uncache(const std::string &filePath)
394394
{
395395
itInfo->second.profileHelper->audioIDs.remove(audioID);
396396
}
397-
_audioIDInfoMap.erase(audioID);
397+
_audioIDInfoMap.erase(itInfo);
398398
}
399399
}
400400
_audioPathIDMap.erase(filePath);
401401
}
402402

403-
if (_audioEngineImpl)
404-
{
405-
_audioEngineImpl->uncache(filePath);
406-
}
403+
_audioEngineImpl->uncache(filePath);
407404
}
408405

409406
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)