簡體   English   中英

如何使用Android的VideoView從本地存儲流式傳輸和下載媒體?

[英]How to stream and download a media from local storage using Android's VideoView?

我必須從服務器流式傳輸和下載視頻文件,並在VideoView中顯示它,感謝谷歌,VV自然不支持我的要求,所以這就是我所做的:

1-使用NanoHTTPD創建HTTP代理並開始從服務器下載文件

2啟動VideoView並將uri設置為我的HTTP代理

我的代理服務器內部有3個內部服務器(),我開始對VideoView進行響應,就像使用具有適當響應頭的真實服務器一樣(我的意思是content-length / content-range帶有206響應)

但是問題開始於VideoView,它可以正常讀取300kb的視頻文件,然后再從619kb或其他內容進行請求。 我沒有任何線索,為什么VV要從300,000bytes跳到突然跳到619,000bytes,經過10次嘗試,它會在VideoView的c ++代碼內以錯誤-1004響應我,我找不到任何線索是什么那。

以下是我的完整解決方案。 感謝任何幫助,因為我對VideoView一無所知,這是一種奇怪的行為!!!

注意:我故意修復了文件的大小,沒有任何問題。

package com.example.videoBuffer2;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.*;
import java.util.Map;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    Button btn;
    VideoView videoView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button);
        videoView = (VideoView) findViewById(R.id.videoView);
        MediaController mediaController = new
                MediaController(this);
        videoView.setMediaController(mediaController);
        final VideoStreamServer server = new VideoStreamServer(9090);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    server.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                videoView.setVideoURI(Uri.parse("http://127.0.0.1:9090/1.mp4"));
                videoView.requestFocus();

                videoView.start();
            }
        });
    }


    public class VideoStreamServer extends NanoHTTPD
    {

        public VideoStreamServer(int port) {
            super(port);
        }

        @Override
        public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {

            //range=bytes=619814-
            long range;

            if (headers.containsKey("range"))
            {
                String contentRange = headers.get("range");
                range = Integer.parseInt(contentRange.substring(contentRange.indexOf("=") + 1, contentRange.indexOf("-")));
            }
            else
                range = 0;


            byte[] buffer;
            int constantLength = 256000;
            long bufLength=0;
            boolean isLastPart=false;
            try {

                RandomAccessFile ff =new RandomAccessFile(new File("/mnt/sdcard","1.mp4"),"rw" );
                long remainingChunk = ff.length() - range; //remaining
                if (remainingChunk < constantLength){
                    bufLength= remainingChunk; //means last part
                    isLastPart = true;
                }

                else
                    bufLength = constantLength;
                if (range !=0)
                    ff.seek(range);
                buffer= new byte[(int)bufLength];


                ff.read(buffer);


            } catch (FileNotFoundException e) {
                e.printStackTrace();
                buffer = new byte[0];
            } catch (IOException e) {
                e.printStackTrace();
                buffer = new byte[0];
            }
            Response response;
//            if (isLastPart)
//                response = new Response(Response.Status.OK,"video/mp4",new ByteArrayInputStream(buffer));
//            else
                response = new Response(Response.Status.PARTIAL_CONTENT,"video/mp4",new ByteArrayInputStream(buffer));

            response.addHeader("Content-Length","891064");
            response.addHeader("Content-Range",String.format("bytes %s-%s/%s", range,(range+bufLength),"891064"));
            Log.e("SERVER","Inside server sent " + String.format("bytes %s-%s/%s", range,(range+bufLength),"891064"));
            return response;

        }
    }
}

最終,我發現了問題所在,並不是代碼,但根據Google的文檔,我使用的媒體不是標准的。

您可以在此處了解更多信息: http : //www.vahidhashemi.com/?p=120

暫無
暫無

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

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