open source
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
@file JCAudioMp3Media.cpp
|
||||
@brief
|
||||
@author dt
|
||||
@version 1.0
|
||||
@date 2014_12_24
|
||||
*/
|
||||
|
||||
|
||||
//包含头文件
|
||||
#include <vector>
|
||||
#include "JCAudioMp3Media.h"
|
||||
#include "util/Log.h"
|
||||
#include "../../CToJavaBridge.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
JCAudioMp3Media::JCAudioMp3Media()
|
||||
{
|
||||
m_nCurrentVolume = 1.0;
|
||||
m_pJSAudio = NULL;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
JCAudioMp3Media::~JCAudioMp3Media( void )
|
||||
{
|
||||
m_pJSAudio = NULL;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::play( const char* p_sUrl,int p_nTimes,float nCurrentTime,JCAudioInterface* p_pJSAudio )
|
||||
{
|
||||
m_pJSAudio = p_pJSAudio;
|
||||
CToJavaBridge::JavaRet ret;
|
||||
CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "playBackgroundMusic", p_sUrl, p_nTimes,(int)(nCurrentTime*1000),ret);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::delAudio( JCAudioInterface* p_pJSAudio )
|
||||
{
|
||||
if( m_pJSAudio == p_pJSAudio){
|
||||
m_pJSAudio = NULL;
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::pause()
|
||||
{
|
||||
CToJavaBridge::JavaRet ret;
|
||||
CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "pauseBackgroundMusic", ret);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::stop()
|
||||
{
|
||||
CToJavaBridge::JavaRet ret;
|
||||
CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "stopBackgroundMusic", ret);
|
||||
m_pJSAudio = NULL;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::resume()
|
||||
{
|
||||
CToJavaBridge::JavaRet ret;
|
||||
CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "resumeBackgroundMusic", ret);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::setVolume( float p_nVolume )
|
||||
{
|
||||
m_nCurrentVolume = p_nVolume;
|
||||
/*if( p_nVolume < -10000 ) p_nVolume = -10000;
|
||||
if( p_nVolume > 0 ) p_nVolume = 0;
|
||||
p_nVolume = (p_nVolume+10000)/10000.0;*/
|
||||
CToJavaBridge::JavaRet ret;
|
||||
CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "setBackgroundMusicVolume",p_nVolume, ret);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::setMute( bool p_bMute )
|
||||
{
|
||||
if( p_bMute == true )
|
||||
{
|
||||
float nTemp = m_nCurrentVolume;
|
||||
setVolume( 0.0f );
|
||||
m_nCurrentVolume = nTemp;
|
||||
}
|
||||
else
|
||||
{
|
||||
setVolume( m_nCurrentVolume );
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Media::onPlayEnd()
|
||||
{
|
||||
if( m_pJSAudio )
|
||||
{
|
||||
m_pJSAudio->onPlayEnd();
|
||||
}
|
||||
}
|
||||
void JCAudioMp3Media::setCurrentTime(double nCurrentTime)
|
||||
{
|
||||
CToJavaBridge::JavaRet ret;
|
||||
CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "setCurrentTime", (float)nCurrentTime, ret);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
double JCAudioMp3Media::getCurrentTime()
|
||||
{
|
||||
CToJavaBridge::JavaRet ret;
|
||||
if (CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "getCurrentTime", ret, CToJavaBridge::JavaRet::RT_Float))
|
||||
{
|
||||
return (double)ret.floatRet;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
double JCAudioMp3Media::getDuration()
|
||||
{
|
||||
CToJavaBridge::JavaRet ret;
|
||||
if (CToJavaBridge::GetInstance()->callMethod("layaair.game.utility.LayaAudioMusic", "getDuration", ret, CToJavaBridge::JavaRet::RT_Float))
|
||||
{
|
||||
return (double)ret.floatRet;
|
||||
}
|
||||
|
||||
return 0.0f;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
@file JCAudioMp3Play.h
|
||||
@brief
|
||||
@author dt
|
||||
@version 1.0
|
||||
@date 2014_12_24
|
||||
*/
|
||||
|
||||
#ifndef __JCAudioMp3Media_H__
|
||||
#define __JCAudioMp3Media_H__
|
||||
|
||||
//包含头文件
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include "resource/Audio/JCMp3Interface.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
class JCAudioMp3Media : public JCMp3Interface
|
||||
{
|
||||
public:
|
||||
|
||||
//构造函数
|
||||
JCAudioMp3Media();
|
||||
|
||||
//析构函数
|
||||
~JCAudioMp3Media( void );
|
||||
|
||||
void play( const char* p_sUrl,int p_nTimes,float nCurrentTime,JCAudioInterface* p_pJSAudio );
|
||||
void delAudio( JCAudioInterface* p_pJSAudio );
|
||||
|
||||
void pause();
|
||||
|
||||
void stop();
|
||||
|
||||
void resume();
|
||||
|
||||
void setVolume( float p_nVolume );
|
||||
|
||||
void setMute( bool p_bMute );
|
||||
|
||||
void onPlayEnd();
|
||||
|
||||
void setCurrentTime(double nCurrentTime);
|
||||
|
||||
double getCurrentTime();
|
||||
|
||||
double getDuration();
|
||||
|
||||
private:
|
||||
|
||||
JCAudioInterface* m_pJSAudio;
|
||||
|
||||
float m_nCurrentVolume;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //__JCAudioMp3Play_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
@file JCAudioMp3Player.cpp
|
||||
@brief
|
||||
@author wyw
|
||||
@version 1.0
|
||||
@date 2012_11_14
|
||||
*/
|
||||
|
||||
|
||||
//包含头文件
|
||||
#include "JCAudioMp3Player.h"
|
||||
#include <android/log.h>
|
||||
#include "../../../util/Log.h"
|
||||
#include "../JCAudioManager.h"
|
||||
|
||||
namespace laya
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
JCAudioMp3Player::JCAudioMp3Player( const char* p_sFilePath )
|
||||
{
|
||||
m_pPlayerObject=NULL;
|
||||
m_pPlayerPlay=NULL;
|
||||
m_pPlayerSeek=NULL;
|
||||
m_pPlayerMuteSolo=NULL;
|
||||
m_pPlayerVolume=NULL;
|
||||
m_sUrlName = p_sFilePath;
|
||||
m_nCurrentVolume = 0;
|
||||
m_bInit = false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
JCAudioMp3Player::~JCAudioMp3Player( void )
|
||||
{
|
||||
shutdown();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void JCAudioMp3Player::shutdown( void )
|
||||
{
|
||||
if( m_pPlayerObject != NULL )
|
||||
{
|
||||
(*m_pPlayerObject)->Destroy(m_pPlayerObject);
|
||||
m_pPlayerObject = NULL;
|
||||
m_pPlayerPlay = NULL;
|
||||
m_pPlayerSeek = NULL;
|
||||
m_pPlayerMuteSolo = NULL;
|
||||
m_pPlayerVolume = NULL;
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::createAudioPlayer( std::string p_sUrl )
|
||||
{
|
||||
if( m_bInit == true ) return true;
|
||||
m_bInit = true;
|
||||
SLresult pResult;
|
||||
|
||||
//设置数据来源
|
||||
SLchar* sUrl = (SLchar*)p_sUrl.c_str();
|
||||
SLDataLocator_URI kLocUri = { SL_DATALOCATOR_URI, sUrl };
|
||||
SLDataFormat_MIME kFormatMime = { SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED };
|
||||
SLDataSource kAudioSrc = { &kLocUri, &kFormatMime };
|
||||
|
||||
//sink
|
||||
#ifdef ANDROIDSLES
|
||||
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, JCAudioManager::GetInstance()->getMixObject() };
|
||||
SLDataSink audioSnk = {&loc_outmix, NULL};
|
||||
#endif
|
||||
//创建声音
|
||||
const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME};
|
||||
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
|
||||
#ifdef ANDROIDSLES
|
||||
pResult = (*JCAudioManager::GetInstance()->getEngine())->CreateAudioPlayer( JCAudioManager::GetInstance()->getEngine(), &m_pPlayerObject, &kAudioSrc,&audioSnk, 3, ids, req );
|
||||
#endif
|
||||
if( pResult != SL_RESULT_SUCCESS )
|
||||
{
|
||||
LOGE("createAudioPlayer err=CreateAudioPlayer");
|
||||
return false;
|
||||
}
|
||||
|
||||
//realize播放对象
|
||||
pResult = (*m_pPlayerObject)->Realize(m_pPlayerObject, SL_BOOLEAN_FALSE);
|
||||
if( pResult != SL_RESULT_SUCCESS )
|
||||
{
|
||||
(*m_pPlayerObject)->Destroy( m_pPlayerObject );
|
||||
m_pPlayerObject = NULL;
|
||||
LOGE("createAudioPlayer err=RealizePlayer");
|
||||
return false;
|
||||
}
|
||||
|
||||
//获得play
|
||||
pResult = (*m_pPlayerObject)->GetInterface(m_pPlayerObject, SL_IID_PLAY, &m_pPlayerPlay);
|
||||
if( pResult != SL_RESULT_SUCCESS )
|
||||
{
|
||||
LOGE("createAudioPlayer err=GetPlayer");
|
||||
return false;
|
||||
}
|
||||
|
||||
//获得seek
|
||||
pResult = (*m_pPlayerObject)->GetInterface(m_pPlayerObject, SL_IID_SEEK, &m_pPlayerSeek);
|
||||
if( pResult != SL_RESULT_SUCCESS )
|
||||
{
|
||||
LOGE("createAudioPlayer err=GetSeek");
|
||||
return false;
|
||||
}
|
||||
|
||||
//获得mutesolo
|
||||
pResult = (*m_pPlayerObject)->GetInterface(m_pPlayerObject, SL_IID_MUTESOLO, &m_pPlayerMuteSolo);
|
||||
if( pResult != SL_RESULT_SUCCESS )
|
||||
{
|
||||
LOGE("createAudioPlayer err=GetMuteSolo");
|
||||
return false;
|
||||
}
|
||||
|
||||
//获得设置音量的
|
||||
pResult = (*m_pPlayerObject)->GetInterface(m_pPlayerObject, SL_IID_VOLUME, &m_pPlayerVolume);
|
||||
if( pResult != SL_RESULT_SUCCESS )
|
||||
{
|
||||
LOGE("createAudioPlayer err=GetVolume");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::setPlayingAudioPlayer( short p_nState )
|
||||
{
|
||||
if( m_bInit == false ) createAudioPlayer( m_sUrlName );
|
||||
if (m_pPlayerPlay != NULL )
|
||||
{
|
||||
if( p_nState == SL_PLAYSTATE_PLAYING )
|
||||
{
|
||||
(*m_pPlayerPlay)->SetPlayState( m_pPlayerPlay, SL_PLAYSTATE_STOPPED );
|
||||
(*m_pPlayerPlay)->SetPlayState( m_pPlayerPlay, SL_PLAYSTATE_PLAYING );
|
||||
}
|
||||
else
|
||||
{
|
||||
(*m_pPlayerPlay)->SetPlayState( m_pPlayerPlay, p_nState );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::setLoopingAudioPlayer( bool p_bLoop )
|
||||
{
|
||||
if( m_bInit == false ) createAudioPlayer( m_sUrlName );
|
||||
if ( m_pPlayerSeek != NULL )
|
||||
{
|
||||
(*m_pPlayerSeek)->SetLoop(m_pPlayerSeek, (SLboolean) p_bLoop, 0,SL_TIME_UNKNOWN);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::setChannelMuteAudioPlayer( int p_nChannel,bool p_nMute )
|
||||
{
|
||||
if( m_bInit == false ) createAudioPlayer( m_sUrlName );
|
||||
if ( m_pPlayerMuteSolo != NULL )
|
||||
{
|
||||
(*m_pPlayerMuteSolo)->SetChannelMute( m_pPlayerMuteSolo, p_nChannel, p_nMute );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::setChannelSoloAudioPlayer( int p_nChannel,bool p_nSolo )
|
||||
{
|
||||
if( m_bInit == false ) createAudioPlayer( m_sUrlName );
|
||||
if ( m_pPlayerMuteSolo != NULL )
|
||||
{
|
||||
(*m_pPlayerMuteSolo)->SetChannelSolo( m_pPlayerMuteSolo, p_nChannel, p_nSolo );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::setVolumeAudioPlayer( int p_nVolume )
|
||||
{
|
||||
if( m_bInit == false ) createAudioPlayer( m_sUrlName );
|
||||
m_nCurrentVolume = p_nVolume;
|
||||
if ( m_pPlayerVolume != NULL )
|
||||
{
|
||||
(*m_pPlayerVolume)->SetVolumeLevel(m_pPlayerVolume, p_nVolume );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
bool JCAudioMp3Player::setMute( bool p_bMute )
|
||||
{
|
||||
if( m_bInit == false ) createAudioPlayer( m_sUrlName );
|
||||
if ( m_pPlayerVolume != NULL )
|
||||
{
|
||||
(*m_pPlayerVolume)->SetMute(m_pPlayerVolume, p_bMute );
|
||||
if( p_bMute == true )
|
||||
{
|
||||
(*m_pPlayerVolume)->SetVolumeLevel(m_pPlayerVolume, -10000 );
|
||||
}
|
||||
else
|
||||
{
|
||||
(*m_pPlayerVolume)->SetVolumeLevel(m_pPlayerVolume, m_nCurrentVolume );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
@file JCAudioMp3Play.h
|
||||
@brief
|
||||
@author wyw
|
||||
@version 1.0
|
||||
@date 2012_11_14
|
||||
*/
|
||||
|
||||
#ifndef __JCAudioMp3Player_H__
|
||||
#define __JCAudioMp3Player_H__
|
||||
|
||||
//包含头文件
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <SLES/OpenSLES.h>
|
||||
#include <SLES/OpenSLES_Android.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace laya
|
||||
{
|
||||
/**
|
||||
* @brief
|
||||
*/
|
||||
class JCAudioMp3Player
|
||||
{
|
||||
public:
|
||||
|
||||
//构造函数
|
||||
JCAudioMp3Player( const char* p_sFilePath );
|
||||
|
||||
//析构函数
|
||||
~JCAudioMp3Player( void );
|
||||
|
||||
//释放资源
|
||||
void shutdown( void );
|
||||
|
||||
//创建player
|
||||
bool createAudioPlayer( std::string p_sUrl );
|
||||
|
||||
//设置播放
|
||||
bool setPlayingAudioPlayer( short p_nState );
|
||||
|
||||
//设置是否循环
|
||||
bool setLoopingAudioPlayer( bool p_bLoop );
|
||||
|
||||
//设置左右声道的静音
|
||||
bool setChannelMuteAudioPlayer( int p_nChannel,bool p_nMute );
|
||||
|
||||
//设置左右声道是否独唱
|
||||
bool setChannelSoloAudioPlayer( int p_nChannel,bool p_nSolo );
|
||||
|
||||
//设置音量
|
||||
bool setVolumeAudioPlayer( int p_nVolume );
|
||||
|
||||
//设置静音
|
||||
bool setMute( bool p_bMute );
|
||||
|
||||
protected:
|
||||
|
||||
bool m_bInit; //是否初始化
|
||||
|
||||
std::string m_sUrlName; //url
|
||||
|
||||
SLObjectItf m_pPlayerObject; //播放声音的object
|
||||
|
||||
SLPlayItf m_pPlayerPlay; //播放用的
|
||||
|
||||
SLSeekItf m_pPlayerSeek; //设置是否循环用的
|
||||
|
||||
SLMuteSoloItf m_pPlayerMuteSolo; //设置静音 独唱等用的
|
||||
|
||||
SLVolumeItf m_pPlayerVolume; //设置音量用的
|
||||
|
||||
int m_nCurrentVolume; //当前音量,为了解决三星设置静音不好用的问题
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //__JCAudioMp3Play_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
Reference in New Issue
Block a user