Playing a URL using WebView in android

2019-09-24 08:30发布

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:8002/

1条回答
叼着烟拽天下
2楼-- · 2019-09-24 08:55

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;

public class StreamAudio extends Activity implements OnPreparedListener,
         OnErrorListener {

    MediaPlayer mp;
    private ToggleButton btn;
    private ImageView img;
    private boolean flag = false;
    AnimationDrawable frameAnimation;
    ProgressDialog progress;


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

    @Override
    public void onCreate(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 = new MediaPlayer();

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

        Runnable r=new Runnable() {

            @Override
            public void run() {
                setPlayBack();
            }
        };
        Thread th=new Thread(r);
        th.start();
            mp.setOnPreparedListener(this);
            //mp.setOnBufferingUpdateListener(this);
            mp.setOnErrorListener(this);
        //  mp.setOnInfoListener(this);
            btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(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=new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                setPlayBack();
                            }
                        };
                        Thread th=new Thread(r);
                        th.start();
                    }

                }
            });
    }

    @Override
    protected void onDestroy() {

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

    }

    @Override
    public void onPrepared(MediaPlayer mp) {

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


    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {

        mp.release();
        return false;
    }
        private void setPlayBack()
        {
            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();
        }
        private Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                progress.dismiss();
                btn.setBackgroundResource(R.drawable.btn_stop);
                frameAnimation.start();
                mp.start();
            }
        };
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <LinearLayout android:layout_alignParentBottom="true"
        android:gravity="center" android:layout_width="fill_parent"
        android:background="@drawable/bottom_bar" android:layout_height="wrap_content">

        <ToggleButton android: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>
    <ImageView android:id="@+id/radio_image"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_centerInParent="true"></ImageView>

</RelativeLayout>
查看更多
登录 后发表回答