Play online audio using media player in Android

package com.bhadreshtech.Activity;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;

import com.bhadreshtech.R;

/** *  */public class AudioPlayer extends Activity {

    MediaPlayer mp;
    String SONG_URL = "your url hear";

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session_layout);

        mp = new MediaPlayer();
        if (mp != null) {
            playaudio();
        } else {
            mp = new MediaPlayer();
            playaudio();
        }
    }

    private void playaudio() {
        mp.reset();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setDataSource(SONG_URL);
        mp.prepareAsync();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override            public void onPrepared(MediaPlayer mp) {
                mp.start(); //start your music hear            }
        });
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override            public void onCompletion(MediaPlayer mp) {
                //do something after finish            }
        });

    }


}

No comments: