Android游戏开发学习笔记(三):视频的播放
一、在命令行下输入mksdcard 512M c:\sdcard.img命令,创建sdcard镜像文件。
二、单击Eclipse的菜单命令“Window/Preferences”,打开Preferences窗口,在左侧选中“Android/Launch”,在右侧的“Default emulator options”中填入“-sdcard c:\sdcard.img”。
三、启动模拟器,切换视图到DDMS,找到“File Explorer”窗口,选中“mnt/sdcard”,执行右上角的“Push a file onto the device”,向sdcard中添加视频文件“qtfy01.3gp”。
四、编写项目的布局文件main.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Su易做图ceView
android:id="@+id/su易做图ceView"
android:layout_width="fill_parent"
android:layout_height="380px"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/play_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
/>
<Button
android:id="@+id/pause_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
/>
</LinearLayout>
</LinearLayout>
五、编写MainActivity.java代码。
package game.test;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements Su易做图ceHolder.Callback {
String path = "/sdcard/qtfy01.3gp";
Button play_btn, pause_btn;
boolean isPause = false;
Su易做图ceHolder su易做图ceHolder;
MediaPlayer mediaPlayer;
Su易做图ceView su易做图ceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play_btn = (Button) findViewById(R.id.play_btn);
play_btn.setOnClickListener(video_listener);
pause_btn = (Button) findViewById(R.id.pause_btn);
pause_btn.setOnClickListener(video_listener);
getWindow().setFormat(PixelFormat.UNKNOWN);
su易做图ceView = (Su易做图ceView) findViewById(R.id.su易做图ceView);
su易做图ceHolder = su易做图ceView.getHolder();
su易做图ceHolder.addCallback(this);
su易做图ceHolder.setFixedSize(176, 144);
su易做图ceHolder.setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
}
private OnClickListener video_listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play_btn:
isPause = false;
playVideo(path);
break;
case R.id.pause_btn:
if (isPause == false) {
mediaPlayer.pause();
isPause = true;
} else {
mediaPlayer.start();
isPause = false;
}
break;
}
}
private void playVideo(String path) {
// TODO Auto-generated method stub
if (mediaPlayer.isPlaying() == true) {
 
补充:移动开发 , Android ,