Autore Topic: NanoHTTPD  (Letto 421 volte)

Offline x69asterix2

  • Utente junior
  • **
  • Post: 127
  • Respect: +1
    • Mostra profilo
NanoHTTPD
« il: 21 Gennaio 2016, 12:43:38 CET »
0
ciao,
ho il seguente problema se eseguo un file mp3 tutto funziona correttamente ma se voglio riprodurre un file Video mp4 tramite http non viene riprodotto nulla perchè?
codice:
Codice: [Seleziona]
public class VideoServer extends NanoHTTPD {
   
    public static final int DEFAULT_SERVER_PORT = 8080;
    public static final String TAG = VideoServer.class.getSimpleName();
   
    private static final String REQUEST_ROOT = "/";       
   
    private String mVideoFilePath;
    private int mVideoWidth  = 0;
    private int mVideoHeight = 0;

    public VideoServer(String filepath,int width,int height,int port) {
        super(DEFAULT_SERVER_PORT);
        mVideoFilePath = filepath;
        mVideoWidth  = width;
        mVideoHeight = height;
    }
   
    @Override
    public Response serve(IHTTPSession session) {       
        Log.d(TAG,"OnRequest: "+session.getUri());
        if(REQUEST_ROOT.equals(session.getUri())) {
            return responseRootPage(session);
        }
        else if(mVideoFilePath.equals(session.getUri())) {
            return responseVideoStream(session);
        }
        return response404(session,session.getUri());
    }

    public Response responseRootPage(IHTTPSession session) {
        File file = new File(mVideoFilePath);
        if(!file.exists()) {
            return response404(session,mVideoFilePath);
        }
        StringBuilder builder = new StringBuilder();
        builder.append("<!DOCTYPE html><html><body>");
        builder.append("<video ");
        builder.append("width="+getQuotaStr(String.valueOf(mVideoWidth))+" ");
        builder.append("height="+getQuotaStr(String.valueOf(mVideoHeight))+" ");
        builder.append("controls>");
        builder.append("<source src="+getQuotaStr(mVideoFilePath)+" ");
        builder.append("type="+getQuotaStr("video/mp4")+">");
        builder.append("Your browser doestn't support HTML5");
        builder.append("</video>");
        builder.append("</body></html>\n");
        return new Response(builder.toString());
    }
   
    public Response responseVideoStream(IHTTPSession session) {
        try {
            FileInputStream fis = new FileInputStream(mVideoFilePath);
            return new NanoHTTPD.Response(Status.OK, "video/mp4", fis);
           // return new NanoHTTPD.Response(Response.Status.OK, "audio/mpeg", fis);
        }
        catch (FileNotFoundException e) {       
            e.printStackTrace();
            return response404(session,mVideoFilePath);
        }
    }
   
    public Response response404(IHTTPSession session,String url) {
        StringBuilder builder = new StringBuilder();
        builder.append("<!DOCTYPE html><html><body>");       
        builder.append("Sorry, Can't Found "+url + " !");       
        builder.append("</body></html>\n");
        return new Response(builder.toString());
    }
   
   
    protected String getQuotaStr(String text) {
        return "\"" + text + "\"";
    }
   
}

« Ultima modifica: 21 Gennaio 2016, 12:45:56 CET da x69asterix2 »