在android中使用ffmpeg播放视频

时间:2017-02-14 12:41:21

标签: java android ffmpeg

我正在尝试在android中使用ffmpeg。这是迄今为止的代码。我参考了this项目。它只是让我转换视频文件。但我想使用ffmpeg播放视频文件。可能吗?如果是,那么我们如何做到这一点?

    package com.ffmpeg;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {


    Boolean loadedFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!loadedFlag) {
            FFmpegInitLoader();
        }
        decodeVideo();
    }

    public void FFmpegInitLoader() {
        FFmpeg ffmpeg = FFmpeg.getInstance(this);
        try {
            ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
                @Override
                public void onStart() {
                }

                @Override
                public void onFailure() {
                }

                @Override
                public void onSuccess() {
                    System.out.println("Successfully loaded FFmpeg!!!");
                    loadedFlag = true;
                }

                @Override
                public void onFinish() {
                }
            });
        } catch (FFmpegNotSupportedException e) {
            System.out.println("Whatever....this thing is not supported :::::::::::::::::::: ");
        }
    }

    public void decodeVideo() {
        FFmpeg ffmpeg = FFmpeg.getInstance(this);
        try {
            ffmpeg.execute(new String[]{"-y", "-i", "/storage/sdcard0/AVSEQ02.mp4", "-c:v", "libx264", "/storage/sdcard0/conv.mp4"}, new ExecuteBinaryResponseHandler() {
                @Override
                public void onStart() {
                    System.out.println("FFmpeg started for decoding");
                }

                @Override
                public void onProgress(String message) {
                    System.out.println("progress message:::: " + message);
                }

                @Override
                public void onFailure(String message) {
                    System.out.println("failure message:::: " + message);
                }

                @Override
                public void onSuccess(String message) {
                    System.out.println("success message:::: " + message);
                }

                @Override
                public void onFinish() {
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            System.out.println("already running::::::");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

是的,有可能。请参阅以下链接。

AndroidFFmpeg

FFmpegMediaPlayer