使用mp4parser合并相机视频和mp3但在android中出错

时间:2017-06-09 13:23:07

标签: java android android-videoview android-audiorecord mp4parser


首先打开相机并在打开下一个活动和音频文件列表后捕获10秒视频然后单击音频文件这次合并音频和视频但是在我的代码中获取错误时选择音频存储。
活动

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
import com.coremedia.iso.boxes.Container;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.wos.capturevideowithaudio.R;
import com.wos.capturevideowithaudio.adapter.AudioListAdapter;
import com.wos.capturevideowithaudio.utils.Constant;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;

public class AudioMixing extends AppCompatActivity {
private ArrayList<String> audio;
private VideoView mVideoView;
private RecyclerView rvAudioFile;
private AudioListAdapter audioListAdapter;
private String musicUri, timeStamp;
private String[] array_spinnerLoad, files;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audiomixing);
    initView();


}


private void initView() {
    rvAudioFile = (RecyclerView) findViewById(R.id.rvAudioFile);
    mVideoView = (VideoView) findViewById(R.id.video_view);

    //Display Current Capture Video
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        timeStamp = bundle.getString("timeStamp");
        musicUri = bundle.getString("uri");
        mVideoView.setVideoURI(Uri.parse(musicUri));
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();
        mVideoView.start();
    }

    //Get All AudioList in Mobile Phone
    audio = new ArrayList<>();

    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
    File sd = new File(path);
    File[] sdDirList = sd.listFiles();
    if (sdDirList != null) {
        array_spinnerLoad = new String[sdDirList.length];
        files = new String[sdDirList.length];
        for (int i = 0; i < sdDirList.length; i++) {
            array_spinnerLoad[i] = sdDirList[i].getName();
            Log.d(Constant.TAG, "getAudioName = " + sdDirList[i].getName());
            files[i] = sdDirList[i].getAbsolutePath();
            Log.d(Constant.TAG, "getAudioName = " + sdDirList[i].getAbsolutePath());
        }

        audioListAdapter = new AudioListAdapter(getApplicationContext(), array_spinnerLoad);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        rvAudioFile.setLayoutManager(layoutManager);
        rvAudioFile.setAdapter(audioListAdapter);
        audioListAdapter.OnItemClickListener(new AudioListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String fileName = array_spinnerLoad[position];

                String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath() + "/" + fileName;
                MediaPlayer mediaPlayer = new MediaPlayer();
                try {
                    FileInputStream is = new FileInputStream(filePath);
                    mediaPlayer.setDataSource(is.getFD());
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
                String root = Environment.getExternalStorageDirectory().toString();
                String audio = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath() + "/" + fileName;
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "HelloCamera");
                String video = mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4";
                String output = root + "/" + "jd.mp4";
                Log.e(Constant.TAG, "audio:" + audio + " video:" + video + " out:" + output);
                mux(video, audio, output);
            }
        });
    }
}

public boolean mux(String videoFile, String audioFile, String outputFile) {
    Movie video;
    try {
        video = new MovieCreator().build(videoFile);
    } catch (RuntimeException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    Movie audio;
    try {
        audio = new MovieCreator().build(audioFile);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (NullPointerException e) {
        e.printStackTrace();//In this Line Error java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.googlecode.mp4parser.BasicContainer.getBoxes
        return false;
    }

    Track audioTrack = audio.getTracks().get(0);
    video.addTrack(audioTrack);

    Container out = new DefaultMp4Builder().build(video);

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
    try {
        out.writeContainer(byteBufferByteChannel);
        byteBufferByteChannel.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

private static class BufferedWritableFileByteChannel implements WritableByteChannel {
    private static final int BUFFER_CAPACITY = 1000000;

    private boolean isOpen = true;
    private final OutputStream outputStream;
    private final ByteBuffer byteBuffer;
    private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];

    private BufferedWritableFileByteChannel(OutputStream outputStream) {
        this.outputStream = outputStream;
        this.byteBuffer = ByteBuffer.wrap(rawBuffer);
    }

    @Override
    public int write(ByteBuffer inputBuffer) throws IOException {
        int inputBytes = inputBuffer.remaining();

        if (inputBytes > byteBuffer.remaining()) {
            dumpToFile();
            byteBuffer.clear();

            if (inputBytes > byteBuffer.remaining()) {
                throw new BufferOverflowException();
            }
        }

        byteBuffer.put(inputBuffer);

        return inputBytes;
    }

    @Override
    public boolean isOpen() {
        return isOpen;
    }

    @Override
    public void close() throws IOException {
        dumpToFile();
        isOpen = false;
    }

    private void dumpToFile() {
        try {
            outputStream.write(rawBuffer, 0, byteBuffer.position());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
}

错误

Process: com.wos.capturevideowithaudio, PID: 7162
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.googlecode.mp4parser.BasicContainer.getBoxes(java.lang.Class)' on a null object reference
                                                                             at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:48)
                                                                             at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:35)
                                                                             at com.wos.capturevideowithaudio.activity.AudioMixing.appendTwoVideo(AudioMixing.java:154)
                                                                             at com.wos.capturevideowithaudio.activity.AudioMixing$1.onItemClick(AudioMixing.java:142)
                                                                             at com.wos.capturevideowithaudio.adapter.AudioListAdapter$ViewHolder.onClick(AudioListAdapter.java:60)
                                                                             at android.view.View.performClick(View.java:5612)
                                                                             at android.view.View$PerformClick.run(View.java:22285)
                                                                             at android.os.Handler.handleCallback(Handler.java:751)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

0 个答案:

没有答案