目前android自带的解码库只支持 MP4和3gp两种格式 如果你下载"Rockplayer"的应用程序,该程序支持几乎所有的视频格式.
当你向设备加载视频的时候,视频最好是英文名称,因为android很少支持中文。
XML配置:
<?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="420px" android:layout_height="300px" /> <!-- 添加一个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>
java代码:
package jm.out;
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 VedioActivity extends Activity implements OnClickListener,SurfaceHolder.Callback{ String path = "/sdcard/app/liudehua.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) { } }
最主要的是要注意视频文件的路径。![]()