amr format to mp3 and play direct amr file formats -sunziren

  Original articles, please indicate the source!


Preface:

  amr audio format as a high compression ratio, by many customers. This paper mainly involves two parts, one is how to turn amr mp3 format, the second is how to play amr file format directly.

1. How to use Java to amr file formats into mp3 file format?

      To achieve the transformation required a dependency:

    <dependency> 
    <the groupId> ws.schild </ the groupId>
    <the artifactId> Jave-Core </ the artifactId>
    <Version> 2.4.4 </ Version>
    </ dependency>
  this is dependent on the nature of the above package for a pile of ffmpeg call interface, so your environment to install ffmpeg, otherwise there is a single jar package is useless. You can go to download and install the corresponding version of ffmpeg your system, and then configure the environment variables, the final confirmation ffmpeg -version command normal use, says a successful installation.
  With the jar package and ffmpeg, you just need to rest following code on it:
public boolean amrToMp3(java.io.File source, java.io.File target) throws InputFormatException {
        boolean bool = false;
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        try {
            MultimediaObject multimediaObject  = new MultimediaObject(source);
            encoder.encode(multimediaObject,target, attrs);
            bool = true;
        } catch (IllegalArgumentException | EncoderException e) {
            e.printStackTrace();
            bool = false;
        }finally {
            return bool;
        }
    }

   Incoming source code file path and target file path, then we will put into amr mp3.

   I wanted to adopt this approach, because I was local normal use, but is forced to server-based arm cpu version of redhat 7.6, you want to install ffmpeg on it extremely difficult for me, first download ffmpeg source code, and then compile, and then configure the environment variables, the process of trial and error will spend a considerable part of the time, so start thinking, you can not play amr files js?

2. How to Play amr file format directly?

   After a great deal of information in the online search and found amr can put a string into base64, base64 strings can then play with js. This method has a limitation is this: If the base64 string is too long, the program runs to be wrong, because we amr recordings from users, so we can control the maximum recording time, so this problem is resolved.

   Related js My main reference is the https://blog.csdn.net/qq_43466173/article/details/86540193 this blog, and then find that voice-2.0.js online for a long time to find, for fear of infringement, I will not stick a.

      Here usage more to say, after the introduction of js, first when the page is loaded, initialization this thing: RongIMLib.RongIMVoice.init ();

   Then you can get hold button, when clicked triggers, passing base64 string amr turn, (note the comma in front of a base64 string beginning that part of it does not require) RongIMLib.RongIMVoice.play (base64), this page when will the sound.

 If you want to be private letter to me.


  This is the thinking process amr two issues, the brothers understand?

  Well, if you can not find it, then pay attention to my micro-channel public number " Chaos forces ", click on the bottom cookies , and then click audiojs . Hush, this js I did not write, I did not copyright.

 

 

Guess you like

Origin www.cnblogs.com/sunziren/p/12039740.html