java.io.IOException:运行exec时出错()工作目录:null环境:null

时间:2016-05-12 11:33:58

标签: java android ffmpeg ioexception

我在应用程序中使用WritingMinds/ffmpeg-android-java

这是我的代码

loadFFmpeg();
String cmd="ffmpeg -i /storage/emulated/0/media/audio/a.mp3 -i  /storage/emulated/0/recording.3gp -filter_complex \"[0:a][1:a]amerge=inputs=2[aout]\" -map \"[aout]\" " + outputFile;
executeFFmpeg(cmd.split(" "));

private void loadFFmpeg() {
    FFmpeg ffmpeg = FFmpeg.getInstance(MainActivity.this.getApplicationContext());
    try {
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onFailure() {}

            @Override
            public void onSuccess() {}

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegNotSupportedException e) {
        // Handle if FFmpeg is not supported by device
    }
}

private void executeFFmpeg(String[] cmd)
{
    /*String workFolder = getApplicationContext().getFilesDir() + "/ffmpeg";
    String environment = Environment.getExternalStorageDirectory().getAbsolutePath();
    Map<String, String> map = new HashMap<String, String>();
    map.put("Working Directory", workFolder);
    map.put("Environment",environment);*/
    FFmpeg ffmpeg = FFmpeg.getInstance(MainActivity.this.getApplicationContext());
    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() {
                stop.setEnabled(false);
                play.setEnabled(true);
            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // Handle if FFmpeg is already running
    }
}

但我收到了以下错误

  

6784-6962 / com.flipartstudio.playandrecord E / FFmpeg:尝试运行时出现异常:[Ljava.lang.String; @ 41803270                                                                          java.io.IOException:运行exec()时出错。命令:[/ data / data / com.flipartstudio.playandrecord/files/ffmpeg,/ system / bin / ls,-l,/ data / data / com.example.foo / files / fffeeg]工作目录:null环境:null                                                                              在java.lang.ProcessManager.exec(ProcessManager.java:211)                                                                              在java.lang.Runtime.exec(Runtime.java:168)                                                                              在java.lang.Runtime.exec(Runtime.java:123)                                                                              在com.github.hiteshsondhi88.libffmpeg.ShellCommand.run(ShellCommand.java:10)                                                                              at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:38)                                                                              at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:10)                                                                              在android.os.AsyncTask $ 2.call(AsyncTask.java:287)                                                                              在java.util.concurrent.FutureTask.run(FutureTask.java:234)                                                                              在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)                                                                              at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)                                                                              在java.lang.Thread.run(Thread.java:856)                                                                           引起:java.io.IOException:没有这样的文件或目录                                                                              在java.lang.ProcessManager.exec(本机方法)                                                                              在java.lang.ProcessManager.exec(ProcessManager.java:209)                                                                              在java.lang.Runtime.exec(Runtime.java:168)                                                                              在java.lang.Runtime.exec(Runtime.java:123)                                                                              在com.github.hiteshsondhi88.libffmpeg.ShellCommand.run(ShellCommand.java:10)                                                                              at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:38)                                                                              at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:10)                                                                              在android.os.AsyncTask $ 2.call(AsyncTask.java:287)                                                                              在java.util.concurrent.FutureTask.run(FutureTask.java:234)                                                                              在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)                                                                              at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)                                                                              在java.lang.Thread.run(Thread.java:856)

我还添加了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />

3 个答案:

答案 0 :(得分:4)

您的问题是,您在 loadFFmpeg()方法中进行了一次FFmpeg,在 executeFFmpeg()中进行了一次。所以在 executeFFmpeg()中,您的FFmpeg未加载。

解决方案:

你的代码应该是这样的:

public class Main extends Activity{

FFmpeg ffmpeg;
Context context;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;

    loadFFmpeg();
    String cmd="ffmpeg -i /storage/emulated/0/media/audio/a.mp3 -i  /storage    /emulated/0/recording.3gp -filter_complex \"[0:a][1:a]amerge=inputs=2[aout]\" -map \"[aout]\" " + outputFile;
    executeFFmpeg(cmd.split(" "));

}

private void loadFFmpeg() {
    ffmpeg = FFmpeg.getInstance(context);
    try {
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onFailure() {}

            @Override
            public void onSuccess() {}

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegNotSupportedException e) {
        // Handle if FFmpeg is not supported by device
    }
}

private void executeFFmpeg(String[] cmd)
{
    try {
        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() {
                stop.setEnabled(false);
                play.setEnabled(true);
            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // Handle if FFmpeg is already running
    }
}
}

答案 1 :(得分:2)

Try this:

public class MainActivity extends Activity{

FFmpeg ffmpeg;
Context context;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;

    loadFFmpeg();
    String cmd="-i /storage/emulated/0/media/audio/a.mp3 -i  /storage    /emulated/0/recording.3gp -filter_complex \"[0:a][1:a]amerge=inputs=2[aout]\" -map \"[aout]\" " + outputFile;
    executeFFmpeg(cmd.split(" "));

}

private void loadFFmpeg() {
    ffmpeg = FFmpeg.getInstance(context);
    try {
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onFailure() {}

            @Override
            public void onSuccess() {}

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegNotSupportedException e) {
        // Handle if FFmpeg is not supported by device
    }
}

private void executeFFmpeg(String[] cmd)
{
    try {
        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() {
                stop.setEnabled(false);
                play.setEnabled(true);
            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // Handle if FFmpeg is already running
    }
}
}

答案 2 :(得分:0)

原因
ffmpeg.loadBinary 异步。它会立即返回,但此时任务尚未完成。

解决方案
您应该将代码放在onSuccess() ffmpeg.loadBinary中,如下所示:

ffmpeg = FFmpeg.getInstance(context);
    try {
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onFailure() {}

            @Override
            public void onSuccess() {
                String cmd="ffmpeg -i /storage/emulated/0/media/audio/a.mp3 -i  /storage/emulated/0/recording.3gp -filter_complex \"[0:a][1:a]amerge=inputs=2[aout]\" -map \"[aout]\" " + outputFile;
                executeFFmpeg(cmd.split(" "));
            }

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegNotSupportedException e) {
        // Handle if FFmpeg is not supported by device
    }