如何在Android中使用最新的NanoHTTPD 2.3.0来提供mp3文件? [英] How to serve a mp3 file using latest NanoHTTPD 2.3.0 in Android?

查看:102
本文介绍了如何在Android中使用最新的NanoHTTPD 2.3.0来提供mp3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读如何为使用NanoHTTPD(在Android内部)在sdcard上保存文件

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

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

我尝试将其替换为return newFixedLengthResponse(fis),但这是不正确的.我怎样才能做到这一点?谢谢!

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

推荐答案

您可以对InputStream使用newChunkedResponse()方法,如下所示:

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

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

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

这篇关于如何在Android中使用最新的NanoHTTPD 2.3.0来提供mp3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆