1

インテント (デフォルトのメディア プレーヤー) を介してリモート ビデオ ファイルを再生しようとしていますが、ストリーミング中にローカルに保存したいと考えています。次のコードを使用して、正常に動作しているビデオ ファイルにスチームをローカルに保存しようとしています。ファイルがストリーミングされるとすぐにインテントを開始したいのですが、インテントが開始されるとエラー " Sorry this video can not be played" が発生します。ストリーミングが完了したときにインテントコードをこのメソッドの最後に移動すると( when 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;

    }
4

1 に答える 1

0

問題は次の行にあると思います。

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

ローカル ファイルを Intent に渡しますが、それはまだダウンロードされていません。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 をストリーミングし、コンテンツをローカル ファイルに保存します。私はコードを誤解しているかもしれませんが、それを調べることから始めます

于 2012-12-04T10:49:36.350 に答える