2014-11-04 6 views
0

저는 제작중인 앱의 일부 코드 작업을하고 있으며, 큰 PCM 파일 (최대 600MB)을 재생해야합니다.AudioTrack을 통해 대형 PCM 파일을 재생하는 방법은 무엇입니까? (현재 오류가 발생하고 있습니다.)

이 내 플레이에 대한 현재의 코드() 메소드 :

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"); 
     } 
    } 

그러나 라인에 응용 프로그램이 충돌 : short[] music = new short[musicLength]; I 앱이 매우 큰 배열을 만들려고하기 때문에이 믿는다 크기, 따라서 많은 메모리 (램)를 사용합니다.

`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)` 

내가 구글에서 검색 한,하지만 난 짧은 배열로 처음 로딩을하지 않고 PCM 파일을 재생할 수있는 방법을 찾을 수가 캔트 :

충돌에서 로그 캣입니다. 누군가이 문제를 해결하고 마음을 나누지 않겠다는 것을 알고 있으면 매우 감사 할 것입니다.

감사 코리

+0

안녕,하지만 난 응용 프로그램을 만들려고 노력하고 제안에 대한 감사를 오디오 파일을 편집 할 수 있으며 PCM 형식이어야합니다. 고마워 Corey – Fishingfon

답변

2

문제는 당신이 한 번에 메모리에 600 메가 바이트 파일을 읽으려고하고, 앱이 그렇게 할 충분한 메모리를 얻을 수 있다는 것입니다.

작은 버퍼를 만들고 오디오 파일을 청크로 읽고 각 청크를 AudioTrack ... 루프로 작성해야합니다.

(참고 :. 나는 단지 AudoioTrackjavadoc의 말씀에서거야 당신은 자신을 위해 더 나은 독서를 할 거라고 ...)