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() {
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) {
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() {
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>