I want to play audio from the live server. I don't want to download these audio files. Format of these files are mp3.working on android java.
Asked
Active
Viewed 3.4k times
3 Answers
32
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://xty/MRESC/images/test/xy.mp3");
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}

Glorfindel
- 21,988
- 13
- 81
- 109

dipali
- 10,966
- 5
- 25
- 51
-
@dipali.Thanks very much I got your code its working perfect but when during playing audio ir play button is pressed again then forced closed happend. – M.ArslanKhan Dec 17 '13 at 12:40
-
@dipali I got a warning : Activity not started, its current task has been brought to the front. Can you explain me! – iCrazybest Jun 20 '15 at 01:31
-
Assuming that a proper header is set on the uri resource, will the file be cached in RAM or else the audio file is downloaded each time we play it? – GA1 Jul 19 '17 at 23:25
9
Try doing this:
MediaPlayer mp = new MediaPlayer(/*Your-Context*/);
mp.setDataSource(/*MP3 file's Url*/);
mp.setOnPreparedListener(new OnPreparedListener(){
onPrepared(MediaPlayer mp)
{
mp.start();
}
});
mp.prepareAsync();

Maveňツ
- 1
- 12
- 50
- 89

Abhishek Shukla
- 1,242
- 8
- 11
-
-
-
-
There is no constructor like MediaPlayer mp = new MediaPlayer(/*Your-Context*/); So I used, MediaPlayer mp = new MediaPlayer(); Then the code is working. – Thirumalvalavan Nov 29 '16 at 09:46
5
If you simply want to open the stream in the default music player (so that you don't have to deal with implementing player buttons, continuing running in the background, etc.), you can use an Intent
:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://example.com/file.mp3"), "audio/mp3");
startActivity(intent);
If you're not currently in an activity, use getBaseContext().startActivity(intent)
, or something else so that you can call startActivity()
on a Context
object.