We are recording some audios on Android phones which are having the following format as per vlc information:
Codec: MPEG AAC Audio (mp4a)
Language: English
Type: Audio
Channels: Stereo
Sample rate: 32000 Hz
Bits per sample: 32
The above files were not playing on the safari browser with mime type audio/mpeg
, But as soon as we changed the mime to audio/mp4
it started playing on safari browser.
For Android we are using API to stream this file using api resource as follows :
@GET
@UnitOfWork
@Produces("audio/mpeg")
@Path("/getaudiofile/{fileId}")
public Response getPart(@Auth AuthUser authUser, @PathParam("fileId") Long fileId) {
File audioFile = new File(filesTableDAO.getFilePathById(fileId));
if(audioFile.exists()) {
return Response.ok().entity(audioFile).build();
} else {
// ... Return 404 here
}
}
But with above API some files getting played & some are not, Similar to Safari case earlier. But safari problem went away ASAP we changed mime to "audio/mp4"
or "video/mp4"
both the mime type work.
But for the API /getaudiofile/{fileId}
none of the following mime types worked with the javax.ws.rs @Produces
annotaion :
audio/mp4
video/mp4
audio/mpeg
audio/m4a
audio/mpeg-4
audio/mp4a
But with audio/mpeg
some of the files play but some don't.
What may be the right mime type or can file set the codec or mime info itself while returning from API ?
Or Is there any way to make Android Media Player MIME type aware? Like html tag.
We have streamed file content of mime "audio/mpeg" the media player plays smaller sized files easily. But bigger streamed content fails to play e.g. 10 - 20 MB.
Problem with stream URL the player do not get any mime extension e.g. mp3, or mp4. Hence we want the media player to know in advance what type of the content is being streamed.
Does the MediaPlayer API support setting mime type to player instance prior to playing?
Update
1] This is happening for files with big sizes small files play very well, An recording of few seconds ( 1-50 secs ) play without a problem, An recording file with playback length of more than minute fails to play.
2] When played from authenticated URL playback fails if the same file is on local file system plays flawlessly.
https://github.com/aruld/jersey-streaming
Above project by Arul Dhesiaseelan solved my issue, The way I wrote get file Resource was not efficient, In above GitHub project take look into MediaResource.java.
This is the proper way of streaming media files, Adjust mime-types as per your file.
I set headers to media player as follows:
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic <BASE64_ENCODED_AUTH_TOKEN>");
headers.put("Accept-Ranges", "bytes");
headers.put("Status", "206");
headers.put("Cache-control", "no-cache");
Uri uri = Uri.parse(url);
mediaPlayer.setDataSource(getContext(), uri, headers);
Now I am able to stream media with authentication.