在Windows上使用AudioInputStream导致文件被占用无法delete的解决办法

本文探讨了在Windows环境下使用HTML5录制音频并上传至后台保存时遇到的问题,即文件写入一次后无法再次操作。通过采用Binary Blob方式上传音频,并在AudioSystem.getAudioInputStream()方法中使用FileInputStream与BufferedInputStream结合,避免了文件被占用的问题,有效解决了此技术难题。
最近在研究通过HTML5录制音频直接上传到后台保存文件

使用到了AudioInputStream,现在是使用binary 二进制的blob来上传然后反复的操作生成的音频文件,但是在Windows下,文件写了一次之后就不能再往里面操作了,进程被占用,导致无法操作原有文件

解决办法就是
AudioSysten.getAudioInputStream();
方法里面不直接使用File对象

而是FileInputStream读取File对象
再通过BufferedInputStream来加载FileInputStream
然后AudioSysten.getAudioInputStream();来加载这个BufferedInputStream,这样的话,就不会有此类问题了

这里注意,页不能直接使用FileInputStream,否则会报
java.io.IOException: mark/reset not supported的错误

因为FileInputStream没有缓冲
Traceback (most recent call last): File "D:\python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 286, in ffmpeg_parse_infos match = re.findall("([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])", line)[0] IndexError: list index out of range During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\python\配音器.py", line 130, in <module> run_file(input("请输入文件路径:"),input("请输入标题:")) File "D:\python\配音器.py", line 127, in run_file final_clip=final_clip.set_audio(AudioFileClip("tmp.wav")) File "D:\python38\lib\site-packages\moviepy\audio\io\AudioFileClip.py", line 70, in __init__ self.reader = FFMPEG_AudioReader(filename, fps=fps, nbytes=nbytes, File "D:\python38\lib\site-packages\moviepy\audio\io\readers.py", line 51, in __init__ infos = ffmpeg_parse_infos(filename) File "D:\python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 289, in ffmpeg_parse_infos raise IOError(("MoviePy error: failed to read the duration of file %s.\n" OSError: MoviePy error: failed to read the duration of file tmp.wav. Here are the file infos returned by ffmpeg: ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers built with gcc 9.2.1 (GCC) 20200122 configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt libavutil 56. 31.100 / 56. 31.100 libavcodec 58. 54.100 / 58. 54.100 libavformat 58. 29.100 / 58. 29.100 libavdevice 58. 8.100 / 58. 8.100 libavfilter 7. 57.100 / 7. 57.100 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 [wav @ 0000021493dd8980] Cannot check for SPDIF Guessed Channel Layout for Input Stream #0.0 : mono Input #0, wav, from 'tmp.wav': Duration: N/A, bitrate: 0 kb/s Stream #0:0: Audio: pcm_u8 ([1][0][0][0] / 0x0001), 1 Hz, mono, u8, 0 kb/s At least one output file must be specified
最新发布
06-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值