在android中使用WebView播放URL

时间:2011-11-16 05:06:35

标签: android url webview

我只需要知道如何在android中播放以下链接...我在模拟器中尝试了它的工作,但没有在设备上为什么.....的帮助表示赞赏......

   http://stream.radiosai.net:8002/

1 个答案:

答案 0 :(得分:2)

我已经在我的app中实现了播放流音频。直接我在这里粘贴此代码。删除不必要的数据并使用它。我已经测试了你的链接,对我有用。

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>