2013-02-27 3 views
0

AudioTrack을 사용하여 리소스 폴더에서 사운드 파일을 재생하는이 프로그램을 실행하고 있습니다. 출력이 있지만 소리가 틀립니다.Android AudioTrack을 사용하여 사운드를 올바르게 재생할 수 없습니다.

프로젝트 파일이 업로드되었습니다.

http://www.mediafire.com/?995mxc87hf28fxk

package com.self.AudioTrack; 

    import java.io.IOException; 

    import android.app.Activity; 
    import android.content.Context; 
import android.os.Bundle; 

public class AudioTrack2Activity extends Activity { 
/** Called when the activity is first created. */ 
Context ctx; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ctx=getApplicationContext(); 
    new Thread(new Runnable() 
    { 
     // private Object context; 

    public void run() 
     {     

      AudioDevice device = new AudioDevice(ctx); 
     // String filepath="R.raw.cheerapp.mp3"; 
     // InputStream cheerSound = this.getResources().openRawResource(R.raw.cheerapp); 
      //InputStream cheerSound = this.getContext().getResources().openRawResource(R.raw.cheerapp); 
      try { 
      device.PlayShortAudioFile(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

    }).start(); 


} 

}

package com.self.AudioTrack; 

    import java.io.BufferedInputStream; 
    import java.io.ByteArrayOutputStream; 
    import java.io.DataInputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.io.InputStream; 

    import android.content.Context; 
    import android.media.AudioFormat; 
    import android.media.AudioManager; 
    import android.media.AudioTrack; 

    public class AudioDevice 

{

AudioTrack track; 
    short[] buffer = new short[1024]; 
    Context context; 

    public AudioDevice(Context context_) 
    { 
     int minSize =AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);   
     track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, 
             AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, 
             minSize, AudioTrack.MODE_STREAM); 
     context=context_; 
     track.play();   
    } 



    public void PlayShortAudioFile() throws IOException 
    { 
    InputStream in=context.getResources().openRawResource(R.raw.cheerapp); 


    byte[] music = null; 
    music = new byte[in.available()]; 

    music=convertStreamToByteArray(in); 
    in.close(); 

     track.play(); 
     track.write(music, 0, music.length); 
    track.stop(); 
    track.release(); 
    } //Play 



    public static byte[] convertStreamToByteArray(InputStream is) throws IOException { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     byte[] buff = new byte[10240]; 
     int i = Integer.MAX_VALUE; 
     while ((i = is.read(buff, 0, buff.length)) > 0) { 
      baos.write(buff, 0, i); 
     } 

     return baos.toByteArray(); // be sure to close InputStream in calling function 
    } 

}

+0

먼저 샘플 속도, 스테레오/모노 및 인코딩 구성을 다시 확인 했습니까? 실제 오디오와 일치하는지 확인 하시겠습니까? – Geobits

답변

1

죄송하지만,이 오디오 파일은 좋았지 내 안에 헤더를 포함하는 MPEG 파일입니다 당신이 사용하는 thod는 헤더가없는 원시 오디오 파일에서만 작동합니다. mp3 파일 사용을 원할 경우 Mediaplayer (http://developer.android.com/reference/android/media/MediaPlayer.html)

+0

좋아, 나는 파도로 변환. –