Android SDK: Media Player - Download video stream from HTTP-url

I have a MediaPlayerActivity with the following code: This code basically tries to get the video stream from the http url and download it, but for some reason it continues to fail.

public class MediaPlayerActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.video_player); SurfaceView v = (SurfaceView) findViewById(R.id.surface_video); SurfaceHolder holder = v.getHolder(); holder.setFixedSize(400,300); MediaPlayer mp = MediaPlayer.create(this, Uri.parse("http://stream-url.com/playlist.m3u8")); mp.setDisplay(holder); //mp.setAudioStreamType(2); try { //mp.prepare(); mp.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

video_player.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SurfaceView android:id="@+id/surface_video" android:layout_width="250px" android:layout_height="250px"> </SurfaceView> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:padding="10dip" > </LinearLayout> </LinearLayout> 

When I go to this action using the following code, it crashes:

 Intent myIntent = new Intent(HomeActivity.this, MediaPlayerActivity.class); HomeActivity.this.startActivity(myIntent); 

What am I doing wrong?

+4
source share
1 answer

Without magazines, two sentences:

  • try implementing SurfaceHolder.Callback.surfaceCreated() .
  • try using MediaPlayer.create() which accepts a SurfaceHolder

Details (1)

Your surface may not have been created yet when you call start() . You should use MediaPlayer.setDisplay() and MediaPlayer.start() only after surface is created. To do this, you should add override MediaPlayer.start() only after surface is created. To do this, you should add override SurfaceHolder.Callback.surfaceCreated () `. For example, your code might look like this.

 public class MediaPlayerActivity extends Activity implements SurfaceHolder.Callback { MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.video_player); SurfaceView v = (SurfaceView) findViewById(R.id.surface_video); SurfaceHolder holder = v.getHolder(); holder.setFixedSize(400,300); holder.addCallback(this). mp = MediaPlayer.create(this, Uri.parse("http://stream-url.com/playlist.m3u8")); @Override public void surfaceCreated(SurfaceHolder holder) { mp.setDisplay(holder); try { mp.start(); } catch (IllegalStateException e) { e.printStackTrace(); } } } 

Details (2)

It seems that there is another MediaPlayer.create() that takes SurfaceHolder as one of the arguments - you can try: http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri, android.view.SurfaceHolder)

+4
source

Source: https://habr.com/ru/post/1210160/


All Articles