void AndroidSoundEngine::stopBGM(int level)
{
	if (_bgms.find(level) != _bgms.end())
	{
		BGMInfo& info = _bgms[level];
		SLresult result;
		if (info.BGMPlayer == NULL)
		{
			playBGM(info.name, info.path, level, info.volume, info.loop);
		}
		result = (*info.BGMPlayer)->SetPlayState(info.BGMPlayer, SL_PLAYSTATE_STOPPED);
		info.state = SL_PLAYSTATE_STOPPED;
		slCheckErrorWithStatus(result, "Problem stopping BGMplayer (Error %d).", result);
	}
}
Пример #2
0
void SndTest::onEnter()
{
    CCLayer::onEnter();
    
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    makeBtn();
    
    playBGM();
    
    isNarrPlaying = false;
    
    CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
                                                                  callfuncO_selector(SndTest::onNarrationFinished),
                                                                  CJNOTIFICATION_VOICE_FINISHCALL,
                                                                  NULL);
}
void AndroidSoundEngine::setBGMVolume(int level, short int volume)
{
	if (_bgms.find(level) != _bgms.end())
	{
		BGMInfo& info = _bgms[level];
		SLresult result;
		info.volume = volume;
		if (info.BGMPlayer == NULL)
		{
			playBGM(info.name, info.path, level, info.volume, info.loop);
		}
		 result = (*info.BGMPlayerVolume)->SetVolumeLevel(info.BGMPlayerVolume, (float)volume / 100.f * -MINIMUM_VOLUME + MINIMUM_VOLUME);
		    slCheckErrorWithStatus(result, "Problem setting VOLUME for BGM player (Error %d).", result);

	}
}
void AndroidSoundEngine::setBGMLoops(int level, bool isLooping)
{
	if (_bgms.find(level) != _bgms.end())
	{
		BGMInfo& info = _bgms[level];
		SLresult result;
		info.loop = isLooping;
		if (info.BGMPlayer == NULL)
		{
			playBGM(info.name, info.path, level, info.volume, info.loop);
		}
		if (isLooping)
			result = (*info.BGMPlayerSeek)->SetLoop(info.BGMPlayerSeek, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN);
		else
			result = (*info.BGMPlayerSeek)->SetLoop(info.BGMPlayerSeek, SL_BOOLEAN_FALSE, 0, SL_TIME_UNKNOWN);
		slCheckErrorWithStatus(result, "Problem setting loops BGMplayer (Error %d).", result);
	}
}
// on "init" you need to initialize your instance
bool Mainpage::init() {
    //////////////////////////////
    // 1. super init first
    if (!Layer::init()) {
        return false;
    }

    //visibleSize = Director::getInstance()->getVisibleSize();
    visibleSize = Global::getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();

    preloadBGM(); // 预载入BGM
    addBackground(); // 添加背景
    addMenu(); // 添加菜单
    addUI(); // 添加UI
    // 播放bgm
    playBGM();

    // 每分钟更新一次时间
    this->schedule(schedule_selector(Mainpage::updateTime), 60.0f, kRepeatForever, 0);
    return true;
}
void AndroidSoundEngine::playBGM(const BString& name, const BString& path, int level, short int volume, bool isLooping)
{

	if (_bgms.find(level) != _bgms.end())
	{
		if (_bgms[level].path == path && _bgms[level].BGMPlayer != NULL)
		{
			playBGM(level);
			return ;
		}
		destroyBGM(level);
	}

	SLresult result;

	BReader* reader = BReader::create();

	if (!reader->open(path))
	{
		Log::error("Error cannot open audio file <%s>", path.c_str());
		return ;
	}

	BGMInfo info;

	info.name = name;
	info.path = path;
	info.fd = reader->fd();
	info.start = reader->start();
	info.length = reader->length();
	info.volume = volume;
	info.loop = isLooping;
	info.seek = 0;

	reader->close();

	SLDataLocator_AndroidFD _dataLocatorIn;
	_dataLocatorIn.locatorType = SL_DATALOCATOR_ANDROIDFD;
	_dataLocatorIn.fd = info.fd;
	_dataLocatorIn.offset = info.start;
	_dataLocatorIn.length = info.length;
	SLDataFormat_MIME _dataFormat;
	_dataFormat.formatType = SL_DATAFORMAT_MIME;
	_dataFormat.mimeType = NULL;
	_dataFormat.containerType = SL_CONTAINERTYPE_UNSPECIFIED;

	SLDataSource _dataSource;
   _dataSource.pLocator = &_dataLocatorIn;
   _dataSource.pFormat = &_dataFormat;
   SLDataLocator_OutputMix _dataLocatorOut;
   _dataLocatorOut.locatorType = SL_DATALOCATOR_OUTPUTMIX;
   _dataLocatorOut.outputMix = _outputMix;
   SLDataSink _dataSink;
   _dataSink.pLocator = &_dataLocatorOut;
   _dataSink.pFormat = NULL;
   Log::debug("A1");
   // Creates BGM player and retrieves its interfaces.
	const SLuint32 _BGMPlayerIIDCount = 3;
	const SLInterfaceID _BGMPlayerIIDs[] = { SL_IID_PLAY, SL_IID_SEEK, SL_IID_VOLUME };
	const SLboolean _BGMPlayerReqs[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
	result = (*_engine)->CreateAudioPlayer(_engine, &info.BGMPlayerObj, &_dataSource, &_dataSink,
	 _BGMPlayerIIDCount, _BGMPlayerIIDs, _BGMPlayerReqs);
	 Log::debug("A2");
	slCheckErrorWithStatus(result, "Problem creating BGM player (Error %d).", result);
	result = (*info.BGMPlayerObj)->Realize(info.BGMPlayerObj, SL_BOOLEAN_FALSE);
	slCheckErrorWithStatus(result, "Problem realizing BGM player (Error %d).", result);

	result = (*info.BGMPlayerObj)->GetInterface(info.BGMPlayerObj, SL_IID_PLAY, &info.BGMPlayer);
	slCheckErrorWithStatus(result, "Problem getting SL_IID_PLAY for BGM player (Error %d).", result);

	result = (*info.BGMPlayerObj)->GetInterface(info.BGMPlayerObj, SL_IID_SEEK, &info.BGMPlayerSeek);
	slCheckErrorWithStatus(result, "Problem getting SL_IID_SEEK for BGM player (Error %d).", result);
	// Enables looping and starts playing.
	if (isLooping)
	{
		result = (*info.BGMPlayerSeek)->SetLoop(info.BGMPlayerSeek, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN);
		slCheckErrorWithStatus(result, "Problem setting BGM player loop (Error %d).", result);
	}
    result = (*info.BGMPlayer)->SetPlayState(info.BGMPlayer, SL_PLAYSTATE_PLAYING);
    info.state = SL_PLAYSTATE_PLAYING;
    slCheckErrorWithStatus(result, "Problem starting BGMplayer (Error %d).", result);

    result = (*info.BGMPlayerObj)->GetInterface(info.BGMPlayerObj, SL_IID_VOLUME, &info.BGMPlayerVolume);
    slCheckErrorWithStatus(result, "Problem getting SL_IID_VOLUME for BGM player (Error %d).", result);
    result = (*info.BGMPlayerVolume)->SetVolumeLevel(info.BGMPlayerVolume, (float)volume / 100.f * -MINIMUM_VOLUME + MINIMUM_VOLUME);
    slCheckErrorWithStatus(result, "Problem setting VOLUME for BGM player (Error %d).", result);
    _bgms[level] = info;
}
Пример #7
0
void Operation::play_bgm(ScenarioRunner* sr)
{
  playBGM(sr);
  sr->getDWORD();
}