Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is the better between using static var and passing object parameter?

Tags:

java

android

I want to write an APP to record screen, there are two way, RecordHelper_Method_A and RecordHelper_Method_B .

In RecordHelper_Method_A, I define mMediaRecorder, MediaProjection mMediaProjection and mVirtualDisplay as static var, it's easy to invoke, such as StartRecord( mContext, requestCode, resultCode,data), StopRecord().

and in RecordHelper_Method_B, I need to define mMediaRecorder, MediaProjection mMediaProjection in main Activity class, and pass the parameters when I invoke StartRecord(mMediaRecorder, mMediaProjection,mVirtualDisplay), 'StopRecord(mMediaRecorder,mMediaProjection,mVirtualDisplay)`..., it's a little complex.

I don't know which one is the better, and more I don't know if these static var can be released correctly in RecordHelper_Method_A when I end the APP.

BTW, if you have the better way, would you please tell me ? Thanks!

RecordHelper_Method_A

public class RecordHelper_Method_A {

    private static MediaRecorder mMediaRecorder;
    private static MediaProjection mMediaProjection;
    private static VirtualDisplay mVirtualDisplay;

    public static void StartRecord(Context mContext,int requestCode, int resultCode, Intent data){

        mMediaRecorder = new MediaRecorder();
        initRecorder();

        MediaProjectionManager mProjectionManager = (MediaProjectionManager) mContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);

        mVirtualDisplay=mMediaProjection.createVirtualDisplay("MainActivity",
                400,600, 300,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                mMediaRecorder.getSurface(), null, null);

        MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback();
        mMediaProjection.registerCallback(mMediaProjectionCallback, null);

        mMediaRecorder.start();
    }

    public static void StopRecord(){
        mMediaProjection=null;
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mVirtualDisplay.release();
    }


    private static void initRecorder() {
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        //...
    }

    private static class MediaProjectionCallback extends MediaProjection.Callback {
        @Override
        public void onStop() {
            mMediaRecorder.stop();
        }
    }
}

RecordHelper_Method_B

public class RecordHelper_Method_B {

    public static void StartRecord(MediaRecorder mMediaRecorder,MediaProjection mMediaProjection,VirtualDisplay mVirtualDisplay){

        initRecorder(mMediaRecorder);

        mVirtualDisplay=mMediaProjection.createVirtualDisplay("MainActivity",
                400,600, 300,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                mMediaRecorder.getSurface(), null, null);

        MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback(mMediaRecorder);
        mMediaProjection.registerCallback(mMediaProjectionCallback, null);

        mMediaRecorder.start();
    }

    public static void StopRecord(MediaRecorder mMediaRecorder,MediaProjection mMediaProjection,VirtualDisplay mVirtualDisplay){
        mMediaProjection=null;
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mVirtualDisplay.release();
    }


    private static void initRecorder(MediaRecorder mMediaRecorder) {
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        //...
    }

    private static class MediaProjectionCallback extends MediaProjection.Callback {
        MediaRecorder mMediaRecorder;
        public MediaProjectionCallback(MediaRecorder mMediaRecorder){
            this.mMediaRecorder=mMediaRecorder;
        }

        @Override
        public void onStop() {
            mMediaRecorder.stop();
        }
    }

}
like image 653
HelloCW Avatar asked Oct 22 '15 09:10

HelloCW


People also ask

Are static variables more efficient?

Using static variables may make a function a tiny bit faster. However, this will cause problems if you ever want to make your program multi-threaded. Since static variables are shared between function invocations, invoking the function simultaneously in different threads will result in undefined behaviour.

Why static variable should not be used?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.

When should I use static variables?

use static variables when : The value of the variable is independent of the objects (not unique for each object). E.g. number of students. Show activity on this post. Static variable: When you need something that will be used through out the application and every instance need to know the variable.

What is a benefit of static variable?

Benefits of static variables: constants can be defined without taking additional memory (one for each class) constants can be accessed without an instantiation of the class.


1 Answers

I would suggest Objects not living statically. Whatever object or resource that you create, should die when the activity dies. So creating these resources in the activity and then passing it is good.

Also, I see that in both of the methods that you suggested, the methods are themselves static. It's better to create an object for the class and then pass the resources to this object. This would prevent accidental memory leaks. You could do something like this in your Activity.

RecordHelper_Method_B helper = new RecordHelper_Method_B ();
helper.StopRecord(mMediaRecorder,mMediaProjection,mVirtualDisplay);

This keeps things simple. Once your Activity dies, so will the helper object. So all resources will be released for sure.

like image 88
Henry Avatar answered Sep 20 '22 15:09

Henry