播放视频

package com.example.twentyonecode;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;

/**
 * @author HD
 * @date 2015-12-7
 * @package_name com.example.twentyonecode
 * @file_name MainActivity.java
 */
public class MainActivity extends Activity implements OnClickListener {

    private Button btnPlay;

    private Button btnPause;

    private Button btnReplay;

    private VideoView vvLilac;

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

        vvLilac = (VideoView) findViewById(R.id.vvLilac);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPause = (Button) findViewById(R.id.btnPause);
        btnReplay = (Button) findViewById(R.id.btnReplay);

        btnPlay.setOnClickListener(this);
        btnPause.setOnClickListener(this);
        btnReplay.setOnClickListener(this);

        initVideoView();

    }

    private void initVideoView() {
        // TODO 自动生成的方法存根
//      播放视频文件的路径
        File file = new File(Environment.getExternalStorageDirectory()
                .getPath() + "/cet-4/video", "2351.mp4");
        Log.i("hhhd", file.getPath());
//      设置资源路径
        vvLilac.setVideoPath(file.getPath());
    }

    @Override
    public void onClick(View v) {
        // TODO 自动生成的方法存根
        switch (v.getId()) {
        case R.id.btnPlay:
            if (!vvLilac.isPlaying()) {
//              开始播放
                vvLilac.start();
            }
            break;
        case R.id.btnPause:
            if(vvLilac.isPlaying()){
//              暂停播放
                vvLilac.pause();
            }
            break;
        case R.id.btnReplay:
            if(vvLilac.isPlaying()){
//              重新播放
                vvLilac.resume();
            }
            break;

        default:
            break;
        }
    }

    @Override
    protected void onDestroy() {
        // TODO 自动生成的方法存根
        super.onDestroy();
        if(vvLilac != null){
//          释放资源
            vvLilac.suspend();
        }
    }

}
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.twentyonecode.MainActivity" >

    <VideoView
        android:id="@+id/vvLilac"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/vvLilac">

        <Button
            android:id="@+id/btnPlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/vvLilac"
            android:text="开始播放" />

        <Button
            android:id="@+id/btnPause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/vvLilac"
            android:text="暂停视频" />

        <Button
            android:id="@+id/btnReplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/vvLilac"
            android:text="暂停播放" />
    </LinearLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.twentyonecode"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值