通过请求接口得到一个文件流,输出成mp3音频文件
var response = "请求部分略过..."; StreamWriter sw = new StreamWriter(@"D:\Desktop\2222.mp3"); sw.Write(response); sw.Close();
然后代码执行也没有异常,但就是打开文件无法播放
然后我就打开GoldWave查看声波,提示无法确定格式,感觉跟后缀格式应该没关系,试过mp3、wav等那些常用的音频格式
然后根据软件提示转换成了如上图的这个文件类型和属性,然后看到声波是正常的
懂的老哥可以试试
Tips:我这是C#语言,这平台上没有C#频道,只能放兄弟Java这了,两兄弟也差不多==
扣扣:2076155011
首先确认你接收到的流是什么格式的音频流、带不带音频文件头,不带文件头那么需要先不补上文件头。
如果接收到的就是MP3流,也就是编码一直的话,那么补文件头直接合成就OK。
如果编码不一致,那么就需要转码了,比如WAV编码转成MP3编码,在合成音频文件即可
Postman返回的头信息
使用Naudio重采样的时候提示没有文件头
如何补上文件头合成呢?
我这有一个之前用过的java版的,你可以参考下: public static void addWavHeader(String fileHeaderLess, String fileHeaderAdded) { FileInputStream sourceFileIs = null; FileOutputStream resultFileOs = null; File resultFile = new File(fileHeaderAdded); File sourceFile = new File(fileHeaderLess); try { WaveHeader waveHeader = new WaveHeader(); waveHeader.DataHdrLeth = new Long(sourceFile.length()).intValue(); waveHeader.fileLength = new Long(sourceFile.length() + 44 - 8).intValue(); resultFileOs = new FileOutputStream(resultFile); sourceFileIs = new FileInputStream(sourceFile); byte[] sourceBytes = new byte[waveHeader.DataHdrLeth]; sourceFileIs.read(sourceBytes); resultFileOs.write(waveHeader.getHeader()); resultFileOs.flush(); resultFileOs.write(sourceBytes); resultFileOs.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (sourceFileIs != null) { sourceFileIs.close(); } if (resultFileOs != null) { resultFileOs.flush(); resultFileOs.close(); } } catch (IOException e) { e.printStackTrace(); } } } public class WaveHeader { public final char fileID[] = { 'R', 'I', 'F', 'F' }; public int fileLength; public char wavTag[] = { 'W', 'A', 'V', 'E' };; public char FmtHdrID[] = { 'f', 'm', 't', ' ' }; public int FmtHdrLeth = 16; public short FormatTag = 1; public short Channels = 1; public int SamplesPerSec = 8000; public int AvgBytesPerSec = 16000; public short BlockAlign = 2; public short BitsPerSample = 16; public char DataHdrID[] = { 'd', 'a', 't', 'a' }; public int DataHdrLeth; public byte[] getHeader() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); writeChar(bos, fileID); writeInt(bos, fileLength); writeChar(bos, wavTag); writeChar(bos, FmtHdrID); writeInt(bos, FmtHdrLeth); writeShort(bos, FormatTag); writeShort(bos, Channels); writeInt(bos, SamplesPerSec); writeInt(bos, AvgBytesPerSec); writeShort(bos, BlockAlign); writeShort(bos, BitsPerSample); writeChar(bos, DataHdrID); writeInt(bos, DataHdrLeth); bos.flush(); byte[] r = bos.toByteArray(); bos.close(); return r; } private void writeShort(ByteArrayOutputStream bos, int s) throws IOException { byte[] mybyte = new byte[2]; mybyte[1] = (byte) ((s << 16) >> 24); mybyte[0] = (byte) ((s << 24) >> 24); bos.write(mybyte); } private void writeInt(ByteArrayOutputStream bos, int n) throws IOException { byte[] buf = new byte[4]; buf[3] = (byte) (n >> 24); buf[2] = (byte) ((n << 8) >> 24); buf[1] = (byte) ((n << 16) >> 24); buf[0] = (byte) ((n << 24) >> 24); bos.write(buf); } private void writeChar(ByteArrayOutputStream bos, char[] id) { for (int i = 0; i < id.length; i++) { char c = id[i]; bos.write(c); } }
方便加一下扣扣嘛 还是没弄好 ... 2076155011