开发者

Android:Playing bigger size audio wav sound file produces crash

I am trying to p开发者_JS百科lay the bigger size audio wav file(which is >20 mb) using the following code(AudioTrack) on my Android 1.6 HTC device which basically has less memory. But i found device crash as soon as it executes reading, writing and play. But the same code works fine and plays the lesser size audio wav files(10kb, 20 kb files etc) very well. P.S: I should play PCM(.wav) buffer sound, the reason behind why i use AudioTrack here.

Though my device has lesser memory, how would i read bigger audio files bytes by bytes and play the sound to avoid crashing due to memory constraints.

private void AudioTrackPlayPCM() throws IOException
{
    String filePath = "/sdcard/myWav.wav"; // 8 kb file

    byte[] byteData = null;             
    File file = null; 

    file = new File(filePath);

    byteData = new byte[(int) file.length()];

    FileInputStream in = null;

    try {
        in = new FileInputStream( file );
        in.read( byteData );
        in.close(); 
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
    AudioFormat.ENCODING_PCM_8BIT);     

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
    AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);        

    at.play();

    at.write(byteData, 0, byteData.length);         
    at.stop();

    at.release();
}

Could someone guide me please to play the AudioTrack code for bigger size wav files?


Your intSize will be the minimum buffer size needed to init the AudioTrack, try using a multiple of this to create a bigger buffer

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, 
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT, intSize*10, 
AudioTrack.MODE_STREAM); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜