Skip to content

Simple demo of Android network mp3 music player using AudioTrack and jlayer.

Notifications You must be signed in to change notification settings

ChowHo/AudioTrackMp3Player

 
 

Repository files navigation

AudioTrackMp3Player

Extremely simple network mp3 music player for Android using AudioTrack and jlayer.

Steps

  1. Include jlayer library into your project.

jlayer directory in this demo: /app/libs/jl1.0.1.jar

  1. Create a Decoder from jlayer java library.

     Decoder mDecoder = new Decoder();
    
  2. Build an InputStream of your mp3 url source and feed it into BitStream.

     InputStream in = new URL("http://icecast.omroep.nl:80/radio1-sb-mp3")
             .openConnection()
             .getInputStream();
     Bitstream bitstream = new Bitstream(in);
    
  3. Create an AudioTrack instance.

     final int sampleRate = 44100;
     final int minBufferSize = AudioTrack.getMinBufferSize(sampleRate,
             AudioFormat.CHANNEL_OUT_STEREO,
             AudioFormat.ENCODING_PCM_16BIT);
    
     AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
             sampleRate,
             AudioFormat.CHANNEL_OUT_STEREO,
             AudioFormat.ENCODING_PCM_16BIT,
             minBufferSize,
             AudioTrack.MODE_STREAM);
    
  4. Decode the coming mp3 stream by Decoder and feed PCM chunks to AudioTrack.

     Header header;
     while (framesReaded-- > 0 && (header = bitstream.readFrame()) != null) {
         SampleBuffer sampleBuffer = (SampleBuffer) mDecoder.decodeFrame(header, bitstream);
         short[] buffer = sampleBuffer.getBuffer();
         mAudioTrack.write(buffer, 0, buffer.length);
         bitstream.closeFrame();
     }
    

About

Simple demo of Android network mp3 music player using AudioTrack and jlayer.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%