Android Mediaplayer With Api Url
Solution 1:
I've just tried the following code in Android studio targeting Android SDK 28 and it works fine. Make sure your API is actually returning the mp3 file.
try {
MediaPlayer player = new MediaPlayer();
player.setDataSource("https://yourdomain/yourmusic.mp3");
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
Solution 2:
Try testing your code using a url to a known public mp3 file to verify that the MediaPlayer code is correct. Once that is working, you can narrow the issue down to either the token not being set correctly or your server not returning the mp3 file correctly. Maybe the response doesn't indicate it is an mp3 file correctly and thus the media player doesn't know how to handle it.
Solution 3:
I finally found the reason behind this issue. My backend laravel api call was a POST request. Changing the same to Get request worked for me. Also changing request from POST to GET was not an issue as my request parameters were sent through the header. (This was done because android mediaplayer's setDataSource method only accepts headers and not body parameters).
In short android mediaplayer is expecting us to use GET request only.
Post a Comment for "Android Mediaplayer With Api Url"