gpt4 book ai didi

java - 具有暂停和恢复功能的 Android 录音机

转载 作者:太空狗 更新时间:2023-10-29 14:43:55 28 4
gpt4 key购买 nike

有没有人可以帮助我开发具有播放和暂停功能的灵活录音机?

注意:我用过PauseResumeAudioRecorder .这很好但不灵活,因为当我停止录制时我的文件就会损坏。我也使用过 MP4Parser 库,但是合并两个大文件需要花费很多时间。

这是我用于合并两个文件的 Mp4Wrapper 类。

public final class Mp4ParserWrapper {

public static final String TAG = "Mp4ParserWrapper";

public static final int FILE_BUFFER_SIZE = 1024;

private Mp4ParserWrapper() {
}

/**
* Appends mp4 audios/videos: {@code anotherFileName} to {@code mainFileName}.
*
* @param mainFileName The first file path.
* @param anotherFileName The second file path.
*
* @return true if the operation was made successfully.
*/
public static boolean append(String mainFileName, String anotherFileName){
try {
File targetFile = new File(mainFileName);
File anotherFile = new File(anotherFileName);
if (targetFile.exists() && targetFile.length() > 0) {
String tmpFileName = mainFileName + ".tmp";
append(mainFileName, anotherFileName, tmpFileName);
copyFile(tmpFileName, mainFileName);
return anotherFile.delete() && new File(tmpFileName).delete();
} else {
if (!targetFile.exists()) {
if (!targetFile.getParentFile().exists()) {
if (!targetFile.getParentFile().mkdirs()) {
return false;
}
}
if (!targetFile.createNewFile()) {
return false;
}
}
copyFile(anotherFileName, mainFileName);
return anotherFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Appending two mp4 files failed with exception", e);
return false;
} catch (Exception e) {
e.printStackTrace();
return false;

}
}


private static void copyFile(final String from, final String destination)
throws IOException {
FileInputStream in = new FileInputStream(from);
FileOutputStream out = new FileOutputStream(destination);
copy(in, out);
in.close();
out.close();
}

private static void copy(FileInputStream in, FileOutputStream out) throws IOException {
byte[] buf = new byte[FILE_BUFFER_SIZE];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}

public static void append(
final String firstFile,
final String secondFile,
final String newFile) throws Exception {
final Movie movieA = MovieCreator.build(new FileDataSourceImpl(new File(secondFile)));
final Movie movieB = MovieCreator.build(new FileDataSourceImpl(firstFile));

final Movie finalMovie = new Movie();

final List<Track> movieOneTracks = movieA.getTracks();
final List<Track> movieTwoTracks = movieB.getTracks();

for (int i = 0; i < movieOneTracks.size() || i < movieTwoTracks.size(); ++i) {
finalMovie.addTrack(new AppendTrack(movieTwoTracks.get(i), movieOneTracks.get(i)));
}

final Container container = new DefaultMp4Builder().build(finalMovie);

final FileOutputStream fos = new FileOutputStream(new File(String.format(newFile)));
final WritableByteChannel bb = Channels.newChannel(fos);
container.writeContainer(bb);
fos.close();
}

最佳答案

我建议你使用 mp4parser在该文件中,您可以看到它检查了轨道大小,并且相应的代码将完美地添加它。

关于java - 具有暂停和恢复功能的 Android 录音机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43066328/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com