Android NDK Call Java From C++

本文介绍了一种在Android NDK环境下使用C++调用Java代码播放声音的方法。通过实现Java静态方法并在C++中调用,解决了C++直接播放声音的难题。文章详细展示了如何在C++中加载Java虚拟机,获取Java类和方法ID,以及如何调用Java静态方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I search the Android NDK document and Internet a bit, and could not find anything useful to help me play sound with C++ under the Android NDK. Most of topics are about how to play sound with Java, so I grape an idea that why not call Java from C++. I tried along this way, and finally all those stuff works as I expected before.

 

Implement Java static function

A Sound player class implemented in Java class. In addition to play sound basic feature, I also make it expose a static function that will be called from C++. The reason why I used a static function here is that static function could allow me correctly find the right Java object instance, the one with sound pool initialized well and file loaded ok will be the correct one.

public class SoundManager {
    ......
    
    public static void sPlaySound(int sound)
    {
        if ( null != sInstance )
            sInstance.PlaySound(sound);
    }
    
}

 

Call Java function from C/C++
static JavaVM* g_JavaVM = NULL;
static const char *g_JavaClassName = "com/easygame/SoundManager";

JNIEXPORT jint JNI_OnLoad(JavaVM* jvm, void* reserved)
{
    g_JavaVM = jvm;

    JNIEnv *env = NULL;
    if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
        return -1;

    return JNI_VERSION_1_6;
}

JNIEXPORT void JNI_OnUnload(JavaVM* jvm, void* reserved)
{
    g_JavaVM = NULL;
    JNIEnv *env = NULL;
    if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
        return;
}

void SysPlaySound(int soundId)
{
    if (g_JavaVM == NULL)
        return;

    int status = -1;
    JNIEnv *env = NULL;
    bool isAttached = false;

    status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);
    if (status < 0)  
    {
        status = g_JavaVM->AttachCurrentThread(&env, NULL);
        if (status < 0)
            return;
        isAttached = true;
    }

    if ( env != NULL )
    {
        jclass cls = env->FindClass(g_JavaClassName);
        if ( cls != 0 )
        {
            jmethodID mid = env->GetStaticMethodID(cls, "sPlaySound", "(I)V");
            if ( mid != 0 )
                env->CallStaticVoidMethod(cls, mid, soundId);
        }
    }

    if ( isAttached )
        g_JavaVM->DetachCurrentThread();    
}

Function JNI_OnLoad and JNI_OnUnload are Android NDK system call back functions, that will allow us to get the current Java virtual machine. We need to get the class static method or object member function  from the Java virtual machine. Now we could call function SysPlaySound as a C/C++ native function.

 

Reference

http://blog.youkuaiyun.com/airun_zou/article/details/7110101

http://www.cnblogs.com/luxiaofeng54/archive/2011/08/17/2142000.html

http://qfqf16.blog.163.com/blog/static/128109527201281263955386/

http://blog.chinaunix.net/uid-7448773-id-310170.html

转载于:https://www.cnblogs.com/open-coder/archive/2013/01/26/2878364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值