2012-12-07 91 views
0

有人可以告訴我或教我如何在一個文件中下載5個或更多mp3並在我的應用中播放它們。我已經搜尋過,但所有人都問過這個問題,沒有任何解釋。我不想只下載一個MP3,但在file.here多的MP3是main.java如何下載mp3文件並在我的應用程序中播放它們?

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

private ImageButton buttonPlayPause; 
private SeekBar seekBarProgress; 
public EditText editTextSongURL; 

private MediaPlayer mediaPlayer; 
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class 

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); 
} 
+2

您的應用程序使用什麼技術編寫?你已經做了什麼?因爲我非常確定MP3播放器在大多數技術中都是可用的。 –

+1

@FrankvanPuffelen不在Java中:)您需要使用第三方庫來支持MP3播放。請參閱http://en.wikipedia.org/wiki/Java_Media_Framework#Alternatives 對於下載部分,使用'URLConnection'獲取InputStream。 –

+0

@LukasKnuth我會叫某些第三方庫隨時可用。 ;-)但的確,這取決於技術。我可以建議你將你的評論轉換成答案嗎? –

回答

0

你需要使用第三方庫,支持MP3播放功能,因爲它不包括在標準庫。請參閱Wikipedia瞭解替代方案的列表。

對於下載部分,請使用URLConnection獲取文件上的InputStream並將其寫入FileOutputStream。這也可能有所幫助:Working unbuffered Streams

相關問題