Skip to content Skip to sidebar Skip to footer

Playing A Url Using Webview In Android

I just need to know how to play this below link in android...i tried it in emulator its working but not on device why.....Help is appreciated...... http://stream.radiosai.net:8

Solution 1:

Already i have implemented play streaming audio in my app.Directly i paste this code here .Remove unnecessary data and use it.i have tested your link in it ,is working for me.

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;

publicclassStreamAudioextendsActivityimplementsOnPreparedListener,
         OnErrorListener {

    MediaPlayer mp;
    privateToggleButton btn;
    privateImageView img;
    privateboolean flag = false;
    AnimationDrawable frameAnimation;
    ProgressDialog progress;


     String url="http://stream.radiosai.net:8002/";

    @OverridepublicvoidonCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        btn = (ToggleButton) findViewById(R.id.play);
        img = (ImageView) findViewById(R.id.radio_image);

        img.setBackgroundResource(R.drawable.frames);
        frameAnimation = (AnimationDrawable) img.getBackground();

        mp = newMediaPlayer();

        progress=ProgressDialog.show(this, null ,"Loading...",false,true);

        Runnable r=newRunnable() {

            @Overridepublicvoidrun() {
                setPlayBack();
            }
        };
        Thread th=newThread(r);
        th.start();
            mp.setOnPreparedListener(this);
            //mp.setOnBufferingUpdateListener(this);
            mp.setOnErrorListener(this);
        //  mp.setOnInfoListener(this);
            btn.setOnCheckedChangeListener(newOnCheckedChangeListener() {

                @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(flag)
                    {

                        if(!isChecked)
                        {
                                btn.setBackgroundResource(R.drawable.btn_stop);
                                mp.start();
                                frameAnimation.start();
                        }

                        else
                        {
                            btn.setBackgroundResource(R.drawable.btn_play);
                            frameAnimation.stop();
                            mp.stop();
                            mp.reset();
                            flag=false;
                        }
                    }
                    else
                    {
                        btn.setChecked(false);

                        progress=ProgressDialog.show(StreamAudio.this, null ,"Loading...",false,false);
                        Runnable r=newRunnable() {

                            @Overridepublicvoidrun() {
                                // TODO Auto-generated method stubsetPlayBack();
                            }
                        };
                        Thread th=newThread(r);
                        th.start();
                    }

                }
            });
    }

    @OverrideprotectedvoidonDestroy() {

        super.onDestroy();
        mp.release();

    }

    @OverridepublicvoidonPrepared(MediaPlayer mp) {

        flag = true;
        handler.sendEmptyMessage(0);
    }


    @OverridepublicbooleanonError(MediaPlayer mp, int what, int extra) {

        mp.release();
        returnfalse;
    }
        privatevoidsetPlayBack()
        {
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mp.setDataSource(url);
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                } catch (IllegalStateException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                mp.prepareAsync();
        }
        privateHandler handler=newHandler(){
            @OverridepublicvoidhandleMessage(Message msg) {
                progress.dismiss();
                btn.setBackgroundResource(R.drawable.btn_stop);
                frameAnimation.start();
                mp.start();
            }
        };
}

main.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:layout_alignParentBottom="true"android:gravity="center"android:layout_width="fill_parent"android:background="@drawable/bottom_bar"android:layout_height="wrap_content"><ToggleButtonandroid:background="@drawable/btn_stop"android:checked="false"android:id="@+id/play"android:textOff=""android:textOn=""android:layout_width="wrap_content"android:layout_height="wrap_content"></ToggleButton></LinearLayout><ImageViewandroid:id="@+id/radio_image"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"></ImageView></RelativeLayout>

Post a Comment for "Playing A Url Using Webview In Android"