AudioTrack only supports raw PCM data

-niko

On Sep 7, 7:20 am, OldSkoolMark <[email protected]> wrote:
> Once you address the file not found issue, you will discover that
> there is currently no API that allows you to read in audio files and
> decode them into a format that AudioTrack understands. Try MediaPlayer
> instead unless you really need to work at the sample level.
>
> On Sep 6, 6:52 pm, melo <[email protected]> wrote:
>
>
>
> > Hi all
> > I'm an android starter
> > I tused AudioTrack to play music
> > But it just tell me filenotfoundexeception
> > I try to play mp3 wav ogg (all in raw dir)
> > I've no idea where is wrong?
> > here are my code ~
> > Could any one tell me why?
> > TKS in advance :)
>
> > public class mainActivity extends Activity
> > {
> >         private static final String Tag = "audioRecorder";
> >         private static final boolean log = true;
>
> >         AudioTrack track = null;
>
> >     /** Called when the activity is first created. */
> >     @Override
> >     public void onCreate(Bundle savedInstanceState)
> >     {
> >         super.onCreate(savedInstanceState);
> >         setContentView(R.layout.main);
>
> >         //if(AudioTrack.MODE_STATIC != mode && AudioTrack.MODE_STREAM !
> > = mode)
> >                 //throw new InvalidParameterException();
>
> >                 //String audioFilePath = "R.raw.music";
> >                 long fileSize = 0;
> >                 long bytesWritten = 0;
> >                 int bytesRead = 0;
> >                 int bufferSize = 0;
> >                 byte[] buffer;
> >                 //AudioTrack track = null;
> >                 int sampleRateHz = 8000;//22050
> >                 int audioFormat = AudioFormat.CHANNEL_CONFIGURATION_MONO;
> >                 //int audioFormat = 
> > AudioFormat.CHANNEL_CONFIGURATION_STEREO;
> >                 //int audioEncodingFormat= 
> > MediaRecorder.AudioEncoder.AMR_NB;
> >                 int audioEncodingFormat= AudioFormat.ENCODING_PCM_16BIT;
> >                 //int audioEncodingFormat= AudioFormat.ENCODING_PCM_8BIT;
> >                 //int mode = AudioTrack.MODE_STATIC;
> >                 int mode = AudioTrack.MODE_STREAM;
>
> >                 File audioFile = new File("R.raw.test.ogg");
> >                 fileSize = audioFile.length();
>
> >                 if(log)Log.d(Tag,"Buffer size("+ mode +")");
> >                 if(AudioTrack.MODE_STREAM == mode)
> >                   bufferSize =
> > AudioTrack.getMinBufferSize(sampleRateHz,audioFormat,audioEncodingFormat);
> >                 else //AudioTrack.MODE_STATIC
> >                   bufferSize = (int)fileSize;
>
> >                 buffer = new byte[(int)bufferSize];
>
> >                 if(log)Log.d(Tag,"track setting");
> >                 track = new
> > AudioTrack(AudioManager.STREAM_MUSIC,sampleRateHz,
> >                                 audioFormat,audioEncodingFormat,
> >                                 bufferSize,mode);
>
> >                 // in stream mode,
> >                 // 1. start track playback
> >                 // 2. write data to track
>
> >                 if(AudioTrack.MODE_STREAM == mode)
> >                 {
> >                   if(log)Log.d(Tag,"MODE_STREAM:track.play()");
> >                   track.play();
> >                 }
> >                 FileInputStream audioStream = null;
> >                 try{
> >                 audioStream = new FileInputStream(audioFile);//"R.raw.music"
> >                 }catch(FileNotFoundException e) {
> >                 e.printStackTrace();
> >                 Log.e(Tag,"Error:" + e.toString());
> >                 }
>
> >                 if(log)Log.d(Tag,"audioStream read");
> >                 while(bytesWritten < fileSize)
> >                 {
> >                   try
> >                   {
> >                      bytesRead = audioStream.read(buffer,0,bufferSize);
> >                   }catch(IOException e){
> >                           Log.e(Tag,"Error:" + e.toString());
> >                    }
> >                    if(log)Log.d(Tag,"track write");
> >                    bytesWritten += track.write(buffer,0,bytesRead);
> >                 }
> >         // in static mode,
> >                 // 1. write data to track
> >                 // 2. start track playback
>
> >                 if(AudioTrack.MODE_STATIC == mode)
> >                 {
> >                   if(log)Log.d(Tag,"MODE_STATIC:track.play()");
> >                   track.play();
> >                 }
>
> >         }
> >     }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to