2012-12-07 2 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 재생을 지원하기 위해 제 3의 라이브러리를 사용해야합니다

표준 라이브러리 대체 목록은 Wikipedia을 참조하십시오.

다운로드 부분은 URLConnection을 사용하여 파일에 InputStream을 가져오고 FileOutputStream에 작성하십시오. 도움이 될 수도 있습니다. Working unbuffered Streams

관련 문제