-2
  private MediaPlayer mp = new MediaPlayer();
  mp.reset();                       
  mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
  mp.prepare();
  mp.start();

I am following this code .But not working enter image description here

Angel
  • 143
  • 1
  • 4

4 Answers4

0

For streaming you want to prepareAsync() instead of prepare

     public class MusicPlayer implements OnPreparedListener //, OnErrorListener , OnCompletionListener
     {

        private MediaPlayer mp;
        public MusicPlayer()
        {
             mp = new MediaPlayer();
             mp.setOnPreparedListener(this);
             mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        public void play()
        {
              mp.reset();                       
              mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
              mp.prepareAsync();
        }
        @Override
        public void onPrepared(MediaPlayer arg0)//
        {
            mp.start();
        }
}

You need to implement OnErrorListener , OnCompletionListener on later stage

Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
  • I believe this isn't strictly true. (1) Streaming does work with sync (just not the best thing to do); (2) with async you need to start AFTER the prepare is complete - you need to use `onPreparedListener` on the mediaplayer. – Aleks G Oct 21 '13 at 12:10
  • am try this codes also, but same error coming, am testing in emulator – Angel Oct 21 '13 at 12:11
  • yes you are right you just need the onPreparedListener and it will work, ill update my code – Ahmad Dwaik 'Warlock' Oct 21 '13 at 12:28
0

set Internet permission in your AndoridManifest.xml file

String url = "http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3"; // your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        try{
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
        mediaPlayer.start();
        }catch(IOException e){
            e.printStackTrace();
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }catch(IllegalStateException e){
            e.printStackTrace();
        }
vinay kumar
  • 1,451
  • 13
  • 33
0

Use this..

String url = "http://........"; // 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();

Also use the INTERNET Permission in your Manifest.

Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
0
private MediaPlayer mp = new MediaPlayer();                    
  mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
  mp.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();

            }
        });
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151