1

では再生できません。
マイダウンロードしたビデオファイルは任意のプレーヤーから再生することができない、一人で私のアプリのVideoViewを聞かせて、それがVideoViewにURLからうまく演じています。
それは外部記憶装置ではない場合、私はそうVideoViewにURLから直接それを再生、a video fileをダウンロードしています:DONE何ダウンロードしたビデオファイルは、問題のAndroid


コードのVideoView部分は次のようである:

@Override 
protected String doInBackground(Void... params) { 

    URLConnection conn; 
    try { 
     URL httpFileUrl = new URL(fileUrl); 
     conn = httpFileUrl.openConnection(); 
     conn.connect(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
    Log.d(TAG, "Connection opened"); 
    InputStream inputStream; 
    try { 
     inputStream = new BufferedInputStream(conn.getInputStream(), 4096); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 

    BufferedReader bufferedReader = new BufferedReader(
           new InputStreamReader(inputStream)); 

    int responseLen = 0; 
    StringBuilder stringBuilder = new StringBuilder(); 
    String responseStr; 
    try { 
     while ((responseStr = bufferedReader.readLine()) != null) { 
      // Log.i(TAG, "Response read: " + responseStr); 
      stringBuilder.append(responseStr.trim()); 
      // ...my progress-bar related codes 
     } 
     inputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 

    responseStr = stringBuilder.toString(); 
    return responseStr; 
} 

final VideoView vvPlayer = (VideoView) findViewById(R.id.vvPlayer); 
MediaController mc = new MediaController(MainActivity.this); 
mc.setAnchorView(vvPlayer); 
vvPlayer.setMediaController(mc); 

if (videoFile.exists()) { 
    // vvPlayer.setVideoURI(Uri.fromFile(videoFile)); <-- Also Tried :(
    vvPlayer.setVideoPath(videoFile.getAbsolutePath()); 
    Toast.makeText(this, "Playing from local ...", Toast.LENGTH_SHORT).show(); 
} else { 
    vvPlayer.setVideoPath(VIDEO_PATH); 
    Toast.makeText(this, "Playing online & caching ...", Toast.LENGTH_SHORT).show(); 
    downloadVideoFile(); 
} 

コア部分、すなわち、downloadVideoFile()方法のAsyncTaskのthe doInBackground()は、以下のコードを使用して文字列としてファイルのコンテンツを返します

ファイル・コンテンツを取得した後、私は自明以下のコードを使用してファイルのものを保存した:

 try { 
      if (!APP_DIRECTORY.exists()) 
       APP_DIRECTORY.mkdirs(); 
      if (videoFile.createNewFile()) 
       Log.d(TAG, "Vide-file newly created"); 

      FileOutputStream fos = new FileOutputStream(videoFile); 
      fos.write(fileContent.getBytes()); 
      fos.flush(); 
      fos.close(); 
     } catch (IOException e) { 
      Log.d(TAG, "Exception for new creation of videoFile"); 
      e.printStackTrace(); 
     } 

最終的な結果は8.81 MBのファイルで、ビデオプレーヤーでビデオファイルとして開くことはできません。

私はコーデックエンコーディング、あるいは簡単なファイル省一部のようなものが欠けていることができますか?

+1

いいえ、StringBuilderはバイナリビデオデータを処理できません。テキストデータに合わせて調整されたreadLine()は使用しないでください。 FileOutputStreamへのデータの書き込みは、ネットワークから取得した直後に直接行います。メモリに集約しないでください。ビデオには、RAMに収まる量よりも多くのバイトが含まれている可能性があります。 –

答えて

0
Replace your doInBackground with this: 

@Override 
protected String doInBackground(Void... params) { 

    URLConnection conn; 
    try { 
     URL httpFileUrl = new URL(fileUrl); 
     conn = httpFileUrl.openConnection(); 
     conn.connect(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
    Log.d(TAG, "Connection opened"); 
    InputStream inputStream; 
    try { 
     inputStream = new BufferedInputStream(httpFileUrl.openStream(), 8192); 
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.mp4"); 

byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
output.write(data, 0, count); 
} 
output.flush(); 
output.close(); 
      inputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 


    return null; 
} 


And in onPostExecute, 

@Override 
    protected void onPostExecute(String file_url) { 

     String videoPath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.mp4"; 
     // Now pass this path for playing video. 
    } 
+0

メソッドの最初のセクションで初期化された 'conn'オブジェクトの使用はありませんか? ...あなたが 'httpFileUrl.openStream()'を直接使って 'inputStream'を初期化したのを見てみましょう... – Touhid

+1

connオブジェクトは、ファイルの属性(例:コンテンツの長さ)を取得し、それを検証するために使用できますダウンロードしたコンテンツをダウンロードしたり、ファイルの進行状況を表示したりします。int videofilelength = conn.getContentLength(); –

関連する問題