Merge pull request #133 from halx99/wav-testcase

Add wav testcase
This commit is contained in:
HALX99 2020-08-01 06:01:27 -07:00 committed by GitHub
commit 8cc189fb4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 109 additions and 4 deletions

View File

@ -1496,6 +1496,18 @@ std::string FileUtils::getFileExtension(const std::string& filePath) const
return fileExtension;
}
std::string FileUtils::getFileShortName(const std::string& filePath)
{
std::string fileExtension;
size_t pos = filePath.find_last_of("/\\");
if (pos != std::string::npos)
{
return filePath.substr(pos + 1);
}
return filePath;
}
void FileUtils::valueMapCompact(ValueMap& /*valueMap*/) const
{
}

View File

@ -648,6 +648,13 @@ public:
*/
virtual std::string getFileExtension(const std::string& filePath) const;
/**
* Gets filename shotName
* @param filePath The path of the file, it could be a relative or absolute path.
* @return fileName.Extension without path
*/
static std::string getFileShortName(const std::string& filePath);
/**
* Checks whether the path is an absolute path.
*

View File

@ -291,12 +291,16 @@ std::vector<std::string> FileUtilsWin32::listFiles(const std::string& dirPath) c
break;
}
std::string fileName = ntcvt::from_chars(file.name);
if (fileName != "." && fileName != "..")
{
std::string filepath = ntcvt::from_chars(file.path);
if (file.is_dir)
{
filepath.push_back('/');
}
files.push_back(filepath);
}
if (tinydir_next(&dir) == -1)
{

View File

@ -33,6 +33,7 @@ using namespace cocos2d::ui;
AudioEngineTests::AudioEngineTests()
{
ADD_TEST_CASE(AudioControlTest);
ADD_TEST_CASE(AudioWavTest);
ADD_TEST_CASE(AudioLoadTest);
ADD_TEST_CASE(PlaySimultaneouslyTest);
ADD_TEST_CASE(AudioProfileTest);
@ -442,6 +443,65 @@ std::string AudioLoadTest::title() const
return "Audio preload/uncache test";
}
// AudioWavTest
AudioWavTest::~AudioWavTest()
{
}
std::string AudioWavTest::title() const
{
return "Audio wav test";
}
bool AudioWavTest::init()
{
if (AudioEngineTestDemo::init())
{
// list wav files, the wav samples download from wav wiki
_wavFiles = FileUtils::getInstance()->listFiles("wav-samples");
auto& layerSize = this->getContentSize();
_stateLabel = Label::createWithTTF("wav file:", "fonts/arial.ttf", 30);
_stateLabel->setPosition(layerSize.width / 2, layerSize.height * 0.7f);
addChild(_stateLabel);
auto playPrev = TextButton::create("Play Prev", [=](TextButton* button) {
if (_curIndex > 0) {
AudioEngine::stop(_audioID);
_audioID = AudioEngine::play2d(_wavFiles[--_curIndex]);
_stateLabel->setString(StringUtils::format("[index: %d] %s", _curIndex, FileUtils::getFileShortName(_wavFiles[_curIndex]).c_str()));
}
});
playPrev->setPosition(layerSize.width * 0.35f, layerSize.height * 0.5f);
addChild(playPrev);
auto playNext = TextButton::create("Play Next", [=](TextButton* button) {
if (_curIndex != -1 && _curIndex < (_wavFiles.size() - 1)) {
AudioEngine::stop(_audioID);
_audioID = AudioEngine::play2d(_wavFiles[++_curIndex]);
_stateLabel->setString(StringUtils::format("[index: %d] %s", _curIndex, FileUtils::getFileShortName(_wavFiles[_curIndex]).c_str()));
}
});
playNext->setPosition(layerSize.width * 0.65f, layerSize.height * 0.5f);
addChild(playNext);
return true;
}
return false;
}
void AudioWavTest::onEnter()
{
AudioEngineTestDemo::onEnter();
if (!_wavFiles.empty()) {
_curIndex = 0;
_audioID = AudioEngine::play2d(_wavFiles[_curIndex]);
_stateLabel->setString(StringUtils::format("[index: %d] %s", _curIndex, FileUtils::getFileShortName(_wavFiles[_curIndex]).c_str()));
}
}
// PlaySimultaneouslyTest
bool PlaySimultaneouslyTest::init()

View File

@ -76,6 +76,28 @@ private:
cocos2d::Label* _playOverLabel;
};
class AudioWavTest : public AudioEngineTestDemo
{
public:
CREATE_FUNC(AudioWavTest);
virtual ~AudioWavTest();
virtual bool init() override;
void onEnter() override;
virtual std::string title() const override;
private:
int _audioID = -1;
int _curIndex = -1;
std::vector<std::string> _wavFiles;
cocos2d::Label* _stateLabel = nullptr;
};
class PlaySimultaneouslyTest : public AudioEngineTestDemo
{
public:

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.