Demo:
1) MainActivity.java:
2) activity_main.xml:
3) Permission:
1) MainActivity.java:
import
java.io.IOException;
import
android.app.Activity;
import
android.content.Context;
import
android.media.AudioManager;
import
android.media.MediaPlayer;
import
android.net.Uri;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class MainActivity extends Activity implements OnClickListener
{
private MediaPlayer mediaPlayer;
private AudioManager audioManager;
private String Url = "<Paste
your Audio url>";
/*
*
This is Sample Url i am search in google
*
*
http://server11.mp3quran.net/hawashi/002.mp3
*/
Button btnStop, btnPlay, btnPause;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// activate Audio Service
audioManager =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
// create media player
mediaPlayer = new MediaPlayer();
// set audio type to media palyer
mediaPlayer.setAudioStreamType(audioManager.STREAM_MUSIC);
// call button references
btnStop = (Button) findViewById(R.id.buttonStop);
btnPlay = (Button) findViewById(R.id.buttonPlay);
btnPause = (Button) findViewById(R.id.buttonPause);
// set listeners for buttons
btnStop.setOnClickListener(this);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO
Auto-generated method stub
switch (v.getId()) {
case R.id.buttonStop:
// to stop media player
mediaPlayer.stop();
// to release media player
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
// create media player
mediaPlayer = new MediaPlayer();
// enable and disable media palyer
buttons
btnStop.setEnabled(false);
btnPlay.setEnabled(true);
btnPause.setEnabled(true);
break;
case R.id.buttonPlay:
// to play media player call
this method
onlineStreaming(Url);
// enable and disable media palyer
buttons
btnStop.setEnabled(true);
btnPlay.setEnabled(false);
btnPause.setEnabled(true);
break;
case R.id.buttonPause:
// pause media player
mediaPlayer.pause();
// enable and disable media palyer
buttons
btnStop.setEnabled(true);
btnPlay.setEnabled(true);
btnPause.setEnabled(false);
break;
}
}
public void
onlineStreaming(String url) {
Uri tempUri = Uri.parse(url);
try {
// set data for media player
mediaPlayer.setDataSource(this, tempUri);
} catch
(IllegalArgumentException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
} catch
(IllegalStateException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
try {
// prepare media player
mediaPlayer.prepare();
} catch
(IllegalStateException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
// start playing media player
mediaPlayer.start();
}
}
2) activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F04B4"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="U must wait 10
seconds after click play button then only song will be played"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
>
<Button
android:id="@+id/buttonStop"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop"
/>
<Button
android:id="@+id/buttonPlay"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
/>
<Button
android:id="@+id/buttonPause"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
/>
</LinearLayout>
</LinearLayout>
3) Permission: