Complete AudioEngineTest

This commit is contained in:
pandamicro 2015-07-30 17:14:08 +08:00
parent f53ba42b3b
commit 8d8c7fe14b
2 changed files with 188 additions and 10 deletions

View File

@ -168,6 +168,7 @@ namespace {
}
float getRatio () {
_ratio = 1.0f * _percent / _maxPercent;
return _ratio;
}
@ -305,7 +306,6 @@ bool AudioControlTest::init()
float ratio = (float)slider->getPercent() / 100;
ratio = clampf(ratio, 0.0f, 1.0f);
AudioEngine::setCurrentTime(_audioID, _duration * ratio);
slider->setRatio(ratio);
}
case Slider::EventType::ON_SLIDEBALL_CANCEL:
_updateTimeSlider = true;

View File

@ -49,17 +49,17 @@ var AudioEngineBase = BaseTestLayer.extend({
},
onRestartCallback:function (sender) {
var s = new cc.Scene();
s.addChild(FlowControl.current());
s.addChild(AudioTestFlow.current());
director.runScene(s);
},
onNextCallback:function (sender) {
var s = new cc.Scene();
s.addChild(FlowControl.next());
s.addChild(AudioTestFlow.next());
director.runScene(s);
},
onBackCallback:function (sender) {
var s = new cc.Scene();
s.addChild(FlowControl.previous());
s.addChild(AudioTestFlow.previous());
director.runScene(s);
},
// automation
@ -82,7 +82,7 @@ var TextButton = ccui.Button.extend({
this.setTouchEnabled(true);
this.loadTextures("ccs-res/cocosui/animationbuttonnormal.png", "ccs-res/cocosui/animationbuttonpressed.png", "");
this.setTitleText(text);
this.addTouchEventListener(callback, this);
this.addClickEventListener(callback);
}
});
@ -133,9 +133,11 @@ var AudioControlTest = AudioEngineBase.extend({
if(self._audioID !== jsb.AudioEngine.INVALID_AUDIO_ID) {
button.setEnabled(false);
button.setBright(false);
jsb.AudioEngine.setFinishCallback(self._audioID, function(id, filePath){
self._audioID = jsb.AudioEngine.INVALID_AUDIO_ID;
self._playItem.setEnabled(true);
self._playItem.setBright(true);
self._timeRatio = 0;
self._timeSlider.setRatio(self._timeRatio);
@ -152,6 +154,7 @@ var AudioControlTest = AudioEngineBase.extend({
self._audioID = jsb.AudioEngine.INVALID_AUDIO_ID;
self._playItem.setEnabled(true);
self._playItem.setBright(true);
}
});
stopItem.setPosition(layerSize.width * 0.7, layerSize.height * 0.8);
@ -200,6 +203,7 @@ var AudioControlTest = AudioEngineBase.extend({
self._audioID = jsb.AudioEngine.INVALID_AUDIO_ID;
self._playItem.setEnabled(true);
self._playItem.setBright(true);
});
uncacheItem.setPosition(layerSize.width * 0.7, layerSize.height * 0.6);
this.addChild(uncacheItem);
@ -271,12 +275,186 @@ var AudioControlTest = AudioEngineBase.extend({
}
});
var TEST_COUNT = 10
var PlaySimultaneouslyTest = AudioEngineBase.extend({
_title: "Simultaneously play multiple audio",
_files: null,
_playingcount: 0,
_playItem: null,
ctor: function () {
this._super();
var text, tmp = 81, index, self = this;
this._files = [];
for(index = 0; index < TEST_COUNT; ++index){
this._files.push("audio/SoundEffectsFX009/FX0" + (tmp + index) + ".mp3");
}
var playItem = new TextButton("play-simultaneously", function(button) {
var audioId;
self._playingcount = 0;
button.setEnabled(false);
button.setBright(false);
var startTime = Date.now();
cc.log(startTime);
for(index = 0; index < TEST_COUNT; ++index){
audioId = jsb.AudioEngine.play2d(self._files[index]);
if(audioId != jsb.AudioEngine.INVALID_AUDIO_ID){
self._playingcount += 1;
jsb.AudioEngine.setFinishCallback(audioId, function(id, filePath){
self._playingcount -= 1;
if(self._playingcount <= 0){
self._playItem.setEnabled(true);
self._playItem.setBright(true);
}
});
}
else {
cc.log("Fail to play file: " + self._files[index]);
}
}
var diff = Date.now() - startTime;
cc.log("Diff time: " + diff);
});
playItem.setNormalizedPosition(cc.p(0.5, 0.5));
this.addChild(playItem);
this._playItem = playItem;
}
});
var FILE_COUNT = 2;
var AudioProfileTest = AudioEngineBase.extend({
_title: "AudioProfileTest",
_subtitle: "See the console.",
_minDelay: 1,
_time: 0,
_audioCount: 0,
_files: null,
_audioProfile: null,
_showLabel: null,
ctor: function () {
this._super();
var text;
this._files = [];
this._files.push("background.mp3");
if (cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX)
this._files.push("background.caf");
else
this._files.push("background.ogg");
this._audioProfile = new jsb.AudioProfile();
this._audioProfile.name = "AudioProfileTest";
this._audioProfile.maxInstances = 3;
this._audioProfile.minDelay = 1.0;
var pos = cc.p(0.5, 0.7), index, self = this;
for(index = 0; index < FILE_COUNT; ++index) {
text = "play " + this._files[index];
var playItem = new TextButton(text, function(button){
var index = button.getTag();
var id = jsb.AudioEngine.play2d(self._files[index], false, 1, self._audioProfile);
if(id != jsb.AudioEngine.INVALID_AUDIO_ID){
self._time = self._minDelay;
self._audioCount += 1;
var show = "audio count: " + self._audioCount;
self._showLabel.setString(show);
jsb.AudioEngine.setFinishCallback(id, function(id, filePath){
self._audioCount -= 1;
var show = "audio count: " + self._audioCount;
self._showLabel.setString(show);
});
}
});
playItem.setTag(index);
playItem.setNormalizedPosition(pos);
this.addChild(playItem);
pos.y -= 0.15;
}
var origin = cc.visibleRect.bottomLeft;
var size = cc.winSize;
var profileInfoLabel = new cc.LabelTTF("AudioProfile Info:\n max instance:3 \n minimum delay:1.0", "Arial", 12);
profileInfoLabel.setAnchorPoint(cc.p(0, 0.5));
profileInfoLabel.setPosition(cc.p(origin.x, origin.y + size.height * 0.65));
this.addChild(profileInfoLabel);
this._showLabel = new cc.LabelTTF("audio count:0", "Arial", 12);
this._showLabel.setAnchorPoint(cc.p(0, 0.5));
this._showLabel.setPosition(cc.p(origin.x, origin.y + size.height * 0.5));
this.addChild(this._showLabel);
var timeSlider = new Slider();
timeSlider.setEnabled(false);
timeSlider.setNormalizedPosition(pos);
this.addChild(timeSlider);
this._timeSlider = timeSlider;
this.schedule(this.update, 0.05);
},
update: function (dt) {
if(this._time > 0.0)
{
this._time -= dt;
this._timeSlider.setRatio(this._time / this._minDelay);
}
}
});
var InvalidAudioFileTest = AudioEngineBase.extend({
_title: "Test invalid audio file",
_subtitle: "Not crash, please see the console.",
ctor: function () {
this._super();
var playItem = new TextButton("play unsupported media type", function(button) {
if (cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX) {
jsb.AudioEngine.play2d("background.ogg");
}
else if (cc.sys.os == cc.sys.OS_ANDROID || cc.sys.os == cc.sys.OS_WINDOWS) {
jsb.AudioEngine.play2d("background.caf");
}
});
playItem.setNormalizedPosition(cc.p(0.5, 0.6));
this.addChild(playItem);
var playItem2 = new TextButton("play not-existent file", function(button) {
jsb.AudioEngine.play2d("not-existent file.mp3");
});
playItem2.setNormalizedPosition(cc.p(0.5, 0.4));
this.addChild(playItem2);
}
});
var LargeAudioFileTest = AudioEngineBase.extend({
_title: "Test large audio file",
ctor: function () {
this._super();
var playItem = new TextButton("play large audio file", function(button){
jsb.AudioEngine.play2d("audio/LuckyDay.mp3");
});
playItem.setNormalizedPosition(cc.p(0.5, 0.5));
this.addChild(playItem);
}
})
window.AudioTestFlow = new FlowControl([
AudioControlTest,
// PlaySimultaneouslyTest,
// AudioProfileTest,
// InvalidAudioFileTest,
// LargeAudioFileTest
PlaySimultaneouslyTest,
AudioProfileTest,
InvalidAudioFileTest,
LargeAudioFileTest
]);
})(window);
})(window);