2012-12-04 4 views
1

인 텐트 (기본 미디어 플레이어)를 통해 원격 비디오 파일을 재생하려고하지만 스트리밍되는 동안 로컬로 저장하려고합니다. 증기가 잘 작동하는 비디오 파일에 저장하려면 다음 코드를 시도하고있다. 이제 파일을 스트리밍하는 즉시 내 의도를 시작하고 싶지만 의도가 시작되면 오류 "Sorry this video can not be played"이 표시됩니다. 스트리밍이 완료되었을 때 (downloadedSize==totalSize 일 때)이 메소드의 끝으로 인 텐트 코드를 옮기면 잘 작동하지만 동시에 스트리밍되는 동안 재생하려고합니다. 어떤 도움이 필요합니까?비디오 파일을 스트리밍 재생할 수 없습니다.

public String DownloadVideo(String Url, String fileName) 
    { 
    String filepath=null; 
    try { 
    URL url = new URL(Url); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.setDoOutput(true); 
    urlConnection.connect(); 

    File SDCardRoot = Environment.getExternalStorageDirectory(); 
    File file = new File(SDCardRoot,fileName); 
    if(file.createNewFile()) 
    { 
    file.createNewFile(); 
    } 
    FileOutputStream fileOutput = new FileOutputStream(file); 
    InputStream inputStream = urlConnection.getInputStream(); 
    int totalSize = urlConnection.getContentLength(); 
    int downloadedSize = 0; 

    byte[] buffer = new byte[1024]; 
    int bufferLength = 0; //used to store a temporary size of the buffer 
    int counter=0; 

    while ((bufferLength = inputStream.read(buffer)) > 0) { 

    fileOutput.write(buffer, 0, bufferLength); 

    if(downloadedSize>2048) 
    { 
     Intent i = new Intent(Intent.ACTION_VIEW); 
      i.setDataAndType(Uri.parse(file.getPath()),"video/*"); 
     startActivity(i); 
    } 
    downloadedSize += bufferLength; 
    counter++; 
    } 
    fileOutput.close(); 
    if(downloadedSize==totalSize) { 
     filepath=file.getPath(); 
           } 
    } catch (MalformedURLException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    filepath=null; 
    e.printStackTrace(); 
    } 
    return filepath; 

    } 

답변

0

나는 문제가이 라인에 생각 :

i.setDataAndType(Uri.parse(file.getPath()),"video/*"); 

귀하의 텐트에 로컬 파일을 전달하지만 아직 다운로드되지 않은 것입니다. 난 당신이 비디오를 스트리밍하는 URL을 사용하고

// Use the url of the remote location 
i.setDataAndType(Uri.parse(Url),"video/*"); 
// Also, try to use lowercase for variables e.g. url vs Url 

그런 식으로, 원격 URL을 스트리밍하여 로컬 파일에 내용을 저장하는 파일의 내용을 저장한다고 생각합니다. 코드를 잘못 해석했을 수도 있습니다. 그러나 먼저 살펴 보겠습니다.

관련 문제