2016-09-28 5 views
0

우리는 백그라운드 서비스에서 내부 저장소에 MP3 파일을 다운로드해야하는 android 응용 프로그램을 만들고 있습니다. 아래에 언급 된 코드 스 니펫을 intent service과 함께 사용하여 구현했습니다.하지만 플레이어가이 오디오 파일을 지원하지 않는 것처럼 오류가 발생하는 mp3를 재생하려고 할 때. 어떤 사람이이 문제에 대한 해결책을 가지고 있다면, 같은 것을 알려주십시오.mp3 파일 다운로드 및 재생

코드 조각 :

String urlPath = intent.getStringExtra(URL); 
    String fileName = intent.getStringExtra(FILENAME); 
    // String fileName = Environment.getExternalStorageDirectory().toString() + "/trail.mp3"; 
    File output = new File(Environment.getExternalStorageDirectory(), 
      fileName); 

    File dir = new File(output.getAbsolutePath() 
      + "/TRIALS/"); 

    dir.mkdirs(); 
    String path = dir + "/" +"trail" + ".mp3"; 
    File f = new File(path); 


    if (f.exists()) { 
     f.delete(); 
    } 

    InputStream stream = null; 
    FileOutputStream fos = null; 
    try { 

     URL url = new URL(urlPath); 
     URLConnection conection = url.openConnection(); 
     conection.connect(); 
     // getting file length 
     int lenghtOfFile = conection.getContentLength(); 
     //URL url = new URL(urlPath); 
     InputStream input = new BufferedInputStream(url.openStream()); 

     stream = url.openConnection().getInputStream(); 
     InputStreamReader reader = new InputStreamReader(stream); 
     fos = new FileOutputStream(f.getPath()); 

     byte data[] = new byte[lenghtOfFile]; 

     long total = 0; 

     int times = -1; 
     while ((times = reader.read()) != -1) { 
      fos.write(times); 

     } 
     // Successful finished 
     result = Activity.RESULT_OK; 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (stream != null) { 
      try { 
       stream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (fos != null) { 
      try { 
       fos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    publishResults(output.getAbsolutePath(), result); 
} 

답변

0

1.Check는 urlPath는 correct.You는 탐험가하여 열고 .mp3 다운로드되기 시작하면 확인할 수있는 경우.

2. 코드를 통해 다운로드 한 파일로 탐색기로 가져온 파일의 크기가 비슷합니다.

크기가 다를 3.If는

, 즉 다음과 같이 코드를 수정 다운로드 failure.Try을 의미한다 : 나는 몇 가지 쓸모없는 코드를 제거하고 fos.close() 전에 fos.flush()를 추가

String urlPath = intent.getStringExtra(URL); 
    String fileName = intent.getStringExtra(FILENAME); 
    File output = new File(Environment.getExternalStorageDirectory(), 
      fileName); 
    File dir = new File(output.getAbsolutePath() 
      + "/TRIALS/"); 
    dir.mkdirs(); 
    String path = dir + "/" +"trail" + ".mp3"; 
    File f = new File(path); 

    if (f.exists()) { 
     f.delete(); 
    } 

    InputStream stream = null; 
    FileOutputStream fos = null; 
    try { 

     URL url = new URL(urlPath); 
     URLConnection urlcon = url.openConnection(); 
     stream = urlcon.getInputStream(); 

     InputStreamReader reader = new InputStreamReader(stream); 
     fos = new FileOutputStream(f.getPath()); 

     int times = -1; 
     while ((times = reader.read()) != -1) { 
      fos.write(times); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (stream != null) { 
      try { 
       stream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     if (fos != null) { 
      try { 
       fos.flush(); 
       fos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

.