【问题标题】:What should I put as the context - FFmpeg - Android/Java我应该把什么作为上下文 - FFmpeg - Android/Java
【发布时间】:2017-08-07 00:16:18
【问题描述】:

我正在关注the documentation for FFmpeg here,我想知道我应该把什么作为上下文?

我的功能

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();
}
}

然后我这样调用我的函数(来自同一个类):

public void alert(String message) {
      String[] cmd = {"-i"
              , message
              , "Image.gif"};
      conversion(cmd);
  }

【问题讨论】:

  • 我会使用 Application 上下文,因为他们(可能)没有对涉及 UI 的 Context 做任何事情,他们可能正在使用它来初始化单例(所以任何其他Context 表示内存泄漏)。但是,我没有使用过这个库,所以我不知道这种方法是否会导致任何问题。

标签: java android ffmpeg


【解决方案1】:

如果您将其写入Activity,则可以通过传递this 来传递Activity 实例。或者,如果 context 实例的寿命将超过 Activity 的生命周期,您可以将 Application 上下文传递为 this.getApplicationContext()

FFmpeg.getInstance(this);

FFmpeg.getInstance(this.getApplicationContext());

更新:

public class AndroidApplication extends Application {

    private static AndroidApplication sInstance;


    public static AndroidApplication getInstance() {
      return sInstance;
    }

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


}

在您的AndroidManifest.xml 文件中,将这一行添加到application 标记中:

android:name="yourPackage.AndroidApplication"

现在您可以将AndroidApplication.getInstance() 传递为context

【讨论】:

  • 非常感谢您的帮助!但是,当我运行您的代码时,出现错误:cannot be referenced from a static context
  • 也许你在静态方法中有那个方法。你在哪里调用这个方法?在活动中?
  • 你能删除该方法的静态关键字吗?
  • 好吧,很抱歉造成混乱 - 我对 android 开发非常陌生。我已经更新了我的问题。
  • 现在我得到Error:(37, 40) error: incompatible types: RNGifMakerModule cannot be converted to Context 任何想法?非常感谢你帮助我
【解决方案2】:

context 应该是您的活动上下文或应用程序上下文。如果您在片段中使用它,您可以执行getActivity(),这将返回活动上下文。如果你在 Activity 中使用它,你可以这样做YOUR_ACTIVITY_NAME.this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2021-12-18
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多