本菜今天又学习了一下使用MediaPlay实现视频播放的功能.
代码也不复杂..
使用SurfaceView, 来实现视频播放的窗口, 使用MediaPlay来实现视频的播入控制.
进度条是使用Timer来实时从MediaPlay中获取视频的播放进度来进行刷新.
截图:
代码如下:
com/example/youtwo/testvideo/MainActivity.java
package com.example.youtwo.testvideo;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
private SurfaceView surfaceView = null;
private SurfaceHolder surfaceHolder = null;
private MediaPlayer m = null;
private Button btn_start_video = null;
private Button btn_stop_video = null;
private boolean isChanging = false;
private Timer mTimer;
private TimerTask mTimerTask;
private SeekBar skb_video = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m = new MediaPlayer();
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
btn_start_video = (Button) this.findViewById(R.id.Button03);
btn_start_video.setOnClickListener(new ClickEvent());
btn_stop_video = (Button) this.findViewById(R.id.Button04);
btn_stop_video.setOnClickListener(new ClickEvent());
skb_video = (SeekBar) this.findViewById(R.id.SeekBar);
skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent());
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
if (isChanging == true)
return;
skb_video.setProgress(m.getCurrentPosition());
}
};
mTimer.schedule(mTimerTask, 0, 10);
}
@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_main, 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);
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btn_start_video) {
m.reset();
m.setDisplay(surfaceHolder);//设置屏幕
try {
m.setDataSource("/sdcard/test.mp4");
m.prepare();
m.start();
skb_video.setMax(m.getDuration());//设置SeekBar的长度
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(v == btn_stop_video) {
m.stop();
}
}
}
/*
* SeekBar进度改变事件
*/
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isChanging = true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isChanging = false;
}
}
}
<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=".MainActivity">
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放视频"
android:id="@+id/Button03" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放"
android:id="@+id/Button04" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SeekBar"
android:indeterminate="false"
android:layout_weight="1"
android:layout_below="@+id/LinearLayout01"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</LinearLayout>
</LinearLayout>
<SurfaceView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/surfaceView"
android:layout_below="@+id/LinearLayout01"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>