我想将音频与视频合并

问题描述

请帮帮我,我很困惑。

我想在我的短视频应用中将音频与视频合并。

我正在这样做,但它一直在加载...

它可以在我以前的应用程序中运行,但不能在我为客户制作的新应用程序中运行。

调用merge_audio_video

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

    }```

merge_audio_video 类

```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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)