简体   繁体   English

如何通过AudioTrack播放较大的PCM文件? (当前出现错误)

[英]how to play large PCM file via AudioTrack? (Currently getting errors)

I have been working on some code for an app i am making, and i need to play large PCM files (upwards of 600mb). 我一直在为我正在制作的应用程序编写一些代码,并且我需要播放大型PCM文件(最大600mb)。

This is the current code for my play() method: 这是我的play()方法的当前代码:

public void play(View view) {
        Toast.makeText(this, "play", Toast.LENGTH_SHORT).show();

// Get the file we want to playback.
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "ACS.pcm");
// Get the length of the audio stored in the file (16 bit so 2 bytes per short)
// and create a short array to store the recorded audio.
        int musicLength = (int)(file.length()/2);
        short[] music = new short[musicLength];


        try {
// Create a DataInputStream to read the audio data back from the saved file.
            InputStream is = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dis = new DataInputStream(bis);

// Read the file into the music array.
            int i = 0;
            while (dis.available() > 0) {
                music[musicLength-1-i] = dis.readShort();
                i++;
            }


// Close the input streams.
            dis.close();


// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file.
            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                    44100,
                    AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    musicLength,
                    AudioTrack.MODE_STREAM);
// Start playback
            audioTrack.play();

// Write the music buffer to the AudioTrack object
            audioTrack.write(music, 0, musicLength);


        } catch (Throwable t) {
            Log.e("AudioTrack","Playback Failed");
        }
    }

However the application crashes on the line: short[] music = new short[musicLength]; 但是,应用程序崩溃了: short[] music = new short[musicLength]; I believe this is because the app is trying to create an array of a very large size, hence using up a lot of memory (ram). 我相信这是因为该应用程序试图创建一个非常大的数组,因此会占用大量内存(ram)。

The logcat from the crash is: 崩溃的日志是:

`11-03 16:31:03.654  16916-16916/com.bacon.corey.audiotimeshift I/art﹕ Alloc sticky concurrent mark sweep GC freed 6307(421KB) AllocSpace objects, 1(12KB) LOS objects, 33% free, 15MB/23MB, paused 678us total 14.444ms
11-03 16:31:03.663  16916-16916/com.bacon.corey.audiotimeshift I/art﹕ Alloc partial concurrent mark sweep GC freed 213(22KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 15MB/26MB, paused 659us total 7.132ms
11-03 16:31:03.679  16916-16916/com.bacon.corey.audiotimeshift I/art﹕ Alloc concurrent mark sweep GC freed 15(12KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 15MB/26MB, paused 830us total 14.191ms
11-03 16:31:03.681  16916-16916/com.bacon.corey.audiotimeshift I/art﹕ Forcing collection of SoftReferences for 641MB allocation
11-03 16:31:03.695  16916-16916/com.bacon.corey.audiotimeshift I/art﹕ Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 39% free, 15MB/26MB, paused 645us total 13.989ms
11-03 16:31:03.700  16916-16916/com.bacon.corey.audiotimeshift E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 672164876 byte allocation with 11094652 free bytes and 176MB until OOM"
11-03 16:31:03.703  16916-16916/com.bacon.corey.audiotimeshift D/AndroidRuntime﹕ Shutting down VM
11-03 16:31:03.713  16916-16916/com.bacon.corey.audiotimeshift E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bacon.corey.audiotimeshift, PID: 16916
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 672164876 byte allocation with 11094652 free bytes and 176MB until OOM
            at com.bacon.corey.audiotimeshift.MainActivity.play(MainActivity.java:87)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)`

I have searched on google, but i cant seem to find any way of playing the PCM file without first loading it into a short array. 我已经在Google上进行了搜索,但是似乎没有找到任何播放PCM文件的方法,而无需先将其加载到短数组中。 If someone knows of a way around this, and wouldnt mind sharing, i would be very grateful. 如果有人知道解决此问题的方法,并且不愿与他人分享,我将不胜感激。

Thanks Corey 谢谢科里

The problem is that you are trying to read a 600+ Mbyte file into memory in one go, and your app can't get enough memory to do that. 问题在于您试图一次性读取600 MB以上的文件到内存中,而您的应用程序无法获得足够的内存来执行此操作。

What you need to do is to create a smaller buffer, and read the audio file in chunks, writing each chunk to the AudioTrack ... in a loop. 您需要做的是创建一个较小的缓冲区,并AudioTrack读取音频文件,然后将每个分块循环写入AudioTrack ...。

(Note: I'm only going from what the AudoioTrack javadoc says. You'd do better reading it for yourself ...) (注意:我只使用AudoioTrack javadoc所说的内容。您最好亲自阅读一下...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM