关于实现base64文件解码转mp3实现播放功能

该博客围绕实现Base64文件解码转MP3并播放的功能展开。需求是接收Python后端传输的以Base64编码的实时录音文件,用Java进行处理,将其转换为MP3文件,最终实现播放功能。
该文章已生成可运行项目,

关于实现base64文件解码转mp3实现播放功能

1.简介
需求:大致接收一个python后端传输的一段实时录音文件,以base64编码格式为主的,java进行处理,转MP3文件,实现播放小功能。

**一、java对接python后端controller层代码**:
核心:对接收的base64编码文件进行解码处理
方法: 采用Base64.Decoder decoder = Base64.getDecoder();进行解码,采用IO流将文件写入,实现mp3功能播放

 @PostMapping("/speech")
public Result speech(@RequestBody SyncListPojo<Speech> syncListPojo, HttpServletRequest request) {
    List<Speech> speeches = syncListPojo.getData();
    if (null == speeches || speeches.isEmpty()) {
        return Result.fail("同步数据为空!");
    }
    UserDevices current = (UserDevices) request.getAttribute(UserDeviceService.CurrentDevice);
    System.out.println("current = " + current);
    if (null == current) {
            return Result.fail("设备不可用!");
    }
    for (Speech speech : speeches) {
        //base64 转音频 2021-7-19 start
        //解码
*Base64.Decoder decoder = Base64.getDecoder();
        String fileDir = upload + "speech" + File.separator + DateUtil.getDateString(new Date(), "yyyy-MM-dd");
        String fileDir1 ="speech" + File.separator + DateUtil.getDateString(new Date(), "yyyy-MM-dd");
        File file = new File(fileDir);
        if(!file.exists()){
            file.mkdirs();
        }
        String fileName = UUID.randomUUID()+".wav";
        String filePath = fileDir + File.separator + fileName;
        String filePath1 = fileDir1 + File.separator + fileName;
            byte[] decode = decoder.decode(speech.getBasePath());
        try {
            for (int i = 0; i <decode.length ; i++) {
                if (decode[i] < 0) {
                    decode[i] += 256;
                }
                                }
            FileOutputStream out = new FileOutputStream(filePath);
            out.write(decode);
            out.flush();
            out.close();
            if(null != filePath){
                speech.setBasePath(filePath1);
                speech.setSpeechType(1);
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("base64字符串转化成mp3异常,{}",e);
        }*
        **//base64 转音频 2021-7-19  end**
            if (StringUtil.isEmpty(speech.getNumbers())) {
                return Result.fail(JSON.toJSONString(speech) + "numbers不能为空");
            } else if (StringUtil.isEmpty(speech.getCreateDate())) {
                return Result.fail(JSON.toJSONString(speech) + "createDate不能为空");
            } else if (StringUtil.isEmpty(speech.getSpeechType())) {
                return Result.fail(JSON.toJSONString(speech) + "speechType不能为空");
            } else if (StringUtil.isEmpty(speech.getSpeechContent())) {
                return Result.fail(JSON.toJSONString(speech) + "speechContent不能为空");
            }
        speech.setId(StringUtil.getUUID());
        speech.setUserId(current.getUserId());
        speech.setUserName(current.getUserName());
        speech.setLocationId(current.getLocationId());
        speech.setLocationName(current.getLocationName());
        speech.setDeviceId(current.getId());
        speech.setDeviceName(current.getName());
    }

    return Result.success(speechClient.inserts(speeches), "同步成功!");
}
本文章已经生成可运行项目
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值