VideoView播放视频

  • AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pengdl.videoviewtest" >

    <uses-sdk android:minSdkVersion="21"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".VideoViewTest"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这里需要注意的是:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

  • activity_video_view_test.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".VideoViewTest">

    <VideoView
        android:id="@+id/videoview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

 

  • VideoViewTest.java
package com.example.pengdl.videoviewtest;

import android.app.Activity;
import android.util.Log;
import android.widget.MediaController;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.VideoView;

import java.io.File;

public class VideoViewTest extends Activity {

    private VideoView video1;
    public static String TAGS = "VideoViewTest";
    MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view_test);

        video1 = (VideoView)findViewById(R.id.videoview1);
        mediaController = new MediaController(this);
        File file = new File("/sdcard/Movies/u-boot.mp4");

        if (file.exists()) {
            Log.d(TAGS, "file "+file.getAbsolutePath()+" Begin to playbak.");
            video1.setVideoPath(file.getAbsolutePath());
            video1.setMediaController(mediaController);
            mediaController.setMediaPlayer(video1);
            video1.requestFocus();
            video1.start();
        } else {
            Log.d(TAGS, "File not exist.");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_video_view_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

 

posted @ 2015-08-06 20:01  dolinux  阅读(884)  评论(0)    收藏  举报