2013-04-24 68 views
0

我的應用程序包含一個無線電流媒體功能,可以從一個url加載mp3格式的數據。我播放基於以下代碼的現場直播:來自網址的無線電流媒體播放器

public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{ 

    private ImageButton buttonPlayPause; 
     private SeekBar seekBarProgress; 
     public EditText editTextSongURL; 
     private MediaPlayer mediaPlayer; 
     // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class 
     private int mediaFileLengthInMilliseconds; 

     private final Handler handler = new Handler(); 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      initView(); 
     } 

     /** This method initialise all the views in project*/ 
     private void initView() { 
      buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause); 
      buttonPlayPause.setOnClickListener(this); 

      seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay); 
      seekBarProgress.setMax(99); // It means 100% .0-99 
      seekBarProgress.setOnTouchListener(this); 
      editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL); 
      editTextSongURL.setText(R.string.testsong_20_sec); 

      mediaPlayer = new MediaPlayer(); 
      mediaPlayer.setOnBufferingUpdateListener(this); 
      mediaPlayer.setOnCompletionListener(this); 
     } 

     /** Method which updates the SeekBar primary progress by current song playing position*/ 
     private void primarySeekBarProgressUpdater() { 
      seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length" 
      if (mediaPlayer.isPlaying()) { 
       Runnable notification = new Runnable() { 
        public void run() { 
         primarySeekBarProgressUpdater(); 
        } 
       }; 
       handler.postDelayed(notification,1000); 
      } 
     } 

     @Override 
     public void onClick(View v) { 
      if(v.getId() == R.id.ButtonTestPlayPause){ 
       /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */ 
       try { 
        mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source 
        mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL 

       if(!mediaPlayer.isPlaying()){ 
        mediaPlayer.start(); 
        buttonPlayPause.setImageResource(R.drawable.button_pause); 
       }else { 
        mediaPlayer.pause(); 
        buttonPlayPause.setImageResource(R.drawable.button_play); 
       } 

       primarySeekBarProgressUpdater(); 
      } 
     } 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if(v.getId() == R.id.SeekBarTestPlay){ 
       /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/ 
       if(mediaPlayer.isPlaying()){ 
        SeekBar sb = (SeekBar)v; 
        int playPositionInMillisecconds = (mediaFileLengthInMilliseconds/100) * sb.getProgress(); 
        mediaPlayer.seekTo(playPositionInMillisecconds); 
       } 
      } 
      return false; 
     } 

     @Override 
     public void onCompletion(MediaPlayer mp) { 
      /** MediaPlayer onCompletion event handler. Method which calls then song playing is complete*/ 
      buttonPlayPause.setImageResource(R.drawable.button_play); 
     } 

     @Override 
     public void onBufferingUpdate(MediaPlayer mp, int percent) { 
      /** Method which updates the SeekBar secondary progress by current song loading from URL position*/ 
      seekBarProgress.setSecondaryProgress(percent); 
     } 
    } 

我的問題是此代碼不適用於具有約一小時長度的mp3網址。是否有人知道如何從剛剛試過答案1打漫長的MP3鏈接

更新:

public class Mp3 extends Activity implements OnClickListener { 

    Button button; 
    private MediaPlayer mediaPlayer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.button); 


     button = (Button)findViewById(R.id.button1); 
     button.setOnClickListener(this); 


     mediaPlayer = new MediaPlayer(); 


    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     switch(v.getId()){ 

     case R.id.button1: 

      startSound("http://str2.creacast.com:80/pha-michy1"); 

      break; 


     } 
    } 


    public void startSound(final String URL) { 
     button.setText("STOP"); 
      new Thread() { 
      public void run() { 
       try { 
        Uri uri = Uri.parse(URL); 

        mediaPlayer = MediaPlayer.create(Mp3.this, uri); 
        mediaPlayer.start(); 
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() { 
         @Override 
         public void onCompletion(MediaPlayer arg0) { 
          button.setEnabled(true); 
          button.setText("PLAY"); 
         } 
        }); 
       } catch (Exception e) { 

       } 
       // myProgressDialog.dismiss(); 
      } 
      }.start(); 
     } 




} 

我的日誌顯示此錯誤。

05-24 09:09:02.652: I/MediaPlayer(1548): uri 
05-24 09:09:02.652: I/MediaPlayer(1548): path is null 
05-24 09:09:02.652: D/MediaPlayer(1548): Couldn't open file on client side, trying server side 
05-24 09:09:16.707: E/MediaPlayer(1548): error (1, -2147483648) 
05-24 09:09:16.707: D/MediaPlayer(1548): create failed: 
05-24 09:09:16.707: D/MediaPlayer(1548): java.io.IOException: Prepare failed.: status=0x1 
05-24 09:09:16.707: D/MediaPlayer(1548): at android.media.MediaPlayer.prepare(Native Method) 
05-24 09:09:16.707: D/MediaPlayer(1548): at android.media.MediaPlayer.create(MediaPlayer.java:636) 
05-24 09:09:16.707: D/MediaPlayer(1548): at android.media.MediaPlayer.create(MediaPlayer.java:613) 
05-24 09:09:16.707: D/MediaPlayer(1548): at com.hrupin.streamingmedia.Mp3$1.run(Mp3.java:60) 

強大的文本* 更新:當我嘗試你的第二個鏈接我在日誌中得到這個錯誤。你能檢查一下嗎? *強大的文本

05-24 09:59:14.398: D/dalvikvm(2228): Debugger has detached; object registry had 1 entries 05-24 09:59:14.582: E/MediaPlayer(2228): start called in state 4 05-24 09:59:14.582: E/MediaPlayer(2228): error (-38, 0) 05-24 09:59:14.585: E/MediaPlayer(2228): Error (-38,0) 05-24 09:59:14.632: E/MediaPlayer(2228): error (1, -1002) 05-24 09:59:14.683: E/MediaPlayer(2228): Error (1,-1002) 05-24 10:00:07.843: I/MediaPlayer(2371): uri is:http://www.usa8-vn.mixstream.net:8138 05-24 10:00:07.843: I/MediaPlayer(2371): path is null 05-24 10:00:07.847: D/MediaPlayer(2371): Couldn't open file on client side, trying server side 05-24 10:00:07.863: E/MediaPlayer(2371): error (1, -1002) 05-24 10:00:07.863: D/MediaPlayer(2371): create failed: 05-24 10:00:07.863: D/MediaPlayer(2371): java.io.IOException: Prepare failed.: status=0x1 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.prepare(Native Method) 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.create(MediaPlayer.java:636) 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.create(MediaPlayer.java:613) 05-24 10:00:07.863: D/MediaPlayer(2371): at com.hrupin.streamingmedia.Mp3$1.run(Mp3.java:68) 05-24 10:00:38.300: I/MediaPlayer(2411): uri is:http://usa8-vn.mixstream.net:8138 05-24 10:00:38.300: I/MediaPlayer(2411): path is null 05-24 10:00:38.300: D/MediaPlayer(2411): Couldn't open file on client side, trying server side 

回答

0

試試這個代碼,讓我知道

public void startSound(final String URL) { 
    bt.setText("STOP"); 
    new Thread() { 
    public void run() { 
     try { 
      Uri uri = Uri.parse(URL); 

      mp = MediaPlayer.create(PlaySound.this, uri); 
      mp.start(); 
      mp.setOnCompletionListener(new OnCompletionListener() { 
       @Override 
       public void onCompletion(MediaPlayer arg0) { 
        bt.setEnabled(true); 
        bt.setText("SŁUCHAJ"); 
       } 
      }); 
     } catch (Exception e) { 

     } 
     myProgressDialog.dismiss(); 
    } 
    }.start(); 
} 

當我按下後退按鈕:

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
      mp.stop(); 
    } 
} 

來源從 this

得到試試這個

String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3"; 
    MediaPlayer mp = new MediaPlayer(); 
    try { 
    mp.setDataSource(url); 
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
    mp.prepareAsync(); 
    mp.start(); 
    } 
catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
+0

沒有結果我會更新我的問題與您的答案和日誌 – Christina 2013-04-24 05:09:47

+0

@Christina你試圖玩哪個網址? – 2013-04-24 05:10:53

+0

此網址http://str2.creacast.com:80/pha-michy1 – Christina 2013-04-24 05:12:17