Android可以基于Unity Texture2D创建SurfaceTexture吗?

时间:2016-01-28 13:12:37

标签: java android unity3d stream surface

我的问题与:Can an Android (Java not C/C++) plugin alter unity textures?有关。看起来这个家伙和我一样困扰着同样的问题。我创建了允许在Android设备上启动流的插件,但我无法管理如何在Unity界面元素(MeshRenderer,RawImage)上显示视频帧。

我的Unity代码:

private void InitAndroidStreamerObject()
    {
        androidStreamerObj = new AndroidJavaObject("makeitbetter.figazzz.com.vitamiousing7.AndroidStreamer");
        int androidInt = androidStreamerObj.Call<int>("AndroidInt");

        Debug.Log("androidInt = " + androidInt);
    }

    public void StartStream()
    {
        //"http://dlqncdn.miaopai.com/stream/MVaux41A4lkuWloBbGUGaQ__.mp4"; //"rtmp://live.hkstv.hk.lxdns.com/live/hks";
        string streamLink = "rtmp://live.hkstv.hk.lxdns.com/live/hks";

        System.IntPtr ptr = _inputTexture.GetNativeTexturePtr();
        Debug.Log("ptr on Unity side = " + ptr.ToInt32());
        androidStreamerObj.Call("LaunchStream", streamLink, ptr.ToInt32());
    }

如您所见,我初始化Android对象,获取纹理的原生纹理指针并将其发送到Android插件。

在Android插件中,接下来会发生一些事情:

public void LaunchStream(String streamLink, int unityTextureID) {
        final int texId = unityTextureID;
        final String path = streamLink; 
        Log.i("Unity", "hop hop1 = " + path + " textureId = " + unityTextureID);

        _currActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                //_streamConnection = new VideoView(_currActivity); //(VideoView) findViewById(R.id.stream_window);
                //_currActivity.addContentView(_streamConnection, new FrameLayout.LayoutParams(1, 1));

                _streamConnection.setVideoPath(path);
                _streamConnection.setMediaController(new MediaController(_currActivity));
                _streamConnection.requestFocus();

                _streamConnection.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        Log.i("Unity", "some error, I don't know. what = " + what + " extra = " + extra);
                        return false;
                    }
                });

                _streamConnection.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mediaPlayer.setPlaybackSpeed(1.0f);
                    }
                });

                SurfaceTexture unityTexture = new SurfaceTexture(texId);
                if(unityTexture == null)
                    Log.d("Unity", "unityTexture NULL");
                else
                    Log.d("Unity", "unityTexture is ok");

                _cachedSurface = new Surface(unityTexture);
                if(_cachedSurface == null)
                    Log.d("Unity", "cachedSurface NULL");
                else
                    Log.d("Unity", "cachedSurface is ok");

                _streamConnection.setSurfaceToPlayer(_cachedSurface);
                }
        });
    }

你可能会说这部分代码:

SurfaceTexture unityTexture = new SurfaceTexture(texId);
                if(unityTexture == null)
                    Log.d("Unity", "unityTexture NULL");
                else
                    Log.d("Unity", "unityTexture is ok");

                _cachedSurface = new Surface(unityTexture);
                if(_cachedSurface == null)
                    Log.d("Unity", "cachedSurface NULL");
                else
                    Log.d("Unity", "cachedSurface is ok");

                _streamConnection.setSurfaceToPlayer(_cachedSurface);

不起作用。但它工作得非常好,我试图改变Surface,它没问题。但是,如果我尝试从Unity系统中做同样的事情,那就不是了。 我知道我可以通过低级本机插件接口做我需要做的事情。但到目前为止,我还没有得到如何使用我的Android插件。搜索一些教程我什么也没找到。如果有人能解释我该怎么办,我真的很感激。谢谢

0 个答案:

没有答案