Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a unity project and I use a Android (java) plugin to get camera data. I draw this on a TextureView. I want to hide/show this view when I press a button in unity. But my app crashes when I setVisibility

onCreate

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //Reset data
    ResetData();

    //Create camera preview
    _camPreview = new CameraPreview();

    //Create texture view
    TextureView texView = new TextureView(this);

    //Initialize camera texutre
    _camPreview.Init(texView);

    //Add the texture view to unity
    UnityPlayer.currentActivity.addContentView(texView, new FrameLayout.LayoutParams(400, 400));
}

Camera Preview class:

public void Init(TextureView texView)
{
    //Save texture view
    _TextureView = texView;

    //Let this class listen to events of the surface
    _TextureView.setSurfaceTextureListener(this);
}

public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
    //Get camera
    _camera = Camera.open();
    _camera.setDisplayOrientation(90);

    //Set texture view data
    _TextureView.setLayoutParams(new FrameLayout.LayoutParams(425, 425, Gravity.CENTER));
    _TextureView.setTranslationY(-70);

    //Set dummy texture
    try {
        _camera.setPreviewTexture(surface);
    }
    catch (IOException e)
    {
        Log.e("Unity", "Error surface");
    }

    //Get parameters
    Camera.Parameters parameters = _camera.getParameters();

    //Set JPEG
    parameters.setPreviewFormat(ImageFormat.JPEG);

    //Set image size
    parameters.setPreviewSize(480, 320);

    //Set new parameters
    _camera.setParameters(parameters);
}

public void HideVideo()
{
    //Hide view
    _TextureView.setVisibility(View.INVISIBLE);
}

Is there an extra function I need to call, or may I only call it on certain times?

None of these thins work, they all make my app crash.

    _TextureView.setVisibility(View.INVISIBLE);
    _TextureView.setActivated(false);
    _TextureView.setAlpha(0);
    _TextureView.setTranslationY(-1000);
share|improve this question
Have you used the debugger? Are the objects initialized? What's the exact error you're getting? – Byte56 Oct 1 '12 at 13:26
I cannot use the debugger since it is an unity app. Unity runs the java code not eclipse, so no debugging (except string outputs), nothing is null, I checked. And all is initialized properly, I know because now it works with a little work around. In 9 min I can post my work around. – Kazoeja Oct 1 '12 at 15:21
Unity has a debugger. – Jonathan Hobbs Oct 1 '12 at 22:35

2 Answers

I suppose your _TextureView object is null;You have to initialize it correctly;

share|improve this answer
It is not null, because I see it, so that means it is initialized. The camera feedback is drawing on the _TextureView. – Kazoeja Oct 1 '12 at 11:06
Where did you initialize your _TextureView object? – VinceFR Oct 1 '12 at 11:31
It is posted in the question now. I create it in the onCreate of my activity. – Kazoeja Oct 1 '12 at 11:33
I see the initialization of texView not _TextureView – VinceFR Oct 1 '12 at 11:34
texView is passed to Init of CameraPreview class, wich saves it to _TextureView. – Kazoeja Oct 1 '12 at 11:36
show 5 more comments

I have made a work around, now I set a boolean and in the onSurfaceTextureUpdated callback I set the alpha.

public void onSurfaceTextureUpdated(SurfaceTexture surface)
{
    //Visibility changed
    if(_isVisibleChanged == true)
    {
        //Show video
        if(_isVisible)
            _TextureView.setAlpha(1);

        //Hide video
        else
            _TextureView.setAlpha(0);

        //Visibility updated
        _isVisibleChanged = false;
    }
}

In any other function it crashes accept there.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.