读书人

游戏开发2_04 多媒体-视频

发布时间: 2012-09-27 11:11:17 作者: rapoo

游戏开发2_04 多媒体---视频
package wyf.ytl;
import android.app.Activity;//引入Activity类
import android.graphics.PixelFormat;//引入PixelFormat类
import android.media.AudioManager;//引入AudioManager类
import android.media.MediaPlayer;//引入MediaPlayer类
import android.os.Bundle;//引入Bundle类
import android.view.SurfaceHolder;//引入SurfaceHolder类
import android.view.SurfaceView;//引入SurfaceView类
import android.view.View;//引入View类
import android.view.View.OnClickListener;//引入OnClickListener类
import android.widget.Button;//引入Button类
public class Sample_2_11 extends Activity implements OnClickListener,SurfaceHolder.Callback{
String path = "/sdcard/bbb.3gp";
Button play_Button;
Button pause_Button;
boolean isPause = false;
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
SurfaceView surfaceView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play_Button = (Button) findViewById(R.id.play2_Button);
play_Button.setOnClickListener(this);
pause_Button = (Button) findViewById(R.id.pause2_Button);
pause_Button.setOnClickListener(this);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176,144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
}
public void onClick(View v) {
if(v == play_Button){//按下播放电影按钮
isPause = false;
playVideo(path);
}
else if(v == pause_Button){//按下暂停按钮,
if(isPause == false){//如果正在播放则将其暂停
mediaPlayer.pause();
isPause = true;
}
else{//如果暂停中怎继续播放
mediaPlayer.start();
isPause = false;
}
}
}
private void playVideo(String strPath){//自定义播放影片函数
if(mediaPlayer.isPlaying()==true){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);//设置Video影片以SurfaceHolder播放
try{
mediaPlayer.setDataSource(strPath);
mediaPlayer.prepare();
}
catch (Exception e){
e.printStackTrace();
}
mediaPlayer.start();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
public void surfaceCreated(SurfaceHolder arg0) {
}
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}


<?xml version="1.0" encoding="utf-8"?><!-- XML的版本以及编码方式 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
><!--添加一个垂直的线性布局 -->
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="320px"
android:layout_height="200px"
/><!--添加一个SurfaceView用于播放视频 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
><!--添加一个线性布局 -->
<Button
android:id="@+id/play2_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
/><!--添加一个按钮 -->
<Button
android:id="@+id/pause2_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
/> <!--添加一个按钮 -->
</LinearLayout>
</LinearLayout>

读书人网 >网络游戏

热点推荐