I have a server side webapp which provides file upload and download functionality and an Android app which uploads music files onto the web site and then requests uploaded files by some URL.
My URL is structured like: http://mywebsite:8080/api/v1/files/fileById?fileId=<file_id>
When I'm trying to get a file from a web browser, it works fine; the browser downloads file and the OS can play it. But when I'm trying to put URL described before into MediaPlayer as a datasource, I'm getting java.io.IOException: setDataSource failed.
The code of MediaPlayer usage:
private void startPlaying(Uri fileUri) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(this, fileUri);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(preparedPlayer -> preparedPlayer.start());
}
catch (IOException e) {
LogUtil.loge(LOG_TAG, e);
stopPlaying();
return;
}
mediaPlayer.setOnCompletionListener(mp -> stopPlaying());
}
private void stopPlaying() {
if (mediaPlayer == null) { return; }
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
UPD: maybe I have to implement direct links to the files on a server side. all the samples for playing music from URL are requesting direct URL to the file.
Any suggestions?
If you want to play music from a URL, you have to input a String parameter.
Change to:
private void startPlaying(String url) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(preparedPlayer -> preparedPlayer.start());
}
catch (IOException e) {
LogUtil.loge(LOG_TAG, e);
stopPlaying();
return;
}
mediaPlayer.setOnCompletionListener(mp -> stopPlaying());
}
I think I can close this question because I've checked my code correctness by using URL for an external service which works fine. I found that this problem is a problem in my server side app. Android app code is correct.
Related
Iam trying to built an android application for playing online radio. The code is working in emulator properly. but when installed in phone it does not works.
MediaPlayer mediaPlayer = new MediaPlayer();
String url = "http://5293.live.streamtheworld.com:3690/JACK2_LOWAAC_SC";
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
Is there any problem in the link format? I tried to play same link in html5 its working fine on desktop but the same website when opened in phone the link is not working. Also are there any issues or any components in html5 which do not work in smart phones bu work on Desktop ?
I need to play the live stream not the static file like mp3. You can take some URL form www.listenlive.eu/uk.html and try to play.The URL in my code is form this site only. download the VLC file and open it with any text editor and you will get url.
use mediaPlayer.prepareAsync(); instead of mediaPlayer.prepare(); since your are streaming from web and also add permission for internet
<uses-permission android:name="android.permission.INTERNET"/>
Also
String url = "http://5293.live.streamtheworld.com:3690/JACK2_LOWAAC_SC";
doesn't work for me instead i use
final String url = "http://vprmix.streamguys.net/vprmix64.mp3";
private boolean isPLAYING = false;
public void streamAudio(String url) {
if (!isPLAYING) {
isPLAYING = true;
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
Log.e("mediaPlayer", "prepare() failed");
}
} else {
isPLAYING = false;
stopPlaying();
}
}
private void stopPlaying() {
mediaPlayer.release();
mediaPlayer = null;
}
I want to try to play a direct link from the Dropbox app but do not make success.
Uri myUri = Uri
.parse("https://dl-web.dropbox.com/get/LieuTraiTieuThuy2006Tap5.mp3?_subject_uid=442310434&w=AAB7e-MLX5QBBdqbf0EXNmTv5NkX-hRJBWtjv5Vv9dryRw");
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, myUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
But this link is successful.
http://searchgurbani.com/audio/sggs/1.mp3
I found this link in this topic.
enter link description here
I just wanted to use DropBox to store my music later. So you can explain why my DropBox Link does not work in this application.
I'm done my Android app, just need to add some looping background music. Here is my method for playing the song
public void playAudio(){
path = "android.resource://" + getPackageName() + "/" + R.raw.music1;
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path);
mp.prepare();
mp.setLooping(true);
mp.setVolume(100, 100);
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.d("NOTE WORKEING","NOT WORKING");
}
}
It's not working....It's going to catch everytime, and I don't know why. Please help me. Music1 is an mp3 file.
Thank you
Make sure your music1 file is in a Android playable Android format.
Then just use this code:
MediaPlayer mediaPlayer = MediaPlayer.create(YourActivity.this, R.raw.music1);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
mediaPlayer.start();
}
catch (Exception e) {
e.printStackTrace();
Log.d("NOT WORKEING","NOT WORKING");
}
You don't even need to call prepare() method.
I tested it with an mp3 file and it works perfectly.
If #Squonk's answer does not work, then the problem has to do with trying to start the music file before the MediaPlayer has prepared it. Try changing the MediaPlayer's start code to the following:
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
start();
}
});
Let me know if this helps, I have a good amount of experience with the MediaPlayer class and can help you troubleshoot.
Using mp.setDataSource(path) requires a valid filesystem path or URL.
In your case, path is a string representation of a Uri in which case you need to use a different approach.
Try setDataSource(Context context, Uri uri). You'll obviously need to provide a valid Context and parse your path variable into a Uri. Example...
mp.setDataSource(getApplicationContext(), Uri.parse(path));
Also change the path to...
path = "android.resource://" + getPackageName() + "/raw/music1";
I use below code to play streaming music:
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace();
}
I want to buffering file to local cache file at the same time.
And next time want to play this url music can play the local file directly.
How can I do it?
Or anyone can provide references?
Does anybody know what does the second argument mean from this error (1, -2147483648) in MediaPlayer? I constantly receive it when attempting to play an audio from a url stream. I try to play it from a class that extends BaseExpandableListAdapter if that matter. I have already reviewed this post Android MediaPlayer error: MediaPlayer error(1, -2147483648) on Stream from internet however all the answers refers to the stream support issues. It's not a stream support issue in my case since I am able to play an audio from the same stream but just using a different class. This is a method that I'm using for playing:
private void startPlaying(String fileName) {
mediaPlayer = new MediaPlayer();
try {
if (fileInputStream != null) { // Read a file from a fileInputStream from a filesystem (EXTERNAL OR INTERNAL storage)
mediaPlayer.setDataSource(fileInputStream.getFD());
Log.d("MediaPlayer is playing", "from device");
} else {
// mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getFilePath()); // Read a file from a url
Log.d("MediaPlayer is playing", "from stream");
}
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new CompletionListener());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
The error is usually one of these in this case:
File path is in error. Incorrect directory or Url or Uri found.
Media file is in error, incompatible format.
Missing permissions
Here's a good blog that outlines these situations and how to fix them:
http://www.weston-fl.com/blog/?p=2988
Also see this thread:
Android mediaplayer MediaPlayer(658): error (1, -2147483648)