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;
}
Related
I want to play an mp3 file in my res/raw folder.
But i get error as "error (1, -2147483648)" and IOException on mp.prepare()
My code
try {
MediaPlayer mPlayer = MediaPlayer.create(NavigationHome.this, R.raw.notfy);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
I also tried with
try {
mp.setDataSource(NavigationHome.this, Uri.parse("android.resource://com.hipay_uae/res/raw/notfy"));
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
Another solution that I tried
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
These too didn't work for me.
It will help more if you can post the StackTrace in your question.
But, as per the information in your question, the below code should work for playing the media file from the raw resource folder.
If you use the create() method, prepare() gets called internally and you don't need to explicitly call it.
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.notify);
mediaPlayer.start();
But, the point to consider is that prepare() generally throws an IllegalStateException, and in your case, you are getting an IOException. So it would be worth checking if the file is in fact present in raw folder and/or the file is corrupt.
Try to initialize your media player before preparing it or setting data source to it
Play From external directory
String filePath = Environment.getExternalStorageDirectory()+"/folderName/yourfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
From raw folder
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.song);
mediaPlayer.start();
Try this
String fname="your_filename";
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
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?
I need to play a live stream on devices with 2.x and greater versions. This states that it's impossible to play live streams on devices with Android 2.x.
What're my options here ? Especially I'm interested in streaming audio - what format should i pick and in conjunction with which protocol ?
P.S. I've tried Vitamio - don't want to make customers download third party libraries.
UPD
How come I can play this stream "http://188.138.112.71:9018/" ?
try this example for RTSP streaming (the url should support RTSP) for video change the code to support just audio
public class MultimediaActivity extends Activity {
private static final String RTSP = "rtsp://url here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multimedia);
//***VideoView to video element inside Multimedia.xml file
VideoView videoView = (VideoView) findViewById(R.id.video);
Log.v("Video", "***Video to Play:: " + RTSP);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
Uri video = Uri.parse(RTSP);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
}
}
EDIT:
Live Audio streaming using MediaPlayer in Android
Live Audio streaming in android, from 1.6 sdk onwards is become so easy. In setDataSource() API directly pass the url and audio will play without any issues.
The complete code snippet is,
public class AudioStream extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
}
}
You can use RTSP protocol which is supported by Android native media player.
player = new MediaPlayer();
player.reset();
player.setDataSource(intent.getStringExtra("Path"));
player.prepare();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
Where path would be your rtsp audio streaming url.