2011-11-16 65 views
-2

我只需要知道如何在Android中播放下面的鏈接...我在模擬器中嘗試它的工作,但不是在設備上爲什麼.....幫助表示讚賞.... ..在Android中使用WebView播放URL

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

過去的一些代碼在這裏以供參考你在哪裏打.. –

+0

海索尼我也不能張貼代碼... u能幫助我如何打這個網址........ – user1048958

+0

爲什麼不試試與媒體播放器.. –

回答

2

已經我已經實現發揮在我app.Directly流式音頻我在這裏粘貼此代碼卸下襬臂不必要的數據,並使用它。我已經測試你在它的鏈接,爲我工作。

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> 
+0

我的設備也是對的索尼,,,,,, – user1048958

+0

我試過了,但我得到這些除外離子..... bufferCount(4)太小,增加到12 11-16 15:15:35.384:ERROR/AwesomePlayer(34):不發送緩衝狀態,因爲持續時間未知。 – user1048958

+0

其實這個問題可能會發生在使用player.prepare()而不是player.prepareAsync()。但實際上,我把上面的代碼正確的。它完全爲我工作。並按照鏈接在這裏:http://stackoverflow.com/questions/ 3357923/stagefright-media-delay-in-2-2-after-setting-data-source/3376420#3376420 –