从URL的Android播放MP3 [英] Android Mp3 play from url
问题描述
我如何运行使用在线MP3网址下面的教程?我试着更换网址,但它似乎并不奏效。我想用同样的code,但与URL。有没有人有什么建议?
How do I run the following tutorial using an online mp3 url? I tried replacing the url but it doesn't seem to be working. I want to use the same code but with the url. Does anyone have any suggestions?
本教程临客: http://www.tutorialspoint.com/android/android_mediaplayer.htm 该MP3的网址是: http://searchgurbani.com/audio/sggs/1.mp3
The tutorial linke: http://www.tutorialspoint.com/android/android_mediaplayer.htm The mp3 url is: http://searchgurbani.com/audio/sggs/1.mp3
推荐答案
下载.MP3文件,保存到Song.mp3的,并粘贴到/原始文件夹。 如果您鸵鸟政策有/原始文件夹,只需创建到/ res文件夹。
Download the .mp3 file, save it to song.mp3 and paste into the /raw folder. If you don´t have /raw folder, just create it into the /res folder.
这个例子doesn't从互联网加载的MP3播放,从资源方面发挥的MP3播放。
this example doesn´t load the .mp3 from internet, play the .mp3 from the resources.
mediaPlayer = MediaPlayer.create(this, R.raw.song);
要 从URL播放MP3播放
,改变例子的OnCreate()方法:
To play the .mp3 from the url
, change the oncreate() method of the example to:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_video);
songName = (TextView)findViewById(R.id.textView4);
startTimeField =(TextView)findViewById(R.id.textView1);
endTimeField =(TextView)findViewById(R.id.textView2);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
playButton = (ImageButton)findViewById(R.id.imageButton1);
pauseButton = (ImageButton)findViewById(R.id.imageButton2);
songName.setText("song.mp3");
//mediaPlayer = MediaPlayer.create(this, R.raw.song);
Uri myUri = Uri.parse("http://searchgurbani.com/audio/sggs/1.mp3");
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, myUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IOException e) {
e.printStackTrace();
}
seekbar.setClickable(false);
pauseButton.setEnabled(false);
}
所以你将能够播放从URL中的音频MP3播放指定的。
so you will able to play the audio mp3 from the url specified.
,别忘添加
<uses-permission android:name="android.permission.INTERNET"/>
到您的Manifest.xml
into your Manifest.xml
这篇关于从URL的Android播放MP3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!