How to play an .mp3 from the /raw folder:
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.
this example doesn′t load the .mp3 from internet, play the .mp3 from the resources.
mediaPlayer = MediaPlayer.create(this, R.raw.song);
How to play an .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
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
seekbar.setClickable(false);
pauseButton.setEnabled(false);
}
so you will able to play the audio mp3 from the url specified.
don′t forget to add
<uses-permission android:name="android.permission.INTERNET"/>
into your Manifest.xml
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…