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.
Related
I got a path of media file and want to play it.The problem is MediaPlayer.create() work successfully but when I use setDataSource,it's not working correctly, the media not play the music
player = new MediaPlayer();
String path =
"/storage/emulated/0/Music/AThousandYears_ChristinaPerri.mp3";
try {
player.setDataSource(getApplicationContext(),
Uri.parse(path));
player.start();
} catch (IOException e) {
e.printStackTrace();
}
I did not got any exception. Sorry about my bad english.I hope someone help.Thanks in advance
Try this
try{
mediaPlayer.reset();
mediaPlayer.setDataSource(songPath);
mediaPlayer.prepare();
mediaPlayer.start();
}catch(Exception e){
//Handle Exception
}
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 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.
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?