How to play an audio from url in a app widget - android

Im implementing app widget to play audio from url,
I used mediaplayer class to play audio when click on a play button in widget.
but im not getting audio output,
is there any way to work out on this issue.
Please let me know, or give me a reference to follow
Thanks in advance

I hope the following code will help you to play audio from URL.
MediaPlayer mp = new MediaPlayer();
mp.reset();
try {
mp.setDataSource(context, Uri.parse("http:\\your url\test.mp3"));
mp.setLooping(false); // Set looping
mp.prepare();
mp.start();
}
catch(Exception e){
e.printStackTrace();
}

Related

Playing a music file while downloading it

I'm developing a music download App that basically does webscraping across differents torrent platforms and get the torrents. I want to play some music torrent files while I'm downloading them. I have a Service that basically takes care of doing all this. I set a buffer that waits until the torrent file Its at least 25% downloaded, then I use the MediaPlayer class to start playing the file. The music starts playing OK, but It stops after a while, as if It couldn't play more than that 25%. I tried using both the Android's MediaPlayer class and the FFmpegMediaPlayer library. I tried using several configs:
MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = null;
try {
fis = new FileInputStream(mCurrentTrack);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
} catch (Exception e) {
Log.w(APP_TAG, String.format("Something went wrong while trying to play the torrent stream: " + e.toString()));
}
mCurrentTrack is the path to the mp3 file that I downloaded into my Download folder in the external storage.
I tried using AudioManager.STREAM_MUSIC. I tried using the sync prepare() method and then calling the start() method. I triead opening the file with a new File(mCurrentTrack) before start playing It and setting It as readeable and writable. I tried many combinations, but always is the same, the music plays a few seconds and then stops. When I check the external storage, the file ends up being fully downloaded without trouble.
The library that I'm using to download the torrent files is this one TorrentStream-Android
Any tip or idea about how play this file while its being downloaded in the fly, would be appreciated.
As someone pointed out here. Using the ExoMedia player library solved the issue. Here's the snippet that I used to get it working:
compile 'com.devbrackets.android:exomedia:3.1.0'
final EMAudioPlayer mediaPlayer = new EMAudioPlayer (getApplicationContext());
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared() {
mediaPlayer.start();
}
});
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(mCurrentTrack));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();

Android MediaPlayer OnPreparedListener

I am working on a simple app and using a MediaPlayer to play some background noise in 1 activity. I am reading up on MediaPlayer and am not sure whether or not to implement an OnPreparedListener to trigger the start() method. What are the pros / cons to each approach?
Approach 1:
mediaPlayer = MediaPlayer.create(context, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.start();
Approach 2:
mediaPlayer = MediaPlayer.create(context, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
As per the docs, calling start() is effective when you are playing locally available resources for which the MediaPlayer does not require to fetch the data and process it for playing. For example playing audio resources from raw folder.
If you are trying to play a resource from remote source, its a better practice to go for OnPreparedListener() because it might involve fetching and decoding media data.
So, if you know for sure, that your resource is locally available and is of short length, go for Approach 1. Otherwise Approach 2 would be suitable.
Ideally, I prefer this.
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
The MediaPlayer has always been tricky for me to work with. So, I would recommend you to start with the developer docs. Go through it, understand the state diagram. I am sure it will help you in solving lot of questions which you are yet to come across.
Let's say you are playing a video from internet. If you start directly the player it would crash because it may be not ready to play because of some internet problems or something else. But if you use preparedlistener then it will not start the player until it is ready to play.
Therefore it is good to have onpreparedlistener so your activity does not crash or misbehaves.

how can i play mp3 in andengine?

i want to play sound when my condition is matched, but i don't how can i play sound or music in andengine?
help me to find my solution.
your answer will be awarded.
You can obviously use the MediaPlayer in AndEngine too, it works fine as it does in Android, because AndEngine is also a part of Android itself.
You can keep your file in res/raw/anyfile.mp3
MediaPlayer mediaPlayer = MediaPlayer
.create(getApplicationContext(), R.raw.anyfile);
try {
mediaPlayer.start();
mediaPlayer.setLooping(true);
} catch (Exception e) {
e.printStackTrace();
}
though i haven't perform a sound task, but hope this tutorial link will help u,,
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html

Audio attachment with image in android

I have image is my Draw able folder.When I call this image I want to play an audio also which is in my raw folder.can I do this how??please help me.
You can achieve by creating image button and put image on it. and onClick of that button you can play the music using MediaPlayer api.
Refer this : http://developer.android.com/guide/topics/media/index.html
Example code for Play audio from Raw folder
: http://www.barebonescoder.com/2010/06/android-development-audio-playback-safely/
you can write this code at onResume:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),
R.raw.name);
mp.start();
while (mp.isPlaying()) { // donothing
}
;
mp.release();

How to play online radio in android

I am working on android application in which i have play online radio streaming.
i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.
Thank You.
Vikram
Vikram,
You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.
Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().
Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
From what I've read, you should just be able to do:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();
to get basic functionality I believe, but I haven't tested it myself.
the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
}
});
try {
player.setDataSource(currentChannelUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
player.prepareAsync();

Categories

Resources