How to play Audio file Mp3 from the server [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-29 02:25:22

问题


This question already has an answer here:

  • How can I play a mp3 without download from the url? [closed] 6 answers

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.


回答1:


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
}



回答2:


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();



回答3:


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.



来源:https://stackoverflow.com/questions/20628061/how-to-play-audio-file-mp3-from-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!