Android : Merging/concatenating two audio files hangs up the application
Asked Answered
C

1

9

I'm appending two audio file using mp4parser. Appending is done but it is very slow don't know what is the issue.

On this line debugger stuck for big audio files for example approx 30 minutes audio file.

Container out = new DefaultMp4Builder().build(result);

Here is code.

Movie[] inMovies = null;
                inMovies = new Movie[]{
                        MovieCreator.build(fileOne),
                        MovieCreator.build(fileTwo)};
                List<Track> audioTracks = new LinkedList<Track>();

                for (Movie m : inMovies) {
                    for (Track t : m.getTracks()) {
                        if (t.getHandler().equals("soun")) {
                            audioTracks.add(t);
                        }
                    }
                }
                Movie result = new Movie();
                if (audioTracks.size() > 0) {
                    result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
                }
                Container out = new DefaultMp4Builder().build(result);
                String fileName = getFilename();
                FileChannel fc = new RandomAccessFile(String.format(fileName), "rw").getChannel();
                out.writeContainer(fc);
                fc.close();

Manifestfile

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ehrlite"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ehr_icon"
        android:label="@string/app_name"
        android:name="com.webservice.myApplication"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ehrlite.LoginActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
             android:windowSoftInputMode="stateVisible|adjustResize"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.ehrlite.MasterActivity"
            android:screenOrientation="portrait">
        </activity>
          <activity
            android:name="com.ehrlite.MainActivity"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity
            android:name="com.ehrlite.PrevMemo"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name="com.webviewcall.WebViewActivity"
            android:screenOrientation="portrait">
        </activity>`enter code here`
        <activity
            android:name="com.ehrlite.TranscribedActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"
            android:windowSoftInputMode="adjustPan">
        </activity>


    </application>

</manifest>

Can anyone help me out to improve performance, thanks in advance.

Celestina answered 13/10, 2016 at 16:32 Comment(9)
can you put your manifests?Fernando
@BhunnuBaba I have updated my question.Celestina
perhaps MyApplication file extends with MultiDexApplication if yes then once add android:largeHeap="true" in application tagFernando
@BhunnuBaba I have tried but not getting success...Celestina
once try with separate threadFernando
@BhunnuBaba Tried but debugger still stuck on this line new DefaultMp4Builder().build(result)...Celestina
I was having the same problem. but if you do not put it on debug it runs fine. that said you should not put it on main thread as bigger files may take longer time to process. I put few logs instead of debugging for a work around. this problem is rather general, CPU intensive task runs slow when you put the process on debug.Clown
Checkd this different approach ? #6731655 Or with library like github.com/sannies/mp4parser/blob/master/examples/src/main/java/…Callender
Guys any solution ?Nettie
P
0

I found the solution for your answer. I was faced issue previously. You are using RandomAccessfile. You need to use FileOutputStream

Container out = new DefaultMp4Builder().build(result);
        String outputFilePath = getfilename();
        FileOutputStream fos = new FileOutputStream(new File(outputFilePath));
        out.writeContainer(fos.getChannel());
        fos.close();

Reference:

why are java RandomAccessFile so much slower than FileOutputStream?

Parament answered 25/10, 2016 at 4:23 Comment(3)
I have tried FileOutputStream inplace of RandomAccessfile but still it takes much time for concatenating two audio files. No Success.Celestina
do one thing. there may is a chance to didn't find proper path for getFilename(). Use hardcoded path for thisParament
@Parament great observation for code. Bhumit You should try this .Thanks for sharing. I was facing same issue. +1 for this.Abstergent

© 2022 - 2024 — McMap. All rights reserved.