如何使用统一原生方法

时间:2016-07-06 01:12:29

标签: c# unity3d unityscript

我想使用unity native方法调用mediaplayer setOnCompletionListener,如下所示:

 mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
@Override 
        public void onCompletion(MediaPlayer mp) { 
            Log.i(LOGTAG, "onComplete hit"); 
            mp.stop(); 
            mp.release(); 
        } 
}); 

我知道我可以把上面的代码放在android上,然后生成jar,然后把它放到Unity项目中。但我不想那样,我喜欢

    AndroidJavaObject mediaPlayer = new AndroidJavaObject("android/media/MediaPlayer");



    IntPtr setSurfaceMethodId = AndroidJNI.GetMethodID(mediaPlayer.GetRawClass(),"setSurface","(Landroid/view/Surface;)V");
    jvalue[] parms = new jvalue[1];
    parms[0] = new jvalue();
    parms[0].l = androidSurface;


    AndroidJNI.CallVoidMethod(mediaPlayer.GetRawObject(), setSurfaceMethodId, parms); 

    mediaPlayer.Call("setDataSource", filePath);

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题:

public class CompletionCallback:AndroidJavaProxy {

    public CompletionCallback()
    :base("android.media.MediaPlayer$OnCompletionListener") {
    }

    void onCompletion(AndroidJavaObject mp) {
        CommonUtil.showToast("finish");
    }
}

使用AndroidJavaProxy