2016-05-31 4 views
0

다음 번에 인터넷없이 Dropbox에서 오디오 파일을 다운로드하려고합니다. 코드는 실제로 파일을 다운로드하지만 그 오디오를 재생하는 데 문제가 있습니다. , 내가 다운로드 한 파일이나 뭔가를 구문 분석 할 경우 내가 그것을 실행 포스트에서 재생할, 당신은안드로이드에서 mp3 파일을 다운로드하고 재생하십시오.

다운로드 파일을 도울 수있는 희망과 그것을 클래스를 재생, 모르거나 적어도 그것을 시도

class DownloadFileFromURL extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Bar Dialog 
     */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(progress_bar_type); 
     } 

     /** 
     * Downloading file in background thread 
     */ 
     @Override 
     protected String doInBackground(String... f_url) { 
      int count; 
      try { 
       URL url = new URL(f_url[0]); 
       URLConnection conection = url.openConnection(); 
       conection.connect(); 

       // this will be useful so that you can show a tipical 0-100% 
       // progress bar 
       int lenghtOfFile = conection.getContentLength(); 

       // download the file 
       InputStream input = new BufferedInputStream(url.openStream(), 
         8192); 

       // Output stream 
       OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3"); 

       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        // publishing the progress.... 
        // After this onProgressUpdate will be called 
        publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 

        // writing data to file 
        output.write(data, 0, count); 
       } 

       // flushing output 
       output.flush(); 

       // closing streams 
       output.close(); 
       input.close(); 

      } catch (Exception e) { 
       Log.e("Error: ", e.getMessage()); 
      } 
      return null; 
     } 
     /** 
     * Updating progress bar 
     */ 
     protected void onProgressUpdate(String... progress) { 
      // setting progress percentage 
      pDialog.setProgress(Integer.parseInt(progress[0])); 
     } 
     /** 
     * After completing background task Dismiss the progress dialog 
     **/ 
     @Override 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after the file was downloaded 
      dismissDialog(progress_bar_type); 
      Uri u = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3"); 

      MediaPlayer mediaPlayer = new MediaPlayer(); 
      try { 
       mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
       mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/workout.mp3"); 
       mediaPlayer.prepare(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
       @Override 
       public void onPrepared(MediaPlayer mp) { 
        mp.start(); 
       } 
      }); 
     } 
    } 

실제로이 오류가 로그에 실제로 표시됩니다.

05-31 11:46:11.605 4638-4650/com.example.project.calisthenic E/MediaPlayer: error (1, -2147483648) 

문제는 다운로드 한 파일을 재생하려고합니다. 안드로이드 네이티브 플레이어로 재생하려고해도 "플레이어가이 유형의 오디오 파일을 지원하지 않습니다."라고되어 있으므로, 잘못된 방식으로 파일을 다운로드하는 중. 잘못된 방법으로 파일을 재생하려고합니다.

내가 낫겠과 파일 크기가 10킬로바이트하고 다운로드 한 후에는 문제가 그것에서 오는 경우

+0

무슨 문제가 있습니까? pl에 오류가 발생하면 로그를 게시하십시오. – RAAAAM

답변

0

는 나도 몰라 다운로드에 문제가 될 필요가 있으므로 확실히 400B입니다하지만 당신은 준비를 설정해야합니다 준비 함수를 호출하기 전에 리스너.

+0

이미 시도해보십시오. 문제는 다운로드에서 오는 것입니다. 왜냐하면 10kb 파일이 400b에서 다운로드하기 때문에 가능하지 않습니다. 따라서 손상된 다운로드라고 생각됩니다. –

관련 문제