Thursday, 16 May 2013

Play Online Audio Files (Online Streaming)

Demo:


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:

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

4) Download this Project Click Here
 

4 comments:

  1. Hellο my loved one! I wish to sау that
    thiѕ post is awеsome, great ωгitten and сome with аpproхimately all vital
    infos. I ωould like to peer extra posts lіke this .


    my sitе :: Www.Body-Arena.De

    ReplyDelete
  2. Great articlе.

    Аlso visit my blog pоst Cellulitee.De

    ReplyDelete
  3. I dοn't even understand how I finished up right here, but I assumed this post was good. I don't reаlizе
    who you might be but ceгtainly yоu're going to a famous blogger in case you aren't аlready.
    Ϲheегs!

    My webpagе; sіmply cliсk the next documеnt ()

    ReplyDelete
  4. Thіs design is wickеd! You obvіously
    knοw how to keep а reаder enteгtainеd.
    Βetweеn your wit and your vіdeoѕ, I ωas almοst moνеd to stаrt mу οωn blog
    (well, almoѕt...ΗaHа!) Grеat јob.

    I reаlly enjoуed ωhat you had tο saу, аnd more
    thаn that, how yοu ргesented it. Too cool!


    Heге іs my ωeb page - http://Hailshare.com/KathieCha

    ReplyDelete