2


First of all open camera and capture 10 second video after open next activity and list of audio file then click the audio file this time merge the audio and video but in this my code getting error when select audio in storage.
Activity

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

Error

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)
Anand Diamond
  • 339
  • 1
  • 9
  • 1
    What is the values of "videoFile" and "audioFile" in mux()? And what does "new File(audioFile).exists()" return just before the crash? Path should be without content:// stuff etc. (this might appear depending on the way you selected your files) – Morphing Coffee Jun 09 '17 at 15:42
  • In case your problem is related to incorrect URI which results in non-existant file - refer [here](https://stackoverflow.com/a/44383333/5719764) to resolve it. – Morphing Coffee Jun 09 '17 at 16:34
  • URI path is correct error is MP4Parser format error and I have not used Sdcard @Evusas – Anand Diamond Jun 10 '17 at 04:01
  • @AnandDiamond mp4parser does not support .mp3 files. try with .m4a file it will definitely work. – Tushar Lathiya Mar 06 '19 at 13:54

0 Answers0