android path 合并,如何在Android中合并两个或多个MP3音频文件?

I'm trying to merge mp3 audio files But not successful.

Here is my code.

public static void meargeAudio(List filesToMearge)

{

while (filesToMearge.size()!=1){

try {

FileInputStream fistream1 = new FileInputStream(new File(filesToMearge.get(0).getPath())); //(/storage/emulated/0/Audio Notes/1455194356500.mp3) first source file

FileInputStream fistream2 = new FileInputStream(new File(filesToMearge.get(1).getPath()));//second source file

File file1 = new File(filesToMearge.get(0).getPath());

boolean deleted = file1.delete();

File file2 = new File(filesToMearge.get(1).getPath());

boolean deleted1 = file2.delete();

SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);

FileOutputStream fostream = new FileOutputStream(new File(filesToMearge.get(0).getPath()),true);//destinationfile

int temp;

while ((temp = sistream.read()) != -1) {

// System.out.print( (char) temp ); // to print at DOS prompt

fostream.write(temp); // to write to file

}

filesToMearge.add(0,new File(filesToMearge.get(0).getPath()));

filesToMearge.remove(1);

filesToMearge.remove(1);

fostream.close();

sistream.close();

fistream1.close();

fistream2.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

e.g

firstFileSize =12kb

secondFileSize =10kb

finalfileSize=22kb

Size is accurate But sound is missing

No error but in result i found finalfile contains only first file audio second file audio is missing.

Don't know what is the issue.if any one know the solution help me.

Thanks

解决方案

I also struggled with that and solved it using mp4parser

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.googlecode.mp4parser.authoring.tracks.AppendTrack;

For your case, I believe something like this should work:

public static void mergeAudio(List filesToMerge) {

String output = Environment.getExternalStorageDirectory().getAbsolutePath() + "output.mp3";

while (filesToMerge.size()!=1){

try {

String[] videoUris = new String[]{

filesToMerge.get(0).getPath(),

filesToMerge.get(0).getPath()

};

List videoTracks = new LinkedList();

List audioTracks = new LinkedList();

for (Movie m : inMovies) {

for (Track t : m.getTracks()) {

if (t.getHandler().equals("soun")) {

audioTracks.add(t);

}

if (t.getHandler().equals("vide")) {

videoTracks.add(t);

}

}

}

Movie result = new Movie();

if (!audioTracks.isEmpty()) {

result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));

}

if (!videoTracks.isEmpty()) {

result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));

}

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

FileChannel fc = new RandomAccessFile(output, "rw").getChannel();

out.writeContainer(fc);

fc.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

There are a few answers suggesting that, such as:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值