videoview小例

阅读更多

    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />

     VideoPlayer.java文件如下:
package com.simon;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
import android.content.pm.ActivityInfo;

public class VideoPlayer extends Activity implements MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
public static final String TAG = "VideoPlayer";
private VideoView mVideoView;
private Uri mUri;
private int mPositionWhenPaused = -1;

private MediaController mMediaController;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //Set the screen to landscape.
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mVideoView = (VideoView)findViewById(R.id.video_view);

        //Video file
        mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/1.3gp");

        //Create media controller
        mMediaController = new MediaController(this);
        mVideoView.setMediaController(mMediaController);
    }

    public void onStart() {
    // Play Video
    mVideoView.setVideoURI(mUri);
    mVideoView.start();

    super.onStart();
    }

    public void onPause() {
    // Stop video when the activity is pause.
    mPositionWhenPaused = mVideoView.getCurrentPosition();
    mVideoView.stopPlayback();
    Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
    Log.d(TAG, "OnStop: getDuration  = " + mVideoView.getDuration());

    super.onPause();
    }

    public void onResume() {
    // Resume video player
    if(mPositionWhenPaused >= 0) {
    mVideoView.seekTo(mPositionWhenPaused);
    mPositionWhenPaused = -1;
    }

    super.onResume();
    }

    public boolean onError(MediaPlayer player, int arg1, int arg2) {
        return false;
    }

    public void onCompletion(MediaPlayer mp) {
    this.finish();
    }
}

你可能感兴趣的:(videoview小例)