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

原创
2019/04/15 00:40
阅读数 35
public class Music_shi {
    public static void main(String[] args) throws Exception {
        Properties pro = new Properties();
        InputStream is = Music_shi.class.getClassLoader().getResourceAsStream( "music.properties" );
        pro.load(is);
        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
分享
返回顶部
顶部