0

The Exoplayer is throwing an exception "UnrecognizedInputFormatException". The files are .mp3 files and I am able to play the file on browser or by using MediaPlayer library on android. Here is the code that I am using to serve the file:

     public NanoHTTPD.Response serveFile(String mime, Map<String, String> header, File file) {

    NanoHTTPD.Response res;
    String etag = Integer.toHexString((file.getAbsolutePath() +
            file.lastModified() + "" + file.length()).hashCode());
    long fileLen = file.length();

    try {
        res = newFixedLengthResponse(NanoHTTPD.Response.Status.OK, mime, new FileInputStream(file), fileLen);
        res.addHeader("Content-Length", "" + fileLen);
        res.addHeader("ETag", etag);
        return res;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }

}

And Here is how I am generating my MediaItem object, the url is like "http://192.168.23.12:8080/song/abc.mp3"

 private static MediaItem getMediaItem(String songName) {
    String url = String.format("http://%s:%s%s%s", hostIP, hostPort, Values.SONGS_PATH, songName);
    Log.d("MusicPlayerControl", "url : " + url);
    Uri uri = Uri.parse(url);
    return MediaItem.fromUri(uri);
}

static void playNewSong(SongListItem songName) {
    MediaItem mediaItem = getMediaItem(songName.getSongName());
    Log.d("MusicPlayerControl", songName.getSongName());
    mediaPlayer.setMediaItem(mediaItem);
    mediaPlayer.prepare();
    mediaPlayer.play();
}

1 Answers1

0

There was no issue with the code here, I forgot to initialize the mediaPlayer object