Stream Play online audio mp3 from URL in android without downloading

How to programmatically only run Mp3 files online inside android app on button click.

Online audio playing android application is now most popular android apps because with this type of apps any one can easily listen online music from any where, any place. These apps play music from online via internet connection. This is tutorial is one of the easiest basic example to learn online audio streaming in android applications via HTTP URL. The music track which we are playing in this tutorial inside our android application is already stored on our server, So here is the complete step by step tutorial for Stream Play online audio mp3 from URL in android without downloading.

android-project-download-code-button

How to Stream Play online audio mp3 from URL in android without downloading.

Code for Please add internet permission to your AndroidManifest.xml file.

 <uses-permission android:name="android.permission.INTERNET" />

Code for MainActivity.java file.

 package com.playonlineaudiomp3_android_examples.com;

import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

 Button buttonStop,buttonStart ;
 
 String AudioURL = "https://www.android-examples.com/wp-content/uploads/2016/04/Thunder-rumble.mp3";
 
 MediaPlayer mediaplayer;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 buttonStart = (Button)findViewById(R.id.button1);
 buttonStop = (Button)findViewById(R.id.button2);
 
 mediaplayer = new MediaPlayer();
 mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
 
 buttonStart.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 try {
 
 mediaplayer.setDataSource(AudioURL);
 mediaplayer.prepare();
 
 
 } catch (IllegalArgumentException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (SecurityException 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();
 }
 
 mediaplayer.start();
 
 
 }
 });
 
 buttonStop.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 
 mediaplayer.stop();
 
 
 }
 });
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.playonlineaudiomp3_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="Stop Music From URL" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="157dp"
 android:text="Start Music From URL" />

</RelativeLayout>

Screenshot:

Stream Play online audio mp3 from URL in android without downloading

Click here to download Stream Play online audio mp3 from URL in android without downloading project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *