簡體   English   中英

無法播放視頻文件流

[英]Video file can not be played streaming

我正在嘗試通過intent(默認媒體播放器)播放遠程視頻文件,但是我也想在流式傳輸時將其保存在本地。 我正在嘗試按照以下代碼將蒸汽保存在本地的視頻文件中,效果很好。 現在,我要在流式傳輸文件時立即啟動我的意圖,但是當啟動意圖時,出現錯誤“ 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;

    }

我認為問題出在這一行:

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

您將本地文件傳遞給Intent,但這是尚未下載的文件。 我認為您應該使用網址來流式傳輸視頻,並將內容存儲在文件中

// 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,並將內容保存到本地文件中。 我可能會誤解代碼,但首先要研究一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM