Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android http live Streaming URL using mediaplayer

I am trying to play an url but its not playing and the code i used is below..the logcat is showing Mediaplayer error(1,-1002), start state is 0 and error(-38, 0) why...? where i am going wrong......can u help me out how to play........

       import java.io.IOException;

       import android.app.Activity;
       import android.media.AudioManager;
       import android.media.MediaPlayer;
         import android.os.Bundle;
      import android.view.View;
      import android.widget.ImageButton;
       import android.widget.TextView;

        public class BacaFatihahActivity extends Activity {



        final String songs_urIs= "http://stream.radiosai.net:8002/";
           // private TextView txt_song_title;
        private MediaPlayer mediaplayer;
        @Override
         public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  ImageButton btn_play = (ImageButton) findViewById(R.id.button_play);
  ImageButton btn_pause = (ImageButton) findViewById(R.id.button_pause);
  ImageButton btn_next = (ImageButton) findViewById(R.id.button_next);
  ImageButton btn_previous = (ImageButton) findViewById(R.id.button_Previous);
  //txt_song_title = (TextView) findViewById(R.id.txt_song_title);

  mediaplayer = new MediaPlayer();
  mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


  btn_play.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        try {
            mediaplayer.setDataSource(songs_urIs);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            mediaplayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         mediaplayer.start();   
    }
       });


        }
       }
like image 790
user1051599 Avatar asked Nov 17 '11 11:11

user1051599


People also ask

Can Android use HLS?

It is the default media streaming protocol for all iOS devices, but it can be used on Android and web browsers. The basic building blocks of a HLS streams are: M3U8 playlists. Media files for various streams.

How do I play audio files on Android?

Prepare media file: To play a media file, you need to first prepare it i.e. you need to load the file for playback. Methods used for doing this are prepare(), prepareAsync(), and setDataSource(). Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method.

How do I play HLS videos on Android?

To integrate the ExoPlayer into the Android app, you need to add the following dependency in your project's build. gradle file. if (player == null) { exoplayer_player = new DemoPlayer(getRendererBuilder(THE HLS VIDEO URL)); exoplayer_player . addListener(this); exoplayer_player .


1 Answers

right code but wrong api level~

it's not every api level supports this way to play a media,http live streaming ,may be you need api level 10 or higher

like image 54
MoiTempete Avatar answered Oct 02 '22 05:10

MoiTempete