实现音乐串烧_交替播放_完整

原创
2019/04/15 22:48
阅读数 87

 配置文件:

inputpath1=G:\\qiao\\music\\aa.mp3
inputpath2=G:\\qiao\\music\\王力宏 - 改变自己.mp3
outpath=G:\\qiao\\music\\a\\ab.mp3

 正式代码:

public class Music_shi {
    public static void main(String[] args) throws Exception {
        Properties pro = new Properties();
       InputStream in = Music_shi.class.getClassLoader().getResourceAsStream( "music.properties" );
        BufferedReader br = new BufferedReader( new InputStreamReader( in, "utf-8" ) );
        pro.load(br);
        
        //两种方式都可以
        /*File file = new File( "D:\\IdeaProjects\\miao\\lianxi\\src\\music.properties" );
        FileReader fileReader = new FileReader(file);
        pro.load(fileReader);*/
        String s1 = pro.getProperty( "inputpath1" );
        String s2 = pro.getProperty( "inputpath2" );
        String so = pro.getProperty( "outpath" );
        boolean flag1 = s1.endsWith( ".mp3" );
        boolean flag2 = s2.endsWith( ".mp3" );
        if (flag1==true && flag2==true){
        getMusic(s1,s2,so);
        }else{
            System.out.println("有文件不是MP3格式");
        }
    }
    private static void getMusic(String s1, String s2, String so) throws Exception {
        //读取文件
        BufferedInputStream bis = new BufferedInputStream( new FileInputStream( s1 ) );
        BufferedInputStream bis1 = new BufferedInputStream( new FileInputStream( s2 ) );
        //写出文件
        BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( so,true ));

        int len;
        //用于记录第一首歌累计读取字节的大小
        int sum=0;
        //用于记录第二首歌累计读取字节的大小
        int sum1=0;
        //用于记录合并时歌累计写出字节的大小
        int su=0;


        int t1=320*58*1024/8;
        int t2=320*120*1024/8;

        //定义一个标记,用于两首歌交替拼接
        boolean flag=true;
        byte[] bytes = new byte[4096*32];
        while (true){
            if (su>(t1+t2)){
                break;
            }
            if (flag){
                if ((len=bis.read(bytes))!=-1){
                    sum+=len;
                    if (sum<t1){
                        flag=!flag;
                        continue;
                    }
                    bos.write( bytes,0,len );
                    su+=len;
                    if (sum>t2){
                        bos.flush();
                        break;
                    }
                }
                flag=!flag;
            }else{
                if ((len=bis1.read(bytes))!=-1){
                    sum1+=len;
                    if (sum1<t1){
                        flag=!flag;
                        continue;
                    }
                    bos.write( bytes,0,len );
                    su+=len;
                    if (sum1>t2){
                        bos.flush();
                        break;
                    }
                }
                flag=!flag;
            }
        }
        bos.close();
        bis1.close();
        bis.close();
    }
}

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部