微信开发 从微信服务器下载amr转mp3问题

风中掠过 发布于 2016/10/10 12:24
阅读 439
收藏 0
InputStream inputStream = getInputStream(accessToken, mediaId);  
        byte[] data = new byte[10240];  
        int len = 0;  
        FileOutputStream fileOutputStream = null;  
        String sourcePath = filePath+fileName+".amr";
        String targetPath = filePath+fileName+".mp3";
        try {  
         
            fileOutputStream = new FileOutputStream(sourcePath);  
            while ((len = inputStream.read(data)) != -1) {  
                fileOutputStream.write(data, 0, len);
            } 
            //将amr转换成mp3
            changeToMp3(sourcePath,targetPath);
             
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (inputStream != null) {  
                try {  
                    inputStream.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            if (fileOutputStream != null) {  
                try {  
                    fileOutputStream.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }

public static void changeToMp3(String sourcePath, String targetPath) {  
	        File source = new File(sourcePath);  
	        File target = new File(targetPath);  
	        AudioAttributes audio = new AudioAttributes();  
	        Encoder encoder = new Encoder();  
	  
	        audio.setCodec("libmp3lame");  
	        EncodingAttributes attrs = new EncodingAttributes();  
	        attrs.setFormat("mp3");  
	        attrs.setAudioAttributes(audio);  
	        
	        try {  
	            encoder.encode(source, target, attrs);  
	        } catch (IllegalArgumentException e) {  
	            e.printStackTrace();  
	        } catch (InputFormatException e) {  
	            e.printStackTrace();  
	        } catch (EncoderException e) {  
	            e.printStackTrace();  
	        }
	    }



这里转换成MP3后发现部分的mp3文件和原文件amr录音有缺失,录音像被剪辑过,比如同样的59秒源文件转换为mp3会变成只有50秒,后面9秒没有了,还请各位大神帮忙看看是什么原因,本地测试转换都是正常的



加载中
返回顶部
顶部