AudioVideoVideoPlayActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class AudioVideoVideoPlayActivity extends Activity { VideoView mVideoView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get a reference to the VideoView mVideoView = (VideoView) findViewById(R.id.videoViewer); // Add a Media controller to allow forward/reverse/pause/resume final MediaController mMediaController = new MediaController( AudioVideoVideoPlayActivity.this, true); mMediaController.setEnabled(false); mVideoView.setMediaController(mMediaController); mVideoView .setVideoURI(Uri .parse("android.resource://course.examples.AudioVideo.VideoPlay/raw/moon")); // Add an OnPreparedListener to enable the MediaController once the video is ready mVideoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mMediaController.setEnabled(true); } }); } // Clean up and release resources @Override protected void onPause() { if (mVideoView != null && mVideoView.isPlaying()) { mVideoView.stopPlayback(); mVideoView = null; } super.onPause(); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <VideoView android:id="@+id/videoViewer" android:layout_width="480dp" android:layout_height="640dp" android:layout_centerInParent="true" android:gravity="center" > </VideoView> </RelativeLayout> |