使用NanoHttpd进行视频流传输,错误:java.net.SocketException:sendto failed:EPIPE(Broken pipe)

前端之家收集整理的这篇文章主要介绍了使用NanoHttpd进行视频流传输,错误:java.net.SocketException:sendto failed:EPIPE(Broken pipe)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要流式传输视频并在网页中持续播放.我正在使用Nanohttpd库在我的Android应用程序中运行服务器.我可以在页面加载时流式传输和播放视频.视频完成播放后,我无法再播放视频.每次我需要刷新页面才能播放视频.尝试第二次玩时出现以下错误.

java.net.SocketException: sendto Failed: EPIPE (Broken pipe)
    at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
    at libcore.io.IoBridge.sendto(IoBridge.java:515)
    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1386)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1359)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1349)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.send(NanoHTTPD.java:1335)
    at server.http.android.androidhttpserver.server.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:769)
    at server.http.android.androidhttpserver.server.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:186)
    at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: sendto Failed: EPIPE (Broken pipe)
    at libcore.io.Posix.sendtoBytes(Native Method)
    at libcore.io.Posix.sendto(Posix.java:176)
    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
    at libcore.io.IoBridge.sendto(IoBridge.java:513)
    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1386)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1359)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1349)
    at server.http.android.androidhttpserver.server.NanoHTTPD$Response.send(NanoHTTPD.java:1335)
    at server.http.android.androidhttpserver.server.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:769)
    at server.http.android.androidhttpserver.server.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:186)
    at java.lang.Thread.run(Thread.java:818)

我发送的回复如下.

@Override
public Response serve(IHTTPSession session) {
   FileInputStream fis = null;
    File file = new File("/storage/emulated/0/DCIM/Camera/VIDEO.mp4");
    try{
        if(file.exists())
        {
            fis = new FileInputStream(file);
        }
        else
         Log.d("FOF :","File Not exists:");
    }catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    return new NanoHTTPD.Response(Response.Status.OK,"video/mp4",fis,file.length() );
}

用于访问视频的HTML文件

请为我提供一个连续播放视频的解决方案,而无需刷新网页.

最佳答案
我成功地使用以下代码

@Override
public Response serve(IHTTPSession session) {
    MapFailed");
    }

    return (res == null) ? getResponse("Error 404: File not found") : res;
}

// Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status,String mimeType,InputStream message) {
    Response res = new Response(status,mimeType,message);
    res.addHeader("Accept-Ranges","bytes");
    return res;
}

// Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status,String message) {
    Response res = new Response(status,"bytes");
    return res;
}

private Response getResponse(String message) {
    return createResponse(Response.Status.OK,"text/plain",message);
}

猜你在找的Android相关文章