我想把音频和视频合并起来

pjngdqdw  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(205)

请帮帮我,我很困惑。
我想在我的短视频应用程序中合并音频和视频。
我这样做,但它是不断加载。。。
它在我以前的应用程序中工作,但在我为客户制作的新应用程序中不工作。
呼叫合并\u音频\u视频

```public void Merge_withAudio(){

        String audio_file;
        audio_file = Variables.app_hided_folder +"selectedaudio.aac";

        Merge_Video_Audio merge_video_audio=new Merge_Video_Audio(GallerySelectedVideo_A.this);
        merge_video_audio.doInBackground(audio_file,Variables.outputfile,Variables.outputfile2,draft_file);

    }```

合并\u音频\u视频类

```public class Merge_Video_Audio extends AsyncTask<String,String,String> {

    ProgressDialog progressDialog;
    Context context;

    String audio,video,output,draft_file;

    public Merge_Video_Audio(Context context){
        this.context=context;
        progressDialog=new ProgressDialog(context);
        progressDialog.setMessage("Please Wait...");
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    public String doInBackground(String... strings) {
        try {
            progressDialog.show();
        }catch (Exception e){

        }
         audio=strings[0];
         video=strings[1];
         output=strings[2];
         if(strings.length==4){
             draft_file=strings[3];
         }

        Log.d("resp",audio+"----"+video+"-----"+output);

        Thread thread = new Thread(runnable);
        thread.start();

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }

    public void Go_To_preview_Activity(){
        Intent intent =new Intent(context,Preview_Video_A.class);
        intent.putExtra("path", Variables.outputfile2);
        intent.putExtra("draft_file",draft_file);
        context.startActivity(intent);
    }

    public Track CropAudio(String videopath,Track fullAudio){

        Toast.makeText(context, "crop function", Toast.LENGTH_SHORT).show();
        try {

            IsoFile isoFile = new IsoFile(videopath);

            double lengthInSeconds = (double)
                    isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
                    isoFile.getMovieBox().getMovieHeaderBox().getTimescale();

            Track audioTrack = (Track) fullAudio;

            double startTime1 = 0;
            double endTime1 = lengthInSeconds;

            long currentSample = 0;
            double currentTime = 0;
            double lastTime = -1;
            long startSample1 = -1;
            long endSample1 = -1;

            for (int i = 0; i < audioTrack.getSampleDurations().length; i++) {
                long delta = audioTrack.getSampleDurations()[i];

                if (currentTime > lastTime && currentTime <= startTime1) {
                    // current sample is still before the new starttime
                    startSample1 = currentSample;
                }
                if (currentTime > lastTime && currentTime <= endTime1) {
                    // current sample is after the new start time and still before the new endtime
                    endSample1 = currentSample;
                }

                lastTime = currentTime;
                currentTime += (double) delta / (double) audioTrack.getTrackMetaData().getTimescale();
                currentSample++;
            }

            CroppedTrack cropperAacTrack = new CroppedTrack(fullAudio, startSample1, endSample1);

            return cropperAacTrack;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return fullAudio;
    }

   public Runnable runnable =new Runnable() {
        @Override
        public void run() {

            try {

                Movie m = MovieCreator.build(video);

                List nuTracks = new ArrayList<>();

                for (Track t : m.getTracks()) {
                    if (!"soun".equals(t.getHandler())) {
                        nuTracks.add(t);
                    }
                }

                 Track nuAudio = new AACTrackImpl(new FileDataSourceImpl(audio));
                 Track crop_track= CropAudio(video,nuAudio);
                 nuTracks.add(crop_track);
                 m.setTracks(nuTracks);
                Container mp4file = new DefaultMp4Builder().build(m);
                FileChannel fc = new FileOutputStream(new File(output)).getChannel();
                mp4file.writeContainer(fc);
                fc.close();
                try {
                    progressDialog.dismiss();
                }catch (Exception e){
                    Log.d(Variables.tag,e.toString());

                }finally {
                    Go_To_preview_Activity();
                }

            } catch (IOException e) {
                e.printStackTrace();
                Log.d(Variables.tag,e.toString());

            }

        }

    };
}```

可能的原因-我使用的是mp3文件,但我将其保存为selectedaudio.aac

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题