本文主要是介绍【Java学习】将几首歌拼接成一首连续播放的大合唱(20),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
以任意字节切割mp3歌之后再拼接成完整的
- 案例1:
- 方法1
- 方法2
- 案例2
- 将多首歌拼接成一首大合唱,优化代码
对一首歌或者多首歌按照1Mb进行切割保存,然后再将切割后的碎片拼接成一首的完整的歌,可以拼接多首歌.来个几十兆的大合唱.
案例1:
对一首歌按照1Mb进行切割保存,然后再将切割后的碎片拼接成一首的完整的歌
将g:\夜夜夜夜.mp3
切开存储g:\apartFile文件夹中
再访问文件夹拼接完整.
方法1
用随机访问流RandomAccessFile,可以控制文件指针
顺序流SequenceInputStream 拼接,可以传一个Enumeration迭代器
package com.westos.afternoon;import java.io.*;
import java.util.Enumeration;
import java.util.Vector;public class Demo5 {public static void main(String[] args) throws IOException {//用随机访问流,可以控制文件指针RandomAccessFile in = new RandomAccessFile(new File("g:\\夜夜夜夜.mp3"), "rw");byte[] bytes = new byte[1024];takeApart(in, bytes);//拆分mergingMusic(bytes);//合并}private static void takeApart(RandomAccessFile in, byte[] bytes) throws IOException {//我的这首歌是4.6兆左右,按照一兆一切可以切5份File file = new File("g:\\apartFile");if(!file.exists()){file.mkdirs();}for (int i = 1; i < 6; i++) {//切开生成5个FileOutputStream out = new FileOutputStream("g:\\apartFile\\apart" + i + ".mp3");int len = 0;while (in.getFilePointer() < 1024*1024 * (i) && (len = in.read(bytes)) != -1) {out.write(bytes, 0, len);out.flush();//System.out.println(in.getFilePointer());}}}private static void mergingMusic(byte[] bytes) throws IOException {Vector<FileInputStream> vector = new Vector<>();File file = new File("g:\\apartFile");File[] files = file.listFiles();for (File f : files) {vector.add(new FileInputStream(f));}Enumeration<FileInputStream> elements = vector.elements();//顺序流SequenceInputStream可以传一个Enumeration迭代器SequenceInputStream sq = new SequenceInputStream(elements);FileOutputStream copy = new FileOutputStream("g:\\apartFile\\夜夜夜夜.mp3",true);int len = 0;byte[] bytes1 = new byte[1024];while ((len = sq.read(bytes)) != -1) {copy.write(bytes, 0, len);}copy.close();sq.close();}}
方法2
用字节缓冲区,一次缓冲1Mb存1Mb
package org.westos.demo2;import java.io.*;
import java.util.Vector;/*** @Author: Administrator* @CreateTime: 2019-01-18 15:17* @Description todo*/
public class MyTest5 {public static void main(String[] args) throws IOException {chaifen(); //拆分文件hebing();//合并文件}private static void hebing() throws IOException {File file1 = new File("g:\\music");File[] files = file1.listFiles();Vector<FileInputStream> v = new Vector<>();for (File f : files) {if (f.isFile() && f.getName().endsWith(".mp3")) {FileInputStream in = new FileInputStream(f);v.add(in);}}SequenceInputStream sin = new SequenceInputStream(v.elements());byte[] bytes = new byte[1024 * 1024];int len = 0;FileOutputStream out = new FileOutputStream("g:\\music\\夜夜夜夜-齐秦.mp3");while ((len = sin.read(bytes)) != -1) {out.write(bytes, 0, len);out.flush();}sin.close();out.close();}private static void chaifen() throws IOException {File file = new File("夜夜夜夜.mp3");//2.封装一个文件夹File file1 = new File("g:\\music");if (!file1.exists()) {file1.mkdirs();}FileInputStream in = new FileInputStream(file);//拆分文件byte[] bytes = new byte[1024 * 1024];//一次取出1Mb,存1Mbint len = 0;int i = 1;while ((len = in.read(bytes)) != -1) {FileOutputStream out = new FileOutputStream(new File(file1, (i++) + ".mp3"));out.write(bytes, 0, len);out.close();}in.close();}
}
案例2
将多个音乐切割并拼接成一首歌
例如以下三首:
g:\情书.mp3
g:\夜夜夜夜.mp3
g:\怎样.mp3
拼接成g:\Copy.mp3
将多首歌拼接成一首大合唱,优化代码
package com.westos.afternoon;import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;public class Demo5 {public static void main(String[] args) throws IOException {//用随机访问流,可以控制文件指针//常见要合并的歌曲,以下三首RandomAccessFile in = new RandomAccessFile(new File("g:\\情书.mp3"), "rw");RandomAccessFile in1 = new RandomAccessFile(new File("g:\\夜夜夜夜.mp3"), "rw");RandomAccessFile in2 = new RandomAccessFile(new File("g:\\怎样.mp3"), "rw");ArrayList<RandomAccessFile> list = new ArrayList<>();list.add(in);list.add(in1);list.add(in2);for (RandomAccessFile randomAccessFile : list) {takeApart(randomAccessFile);//拆分成10份,每次循环覆盖mergingMusic();//合并这10分}}private static void takeApart(RandomAccessFile in) throws IOException {//我的这首歌是4.6兆左右,按照一兆一切可以切5份,但是我考虑到有的歌大于5兆,就分成10份,相当于可以存10兆之内的歌File file = new File("g:\\apartFile");if(!file.exists()){file.mkdirs();}for (int i = 1; i < 11; i++) {//切开生成5个FileOutputStream out = new FileOutputStream("g:\\apartFile\\apart" + i + ".mp3");int len = 0;byte[] bytes = new byte[1024];while (in.getFilePointer() < 1024*1024 * (i) && (len = in.read(bytes)) != -1) {out.write(bytes, 0, len);out.flush();//System.out.println(in.getFilePointer());}out.close();}}private static void mergingMusic() throws IOException {Vector<FileInputStream> vector = new Vector<>();File file = new File("g:\\apartFile");File[] files = file.listFiles();for (File f : files) {if(f.isFile()&&f.getName().substring(0, 5).equals("apart")){vector.add(new FileInputStream(f));}}Enumeration<FileInputStream> elements = vector.elements();//顺序流SequenceInputStream可以传一个Enumeration迭代器SequenceInputStream sq = new SequenceInputStream(elements);FileOutputStream copy = new FileOutputStream("g:\\Copy.mp3",true);int len = 0;byte[] bytes = new byte[1024];while ((len = sq.read(bytes)) != -1) {copy.write(bytes, 0, len);}copy.close();sq.close();}}
结果:
将一首歌切成10块,每块最大1024kb,每次循环覆盖原有的
Copy.mp3就是最后结果,将3首歌拼接在了一起.
谢谢
这篇关于【Java学习】将几首歌拼接成一首连续播放的大合唱(20)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!