Android 如何从服务器播放音频文件 Mp3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20628061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:10:32  来源:igfitidea点击:

How to play Audio file Mp3 from the server

androidaudiomedia-playerhttp-live-streamingaudio-player

提问by M.ArslanKhan

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.

我想从实时服务器播放音频。我不想下载这些音频文件。这些文件的格式是 mp3.working on android java。

回答by dipali

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
}

回答by Abhishek Shukla

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

回答by Abhishek Shukla

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 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 Contextobject.

如果您在活动中,使用目前还不是getBaseContext().startActivity(intent),或别的东西,这样你可以调用startActivity()一个上Context对象。