Android Mp3来自网址

时间:2014-01-24 22:08:17

标签: android media-player mp3

如何使用在线mp3网址运行以下教程?我尝试更换网址,但它似乎没有工作。我想使用相同的代码但使用url。有没有人有任何建议?

教程linke:http://www.tutorialspoint.com/android/android_mediaplayer.htm mp3网址为:http://searchgurbani.com/audio/sggs/1.mp3

2 个答案:

答案 0 :(得分:7)

如何从/ raw文件夹中播放.mp3:

下载.mp3文件,将其保存到song.mp3并粘贴到/ raw文件夹中。 如果您没有/ raw文件夹,只需将其创建到/ res文件夹即可。

enter image description here

此示例未从Internet加载.mp3,从资源播放.mp3。

  mediaPlayer = MediaPlayer.create(this, R.raw.song);

如何 play an .mp3 from the url:

将示例的oncreate()方法更改为:

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

   }

这样您就可以从指定的网址播放音频mp3。

别忘了添加

 <uses-permission android:name="android.permission.INTERNET"/>

进入你的Manifest.xml

答案 1 :(得分:1)

通过URL

    try {
        String url = "http://www.all-birds.com/Sound/western%20bluebird.wav"; // your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
        mediaPlayer.start();
    }catch (Exception e){
        e.printStackTrace();
    }

通过res --->内->行文件夹

MediaPlayer mp2 = MediaPlayer.create(this, R.raw.genuine_);
mp2.start();