How to serve a mp3 file using latest NanoHTTPD 2.3.0 in Android?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 07:54:15

问题


I have read How to serve a file on sdcard using NanoHTTPD (inside Android)

The code return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis) doesn't be supported in latest NanoHTTPD 2.3.0 again.

I try to replace it with return newFixedLengthResponse(fis) , but it's incorrect. How can I do that? Thanks!

public class StackOverflowMp3Server extends NanoHTTPD {

    public StackOverflowMp3Server() {
         super(8089);
    }

    @Override
    public Response serve(String uri, Method method,
        Map<String, String> header, Map<String, String> parameters,
        Map<String, String> files) {
    String answer = "";

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(Environment.getExternalStorageDirectory()
                + "/music/musicfile.mp3");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis);
  }
}

回答1:


You can use newChunkedResponse() method for InputStream like this:

return newChunkedResponse(Status.OK, "audio/mpeg", fis);




回答2:


This code work fine (put your sound in the assets folder)

class SoundServer extends NanoHTTPD {

    SoundServer() {
        super(8089);
    }

    @Override
    public Response serve(IHTTPSession session) {
        InputStream myInput = null;
        try {
            myInput = mContext.getAssets().open("sound.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return createResponse(Response.Status.OK, "audio/mpeg", myInput);
    }

    //Announce that the file server accepts partial content requests
    private Response createResponse(Response.Status status, String mimeType,
                                    InputStream message) {
        return newChunkedResponse(status, mimeType, message);
    }
}



回答3:


This code works to serve any type files from nanohttpd webserver.

 @Override
public Response serve(IHTTPSession session) {
    String msg = "<html><body><h1>Hello server</h1></body></html>\n";
    String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
    Method method = session.getMethod();
    String uri = session.getUri();
    Map<String, String> files = new HashMap<>();
    Storage storage = new Storage(OpenRAP.getContext());
    String currentpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/";
    String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/temp/";
    String ecarpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/ecars_files/";
    String xcontent = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/xcontent/";
    MainActivity.checkfiles();
    updateurl();
    String Endpoint = session.getUri();
    if (Endpoint.equals("/")) {

        String answer = "";
        try {
            // Open file from SD Card
            File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
            FileReader index = new FileReader(root +
                    "/www/openrap/index.html");
            BufferedReader reader = new BufferedReader(index);
            String line = "";
            while ((line = reader.readLine()) != null) {
                answer += line;
            }
            reader.close();
        } catch (IOException ioe) {
            Log.w("Httpd", ioe.toString());
        }

        return newFixedLengthResponse(answer);
    }
    else if (uri.equals("/app-debug.apk")) {

        File root = Environment.getExternalStorageDirectory();
        FileInputStream fis = null;
        File file = new File(root.getAbsolutePath() + "/www/resources/app-debug.apk");
        String mime = NanoHTTPD.getMimeTypeForFile("app-debug.apk");
        Log.d("Path", root.getAbsolutePath());
        try {
            if (file.exists()) {
                fis = new FileInputStream(file);

            } else
                Log.d("FOF :", "File Not exists:");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        return newFixedLengthResponse(Response.Status.OK, mime, fis, file.length());
    }

Here i am sending apk file you can anytype of file with respective format and file name



来源:https://stackoverflow.com/questions/36562616/how-to-serve-a-mp3-file-using-latest-nanohttpd-2-3-0-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!