0

I want to read the audio file from sdcard into byte array. and play the file using this byte array into AudioTrack.

Like AudioTrack play file in this way

   track.play();                       
   track.write(byte_Arr,0,byte_Arr.length);
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81

2 Answers2

1

The simplest way to play audio in such a scenario is to read a "wav" file. If you want to read another format such as "mp3" you will have to use another audio player class such MediaPlayer. An example of its usage is as follows:

String filePath = Environment.getExternalStorageDirectory()+"/yourfolderName/yourfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();   
mediaPlayer.start();

If you still want to use AudioTrack for playing then you need a decoder. This can be a much complicated task so we will carry on "wav" files.

A "wav"file has an header like other formats such as "mp3".

In order to do this, you have to read the header. You have to find out the sample rate, bit depth, and channels configuration of the file. You can find out how to get those from a header by a research. I'm not sure if you actually want to read a "wav" file so not giving further details on headers but you may check this out:

Wave format

If you simply know the sample rate, bit depth and channel configuration then you may bypass the header. It usually occupies the first 44 bytes of the file then the raw PCM data follows. You may read that data with BufferedReader.

String filePath = Environment.getExternalStorageDirectory()+"/yourfolderName/yourfile.wav";
BufferedReader br = new BufferedReader(new FileReader(filePath));
File mFile = new File(filePath);
long fLength = mFile.length();
char[fLength - 44] buffer;
br.Read(buffer, 44, fLength - 44);
br.close();
// Additional tasks...

I'm not sure about the previous code above because I didn't try it.

mtpc1486
  • 111
  • 5
  • You should save your record in wav format. Check this out: [link](http://stackoverflow.com/questions/5245497/how-to-record-wav-format-file-in-android) You can load it up then chance its frequency. Do you want a code example? – mtpc1486 Sep 04 '15 at 19:28
  • Yes brother it will be great if you provide me code example, thanks – Pir Fahim Shah Sep 04 '15 at 19:30
  • This is for recording audio from mic and then saving it to a file: [link](http://www.edumobile.org/android/audio-recording-in-wav-format-in-android-programming/). Also check this out: [link](http://stackoverflow.com/questions/3925030/using-audiotrack-in-android-to-play-a-wav-file). Sorry for not giving a full example. Use `BufferedReader` to read the content of the file. In my answers last code example, there is a char array named `buffer`. Write that buffer to the `AudioTrack` using `Write` function of the `AudioTrack`. Use `SetPlaybackRate` function to change the frequency. – mtpc1486 Sep 04 '15 at 19:55
  • Where did you actually stuck? – mtpc1486 Sep 04 '15 at 19:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88882/discussion-between-pir-fahim-shah-and-mtpc1486). – Pir Fahim Shah Sep 05 '15 at 15:54
  • I used the two links you provided and now i am able to do the specific job. Thank you so much – Pir Fahim Shah Sep 06 '15 at 07:18
0

Instead of reading byte array from Audio file, you can set the path like this

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.mp3");
mp.prepare();
mp.start();
Ajay Shrestha
  • 2,433
  • 1
  • 21
  • 25
  • be careful with the mp3 license! – Soley Sep 04 '15 at 19:00
  • @Salivan what do you mean? :D – Ajay Shrestha Sep 04 '15 at 19:05
  • @AjayShrestha actually i want to play the file using audiotrack, because in audio track i can change the frequency, hence i can change one sound to another. I am developing voice changer application – Pir Fahim Shah Sep 04 '15 at 19:13
  • I mean developing an application using MP3 technology requires you to pay a royalty fee. So it is better to use other codecs than mp3 if possible. :) I didn't know this until I found it while checking for licenses for my application. – Soley Sep 06 '15 at 06:07