본문 바로가기

Stream Play online audio mp3 from URL in android without downloading

반응형

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 = "http://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>

 

반응형

댓글


Copyright ⓒ SmartWeb All rights reserved.