【问题标题】:Serve 2 files simultaneously with Nanohttpd from Android device使用 Android 设备上的 Nanohttpd 同时提供 2 个文件
【发布时间】:2015-08-18 14:04:56
【问题描述】:

我想将本地 mp3 文件从我的设备发送到 Chromecast。我已经有一个版本的 Nanohttpd 正在运行,它运行良好,我可以在电视上播放我的歌曲而不会出现问题:

MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);

MediaInfo mediaInfo = new MediaInfo.Builder(
 "http://192.168.0.XX:8080")
.setContentType("audio/mp3")
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setMetadata(mediaMetadata)
.build()

mRemoteMediaPlayer.load(mApiClient, mediaInfo, true) .....

...“http://192.168.0.XX:8080”是我的服务器网址。

现在,我想为我的 mediaMetadata 添加封面,但是,为此,我还需要提供图片文件,因为这张图片是作为 WebImage 发送到 Cromecast 的,如下所示:

mediaMetadata.addImage(new WebImage(Uri.parse("My Url in Nanohttpd ")));

可以直接从资源创建 WebImage 吗?

如果没有,有什么方法可以同时服务(歌曲和图片)?也许我可以在 http://192.168.0.XX:8080/song 提供歌曲,在 http://192.168.0.XX:8080/image 提供图片或类似的东西,但我不知道如何。 ..

这是我目前的 Nanohttpd 服务方法:

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

        String mediasend = "audio/mp3";
        FileInputStream fis = null;
        File song = new File(songLocalPath);
        Log.e("Creando imputStream", "Size: ");
            try {
                fis = new FileInputStream(song);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        Response.Status st = Response.Status.OK;
        return new NanoHTTPD.Response(st, mediasend, fis,song.length());
    } 

每一种方法都会受到欢迎。

【问题讨论】:

    标签: android server chromecast nanohttpd webimage


    【解决方案1】:

    好吧,最后我将 serve 方法更改为有 2 个 URL 并区分它们:

    @Override
    public Response serve(String uri, Method method,
                          Map<String, String> header,
                          Map<String, String> parameters,
                          Map<String, String> files) {
    
    
     if (uri.contains("picture")){          
     //serve the picture 
          return new NanoHTTPD.Response(st, mediasend, fisPicture, f.length());
    
     }else if (uri.contains("song")){
    
     //serve the song
          return new NanoHTTPD.Response(st, mediasend, fisSong, f.length());
     }
    

    然后在 Sender App 中发送歌曲:

     MediaMetadata mediaMetadata = new       MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
    
     MediaInfo mediaInfo = new MediaInfo.Builder(
     "http://192.168.0.XX:8080/song")
     .setContentType("audio/mp3")
     .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
     .setMetadata(mediaMetadata)
     .build()
    
     mRemoteMediaPlayer.load(mApiClient, mediaInfo, true)
    

    对于专辑封面:

    mediaMetadata.addImage(new WebImage(Uri.parse(http://192.168.0.XX:8080/picture));
    

    【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2020-07-08
    相关资源
    最近更新 更多