您的位置:首页 > 其它

019_01播放视频之SurfaceView

2015-06-01 13:09 288 查看
  MediaPlayer主要用于播放音频,因此它没有提供图像输出界面,所以要借助于SurfaceView来显示MediaPalyer播放的图像输出。

  SurfaceHolder是一个接口,其作用就像一个关于Surface的监听器。提供访问和控制SurfaceView背后的Surface 相关的方法 (providingaccess and control over this SurfaceView's underlying surface),它通过三个回调方法,让我们可以感知到Surface的创建、销毁或者改变。在SurfaceView中有一个方法getHolder,可以很方便地获得SurfaceView所对应的Surface所对应的SurfaceHolder

package com.example.day19_01videoplayer;

import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class MainActivity extends Activity {

private SurfaceHolder holder;
private MediaPlayer mediaPlayer;
private final int STATE_IDLE = 0;
private final int STATE_PLAYING = 1;
private final int STATE_PAUSED = 2;
private final int STATE_STOP =3;

private int current_state = STATE_IDLE;
private String path;

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

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.sf_video);
holder = surfaceView.getHolder();
path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/sirendingzhi.mp4";

}

public void play(View v){

if (mediaPlayer==null) {
mediaPlayer = new MediaPlayer();
}

if (current_state==STATE_PAUSED) {
mediaPlayer.start();
}

if (current_state==STATE_IDLE||current_state==STATE_STOP) {
mediaPlayer.reset();
try {
mediaplay();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

current_state=STATE_PLAYING;

}

private void mediaplay() throws IOException {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(holder);
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();

}

public void pause(View v){
if (current_state==STATE_PLAYING) {
mediaPlayer.pause();
current_state=STATE_PAUSED;
}
}

public void stop(View v){
if (mediaPlayer!=null) {
if (current_state==STATE_PLAYING||current_state==STATE_PAUSED) {
mediaPlayer.stop();
mediaPlayer.reset();
current_state=STATE_STOP;
}
}

}

public void replay(View v){

if (current_state!=STATE_IDLE) {
if (current_state==STATE_STOP) {

}

if (current_state==STATE_PLAYING||current_state==STATE_PAUSED) {
mediaPlayer.stop();
mediaPlayer.reset();
}
try {
mediaplay();
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
current_state=STATE_PLAYING;
}
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

if (mediaPlayer!=null) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}
}


<LinearLayout 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.day19_01videoplayer.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:onClick="play" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:onClick="pause" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="stop" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重播"
android:onClick="replay" />
</LinearLayout>

<SurfaceView
android:id="@+id/sf_video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</LinearLayout >


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: