我必须从服务器流式传输和下载视频文件并在VideoView中显示它,感谢谷歌,VV并不能自然地支持我的要求,所以这就是我所做的:
1 - 使用NanoHTTPD创建HTTP代理并开始从服务器下载文件
2启动VideoView并将uri设置为我的HTTP代理
我的代理服务器中的3-inside server()我开始响应VideoView,与具有相应响应标头的真实服务器相同(我的意思是内容长度/内容范围,206响应)但问题是从VideoView开始,它正确地读取300kb的视频文件,之后它从619kb或其他东西请求。我不知道为什么VV想要从300,000字节跳跃并突然跳到619,000bytes,经过10次尝试后它会在我看不到的VideoView的c ++代码中用ERROR -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;
}
}
}
答案 0 :(得分:-2)
最后我发现我的问题问题不在于代码,但根据谷歌的文档,我使用的媒体并不是标准的。
您可以在此处阅读更多内容:http://www.vahidhashemi.com/?p=120