Trim mp3 file programmatically

后端 未结 1 1445
太阳男子
太阳男子 2021-01-22 18:55

Hey <> I need to trim / cut / edit a audio file in my app, I tried to use ffmpeg but I don\'t know how to install this library or how to even use it.. so , can someone give m

1条回答
  •  迷失自我
    2021-01-22 19:27

    There are a number of open source Android ffmpeg projects, the one which appears to be most used or discussed being:

    https://github.com/guardianproject/android-ffmpeg

    This provides ffmpeg on an Android platform and a related project provides a Java wrapper (using the Runtime.getRuntime().exec approach, not JNI as I thought originally) to allow you use it from a regular Java Android app:

    https://github.com/guardianproject/android-ffmpeg-java

    The top project above includes build instructions and some examples to test the installation.

    Some examples of the Java Wrapper project method declarations that are using ffmpeg binary are shown below. You can see the full code in the file 'android-ffmpeg-java/src/org/ ffmpeg/android/FfmpegController.java' in the android-ffmpeg-java project you have downloaded:

    public void extractAudio (Clip mdesc, String audioFormat, File audioOutPath, ShellCallback sc) throws IOException, InterruptedException 
        { ...
    
    public void concatAndTrimFilesMPEG (ArrayList videos,Clip out, boolean preConvert, ShellCallback sc) throws Exception
    { ...
    
    public Clip convertToMPEG (Clip mediaIn, String outPath, ShellCallback sc) throws Exception
    { ...
    

    If you take a look at these you should be able to see how to add one that does exactly what you require. Essentially, these methods call the cmd line ffmpeg with the parameters required for a specific task, using the Runtime.getRuntime().exec function.

    A couple of things to be aware of, from past experience using Runtime.getRuntime().exec on Android:

    • Earlier versions of Android had a bug that means that Runtime.getRuntime().exec would sometimes hang: https://stackoverflow.com/a/11411709/334402
    • Runtime.getRuntime().exec needs to be used with caution - take a look at this link for more info: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
    • As far as I am aware you need to be on a rooted device to use Runtime.getRuntime().exec on Android. This may have changed since I last looked, but worth checking if this is important for your application.

    0 讨论(0)
提交回复
热议问题