2011-09-15 8 views
1

정확하게 서버에서 .mp3 음악 파일을 다운로드 할 수있는 앱을 만들려고합니다.이 Android 개발 필드에서 신참으로 어떤 도움을 주시면 감사하겠습니다. 너희들에게서. 시작해야 할 것이 있습니다. 유용한 리소스를 얻을 수있는 링크를 제공 해주시면 정말 고맙겠습니다. 감사android의 서버에서 mp3 파일 다운로드

답변

2

당신이이 방법 ::

  try { 
        MediaPlayer player = new MediaPlayer(); 
     player.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     player.setDataSource("http://xty/MRESC/images/test/xy.mp3" 
       ); 
     player.prepare(); 
        player.star(); 

    } catch (Exception e) { 
     // TODO: handle exception 
    } 

매니페스트 권한을 수행 할 수 있습니다 어떤 URL에서 .MP3 파일을 재생하려면

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
+0

을 Thanx nik..i는 mp3 파일을 다운로드하고 코드는 플레이어 용입니다. – clayforbrick

+3

당신의 코드가 작동하는 동안, OP는 다운로드를 요청했고 코드는 – Jimmar

11

다음을 따르십시오 nik이 제안한 코드.

하지만 다음이 코드

private class DownloadFile extends AsyncTask<String, Integer, String>{ 
@Override 
protected String doInBackground(String... urlParams) { 
    int count; 
    try { 
     URL url = new URL("url of your .mp3 file"); 
     URLConnection conexion = url.openConnection(); 
     conexion.connect(); 
     // this will be useful so that you can show a tipical 0-100% progress bar 
     int lenghtOfFile = conexion.getContentLength(); 

     // downlod the file 
     InputStream input = new BufferedInputStream(url.openStream()); 
     OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3"); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      // publishing the progress.... 
      publishProgress((int)(total*100/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (Exception e) {} 
    return null; 
} 

편집에 따라 파일 서버를 형성하고 SDCARD 또는 내부 저장 장치에있는 모든 장소에 보관 다운로드하려면 : 매니페스트 권한 :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
+0

고맙습니다. 그러나 여전히 동적으로 입력하는 방법을 모르겠으므로 여전히 문제가 있습니다. 사용자가 링크를 복사하여 붙여 넣으면 텍스트 상자가 있으면 해당 사이트에서 파일 다운로드가 시작됩니다. – clayforbrick

+0

@clayforbrick - 사용자가 복사 - 편집 텍스트 상자에 링크를 붙여 넣으면 해당 편집 텍스트를 가져 와서 다운로드 파일 URL에 전달하십시오. url = 새 URL ("다운로드하려는 .mp3 파일의 URL")); 파일을 저장하는 경우 OutputStream output = new FileOutputStream ("/ sdcard/somewhere/nameofthefile saving file.mp3"); – user370305

+0

그리고 이것이 맞다면이 대답을 수락하십시오. 감사합니다 :-) – user370305

관련 문제