mirror of https://github.com/axmolengine/axmol.git
Add wav testcase
This commit is contained in:
parent
5edcd0b97d
commit
bd9a409dd7
|
@ -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
|
||||
{
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -291,12 +291,16 @@ std::vector<std::string> FileUtilsWin32::listFiles(const std::string& dirPath) c
|
|||
break;
|
||||
}
|
||||
|
||||
std::string filepath = ntcvt::from_chars(file.path);
|
||||
if (file.is_dir)
|
||||
std::string fileName = ntcvt::from_chars(file.name);
|
||||
if (fileName != "." && fileName != "..")
|
||||
{
|
||||
filepath.push_back('/');
|
||||
std::string filepath = ntcvt::from_chars(file.path);
|
||||
if (file.is_dir)
|
||||
{
|
||||
filepath.push_back('/');
|
||||
}
|
||||
files.push_back(filepath);
|
||||
}
|
||||
files.push_back(filepath);
|
||||
|
||||
if (tinydir_next(&dir) == -1)
|
||||
{
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue