Modify layout file, /res/layout/activity_main.xml, to add two control buttons (play and stop), and a SurfaceView to hold the media window.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="PLAY"/> <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="STOP"/> <SurfaceView android:id="@+id/mediasurface" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
Modify MainActivity, extends Activity and implements SurfaceHolder.Callback. In the code, path3gp is the path to the Youtube's 3gp video.
package com.example.androidyoutubeplayer; import java.io.IOException; import android.graphics.PixelFormat; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements SurfaceHolder.Callback{ MediaPlayer myMediaPlayer; SurfaceView mySurfaceView; SurfaceHolder mySurfaceHolder; Button btnPlay, btnStop; String path3gp = "rtsp://v8.cache6.c.youtube.com/CjYLENy73wIaLQkP0kiLmutogRMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKjR78WV1ZH5Tgw=/0/0/0/video.3gp"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFormat(PixelFormat.UNKNOWN); mySurfaceView = (SurfaceView)findViewById(R.id.mediasurface); mySurfaceHolder = mySurfaceView.getHolder(); mySurfaceHolder.addCallback(this); myMediaPlayer = new MediaPlayer(); btnPlay = (Button)findViewById(R.id.play); btnStop = (Button)findViewById(R.id.stop); btnPlay.setOnClickListener(playOnClickListener); btnStop.setOnClickListener(stopOnClickListener); } OnClickListener playOnClickListener = new OnClickListener(){ @Override public void onClick(View v) { myMediaPlayer.reset(); myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); myMediaPlayer.setDisplay(mySurfaceHolder); try { myMediaPlayer.setDataSource(path3gp); myMediaPlayer.prepare(); myMediaPlayer.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException 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(); } }}; OnClickListener stopOnClickListener = new OnClickListener(){ @Override public void onClick(View v) { if(myMediaPlayer.isPlaying()){ myMediaPlayer.stop(); } }}; @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }
Modify AndroidManifest.xml to add permission of "android.permission.INTERNET".
Related article: Get video width and height of MediaPlayer and update SurfaceView LayoutParams accordingly.
Hi
ReplyDeleteIt does't work , shows MediaPlayer error (1, -2147483648) , do u konw what's the problem ?