0

I am following the documentation for FFmpeg here and I am wondering what I should put as the context?

My function

public static void conversion(String[] cmd) {

FFmpeg ffmpeg = FFmpeg.getInstance(context); //what should I put as the context here?

try {


  // to execute "ffmpeg -version" command you just need to pass "-version"
  ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

    @Override
    public void onStart() {
    }

    @Override
    public void onProgress(String message) {
    }

    @Override
    public void onFailure(String message) {
    }

    @Override
    public void onSuccess(String message) {
    }

    @Override
    public void onFinish() {
    }
  });
} catch (FFmpegCommandAlreadyRunningException e) {
  // Handle if FFmpeg is already running
  e.printStackTrace();
}
}

And then I call my function like this (from the same class):

public void alert(String message) {
      String[] cmd = {"-i"
              , message
              , "Image.gif"};
      conversion(cmd);
  }
Community
  • 1
  • 1
zoecarver
  • 5,523
  • 2
  • 26
  • 56
  • I'd use the `Application` context, as they (probably) are not doing anything with the `Context` that involves the UI, and they might be using this to initialize a singleton (so any other `Context` would represent a memory leak). But, I haven't used this library, so I don't know if that approach causes any problems. – CommonsWare Aug 06 '17 at 15:58

2 Answers2

1

If you are writing that in an Activity, you can pass the Activity instance by passing this. Or if the context instance will outlive the lifetime of the Activity, you can pass Application context as this.getApplicationContext()

FFmpeg.getInstance(this);

FFmpeg.getInstance(this.getApplicationContext());

Update:

public class AndroidApplication extends Application {

    private static AndroidApplication sInstance;


    public static AndroidApplication getInstance() {
      return sInstance;
    }

    @Override
    public void onCreate() {
      super.onCreate();  
      sInstance = this;
    }


}

In your AndroidManifest.xml file, add this line in the application tag:

android:name="yourPackage.AndroidApplication"

Now you can pass the AndroidApplication.getInstance() as context.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • Thank you so much for the help! However when I ran your code I got an error: `cannot be referenced from a static context` – zoecarver Aug 06 '17 at 16:01
  • Perhaps you have that method inside a static method. Where are you calling this method? In an Activity? – Bob Aug 06 '17 at 16:04
  • Can you remove that static keyword for the method? – Bob Aug 06 '17 at 16:05
  • alright sure, Sorry about the confusion - I am very new to android development. I have updated my question. – zoecarver Aug 06 '17 at 16:06
  • now I am getting `Error:(37, 40) error: incompatible types: RNGifMakerModule cannot be converted to Context` any ideas? thank you so much for helping me – zoecarver Aug 06 '17 at 16:07
  • Is `RNGifMakerModule` is the name of the class? From where are calling the method `conversion()` – Bob Aug 06 '17 at 16:09
  • Yes, the class is `RNGifMAkerModlue`. Would it be helpful if I just posted the whole script? – zoecarver Aug 06 '17 at 16:11
  • Follow this tutorial: https://androidcookbook.com/Recipe.seam?recipeId=1218 . And subclass the Application class. Pass that sInstance from the Application class as the context. – Bob Aug 06 '17 at 16:12
  • Thank you! will do. – zoecarver Aug 06 '17 at 16:14
  • Thank you so much! That was very helpful, I have followed what you said, and it worked! Now I have one more question (sorry). I am getting this error: `Error:(43, 31) error: cannot find symbol class ExecuteBinaryResponseHandler` any idea as to why/how to fix? – zoecarver Aug 07 '17 at 02:26
  • It looks like a different issue. Would you mind posting it as a separate question with necessary code and error messages? – Bob Aug 07 '17 at 07:48
  • Sure, Here is is: https://stackoverflow.com/questions/45549701/ffmpeg-cannot-find-executebinaryresponsehandler-android-java thanks again for all the help! – zoecarver Aug 07 '17 at 14:45
0

context should be your activity context or application context. If you are using it inside a fragment, you can do getActivity() which will return activity context. If you are using it inside Activity you can do YOUR_ACTIVITY_NAME.this

Kapil G
  • 4,081
  • 2
  • 20
  • 32