2013-02-28 7 views
0
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
      AudioFormat.ENCODING_PCM_16BIT); 

    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
     AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM); 


    playfilesound(); 
} 


private void playfilesound() throws IOException 
{ 




    int count = 512 * 1024; // 512 kb 
    //Reading the file.. 
    byte[] byteData = null; 
    File file = null; 
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound"); //filePath 


    byteData = new byte[(int)count]; 
    FileInputStream in = null; 
    try { 
    in = new FileInputStream(file); 

    } catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 


    int bytesread = 0, ret = 0; 
    int size = (int) file.length(); 
    audioTrack.play(); 



    while (bytesread < size) { // Write the byte array to the track 
     ret = in.read(byteData,0, count); //ret =size in bytes 

     if (ret != -1) { 
      audioTrack.write(byteData,0, ret); 
      bytesread += ret; } //ret 
     else break; 

    } //while 



    in.close(); 
    audioTrack.stop(); audioTrack.release(); 
    } 

J'ai utilisé le débogueur pour parcourir le code et survoler audioTrack, il est alloué et initié. Le fichier existe également. Mais quand il frappe audioTrack.play() il lance une erreur de dire son exception d'état illégal, AudioTrack non utilisé.Android AudioTrack Error: non initialisé AudioTrack

J'ai joint le projet qui comprend la partie du fichier d'enregistrement. http://www.mediafire.com/?6i2r3whg7e7rs79

Répondre

1

On dirait que vous avez appelé jouer avant d'écrire! Essayez ceci ...

int bytesread = 0, ret = 0; 
int size = (int) file.length(); 
//audioTrack.play(); <---- play called prematurely 



while (bytesread < size) { // Write the byte array to the track 
    ret = in.read(byteData,0, count); //ret =size in bytes 

    if (ret != -1) { 
     audioTrack.write(byteData,0, ret); 
     bytesread += ret; 
     audioTrack.play(); //<--- try calling it here! 
    } //ret 
    else break; 

} //while 
1

La configuration du canal que vous utilisez est interrompue, à la place de AudioFormat.CHANNEL_CONFIGURATION_MONO Utilise AudioFormat.CHANNEL_IN_MONO Pour enregistrer et jouer AudioFormat.CHANNEL_OUT_MONO ...

+1

S'il vous plaît utiliser l'anglais et que l'anglais ici sur le SO . –

+0

Pour grâce, utilisez les langues et bien sûr les choses en ce moment. – NobodyNada

Questions connexes