android - How can i use AudioTrack to play mp3 file and also seek to position -


iam trying develop application can set speed of music file(mp3) set 1x,1.5x,2x,2.5x this.but mediaplayer not support feauture unless 23 api.how can use audiotrack play mp3 file , seek position.the below code gives me "zzzzzzz" sound.

public void playaudio(){         int minbuffersize = audiotrack.getminbuffersize(8000, audioformat.channel_configuration_mono, audioformat.encoding_pcm_16bit);         int buffersize = 512;         audiotrack = new audiotrack(audiomanager.stream_music, 8000, audioformat.channel_configuration_mono, audioformat.encoding_pcm_16bit, minbuffersize, audiotrack.mode_stream);         string filepath = environment.getexternalstoragedirectory().getabsolutepath();          int = 0;         byte[] s = new byte[buffersize];         try {             final string path= environment.getexternalstoragedirectory().getabsolutepath() + "/folioreader/audio"+".mp3";             fileinputstream fin = new fileinputstream(path);             datainputstream dis = new datainputstream(fin);              audiotrack.play();             while((i = dis.read(s, 0, buffersize)) > -1){                 audiotrack.write(s, 0, i);              }             audiotrack.stop();             audiotrack.release();             dis.close();             fin.close();          } catch (filenotfoundexception e) {             // todo             e.printstacktrace();         } catch (ioexception e) {             // todo             e.printstacktrace();         }     } 

audiotrack can play uncompressed audio (wav) not compressed ones (mp3 or aac etc.,) need call mediacodec decode , use audio track play audio. refer these links, https://developer.android.com/reference/android/media/audiotrack.html , https://developer.android.com/reference/android/media/mediacodec.html.

for faster playback, give sample rate proportional speed require. ex. play 8khz audio in 2x rate, give 16khz in audiotrack , on. crude way.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -