为cocos2d-x添加调节视频音量的功能(Android)

为cocos2d-x添加调节视频音量的功能(Android)


日常吐槽cocos。我想放广告的时候播放一些其他声音,这时候我希望视频先静音,但是不停止播放。等声音播放完,我再放视频声音。赫然发现,VideoPlayer 居然没这个接口。
而且现在视频的播放居然还没用上跨平台库,而是每个平台自己实现了一套。。神坑。。。
废话完毕,步骤如下:
1.打开UIVideoPlayer.h 为此类添加一个函数
virtual void setVolume(int vol); //至于放哪里?随你喜欢。。希望你有基本的c++知识。


2.UIVideoPlayer-android.cpp

void setVideoVolumeJNI(int index,int vol)
{
	JniMethodInfo t;

	if (JniHelper::getStaticMethodInfo(t,CLASS_NAME,"setVideoVolume","(II)V")) {
		//cocos2d::log("setVideoVolumeJNI Found");
		t.env->CallStaticVoidMethod(t.classID,t.methodID,index,vol);

		t.env->DeleteLocalRef(t.classID);
	}
	else 
	{
		cocos2d::log("setVideoVolumeJNI Not !!!!!!!Found!!!!!!!!");
	}
}
void VideoPlayer::setVolume(int vol) 
{
	//if (!_videoURL.empty())
	{
		//cocos2d::log("VideoPlayer::setVolume %d",_videoPlayerIndex);
		setVideoVolumeJNI(_videoPlayerIndex,vol);
	}
}


3.Cocos2dxVideoHelper.java line69

private final static int VideoTaskVolume = 15;

line109

case VideoTaskVolume:
            {
                Log.i("hiiiiiiiiiiiiiiisetVideoVolume","setVideoVolume is setting to"+msg.arg1);
                Cocos2dxVideoHelper helper = mReference.get();
                helper._setVideoVolume(msg.arg1,msg.arg2);
                break;
            }
line 278
 public static void setVideoVolume(int index,int volume)
    {
        Message msg = new Message();
        msg.what = VideoTaskVolume;
        msg.arg1 = index;
        msg.arg2 = volume;
        mVideoHandler.sendMessage(msg);
    }
    
     private void _setVideoVolume(int index,int vol) {
        Cocos2dxVideoView videoView = sVideoViews.get(index);
        if (videoView != null) {
            videoView.setVideoVolume(vol);
        }
    }

Cocos2dxVideoView.java line120
    public void setVideoVolume(int vol)
    {
        if(mMediaPlayer != null)
        {
            Log.i(TAG,"setVideoVolume is setting to"+vol);
            mMediaPlayer.setVolume(vol,vol);
        }else
        {
            Log.i(TAG,"I want to setVideoVolume but mediaplayer is null...");
        }
          
    }
因为我只有音量开关的需求 ,并没有调节大小的需求,所以我传的INT,事实上我不知道传float的IV值是什么。。请大大告知。音量值 这里只能在0.0 -1.0之间。另外,请务必源码级工程下搜索并修改这些文件。如果是预编译的工程,因为你没有编译c++库,所以是无效的。上班偷偷分享不容易。简略。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...