【问题标题】:How to serve a mp3 file using latest NanoHTTPD 2.3.0 in Android?如何在 Android 中使用最新的 NanoHTTPD 2.3.0 提供 mp3 文件?
【发布时间】:2016-04-12 02:30:59
【问题描述】:

我已阅读How to serve a file on sdcard using NanoHTTPD (inside Android)

最新的 NanoHTTPD 2.3.0 不再支持代码return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis)

我尝试用 return newFixedLengthResponse(fis) 替换它,但它不正确。我怎样才能做到这一点?谢谢!

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);
  }
}

【问题讨论】:

标签: android nanohttpd


【解决方案1】:

您可以像这样对InputStream 使用newChunkedResponse() 方法:

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

【讨论】:

  • 用更多细节更新了答案。
  • 经过 2 小时与数百行代码的斗争,这一个确实有效! :D 该死的,为什么我没有早点找到它。对任何人 - 这适用于大文件!
【解决方案2】:

这段代码可以正常工作(把你的声音放在资产文件夹中)

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】:

    此代码适用于从 nanohttpd 网络服务器提供任何类型的文件。

     @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());
        }
    

    我在这里发送 apk 文件,您可以使用具有相应格式和文件名的任何类型的文件

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      相关资源
      最近更新 更多