java - 微信公众号开发语言转码问题
黄舟
黄舟 2017-04-17 17:30:38
0
1
728

微信服务号开发,使用jssdk上传语音,然后再利用下载接口将语音下载到本地服务器为amr格式,ios无法直接播放,所以在网上找了实现转码功能的代码.
但是在使用ffmpeg转码为MP3格式后,转码后的文件可以在安卓端,
PC端正常使用,在ios上无法正常播放,点击播放,没有声音,这问题该怎么解决呢,还是我转码中有什么问题?

使用java做为开发语言,以下是实现转码功能的代码

 /**
  * @param nowPath       转码前文件保存路径
  * @param afterPath   转码后文件保存路径
  * @param choice   是否删除原文件,true为删除   
  */
 public static void convertVoice(String nowPath,String afterPath,boolean choice) {
        File source = new File(nowPath);
        File target = new File(afterPath);
        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();
        }
        //如果传入布尔类型为真,删除原文件
        if(choice){
            source.delete();
        }
    }
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
Ty80

I’m not sure if you are referring to playback on ios using the h5 audio tag, or some other method.
If it is h5, first make sure that the browser you use to open this playback page supports h5 (the one that comes with WeChat supports it). If it still cannot be played, check the MP3 file to find the reason.
ffmpeg has different requirements on different servers. If your server is Linux, please make sure that the number of ffmeg bits is the same as the number of linux bits, and the Linux kernel version needs to be 2.6.32 or above.
Finally, it is not recommended that you delete the source file. The sound quality of the mp3 converted by ffmpeg is very poor. The source file can be saved in amr format and used for group voice messages or passive reply to voice messages.
Attached is an article about ffmpeg: http://linjie.org/2015/08/06/amr%E6%A0%BC%E5%BC%8F%E8%BD%ACmp3%E6%A0%BC%E5 %BC%8F-%E5%AE%8C%E7%BE%8E%E8%A7%A3%E5%86%B3Linux%E4%B8%8B%E8%BD%AC%E6%8D%A20K%E9% 97%AE%E9%A2%98/

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template