无法正确播放声音使用Android AudioTrack

问题描述:

我正在运行此程序,它使用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 2013-02-27 01:26:30

抱歉,但这些音频文件是包含在后援我头MPEG文件如果你想播放mp3文件,请使用Mediaplayer(http://developer.android.com/reference/android/media/MediaPlayer.html

+0

好吧,我转换成波。 – 2013-02-27 07:33:37