Android三种播放视频的方式
- - CSDN博客推荐文章在Android中,我们有三种方式来实现视频的播放:. 指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型. 2、使用VideoView来播放. 在布局文件中使用VideoView结合MediaController来实现对其控制. 3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活.
一,MainActivity.java源码
import android.app.Activity; import android.graphics.PixelFormat; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; public class EX07_14 extends Activity implements SurfaceHolder.Callback { private TextView mTextView01; private static final String TAG = "HIPPO_MediaPlayer"; //打印日志的标志 private MediaPlayer mMediaPlayer01; private SurfaceView mSurfaceView01; private SurfaceHolder mSurfaceHolder01; private ImageButton mPlay; private ImageButton mPause; private ImageButton mReset; private ImageButton mStop; private boolean bIsPaused = false; private boolean bIsReleased = false; private String strVideoPath = ""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if(!checkSDCard()) //如果没有SD卡 { mMakeTextToast ( getResources().getText(R.string.str_err_nosd).toString(), true ); } mTextView01 = (TextView)findViewById(R.id.myTextView1); getWindow().setFormat(PixelFormat.UNKNOWN); mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1); //显示动画用的容器 mSurfaceHolder01 = mSurfaceView01.getHolder(); mSurfaceHolder01.addCallback(this); mSurfaceHolder01.setFixedSize(176,144); mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mPlay = (ImageButton) findViewById(R.id.play); mPause = (ImageButton) findViewById(R.id.pause); mReset = (ImageButton) findViewById(R.id.reset); mStop = (ImageButton) findViewById(R.id.stop); strVideoPath = "/sdcard/a.3gp"; mPlay.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if(checkSDCard()) { playVideo(strVideoPath); } } }); mPause.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if(checkSDCard()) { if (mMediaPlayer01 != null) { if(bIsReleased == false) { if(bIsPaused==false) { mMediaPlayer01.pause(); bIsPaused = true; mTextView01.setText(R.string.str_pause); } else if(bIsPaused==true) { mMediaPlayer01.start(); bIsPaused = false; mTextView01.setText(R.string.str_play); } } } } } }); mReset.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if(checkSDCard()) { if(bIsReleased == false) { if (mMediaPlayer01 != null) { mMediaPlayer01.seekTo(0); } } } } }); mStop.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if(checkSDCard()) { if (mMediaPlayer01 != null) { if(bIsReleased==false) { mMediaPlayer01.stop(); mMediaPlayer01.release(); bIsReleased = true; mTextView01.setText(R.string.str_stop); } } } } }); } private void playVideo(String strPath) { mMediaPlayer01 = new MediaPlayer(); mMediaPlayer01.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer01.setDisplay(mSurfaceHolder01); try { mMediaPlayer01.setDataSource(strPath); } catch (Exception e) { // TODO Auto-generated catch block mTextView01.setText("setDataSource Exceeption:"+e.toString()); } try { mMediaPlayer01.prepare(); } catch (Exception e) { // TODO Auto-generated catch block mTextView01.setText("prepare Exceeption:"+e.toString()); } mMediaPlayer01.start(); bIsReleased = false; mTextView01.setText(R.string.str_play); mMediaPlayer01.setOnCompletionListener (new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub mTextView01.setText(R.string.str_stop); } }); } private boolean checkSDCard() { if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } public void mMakeTextToast(String str, boolean isLong) { if(isLong==true) { Toast.makeText(EX07_14.this, str, Toast.LENGTH_LONG).show(); } else { Toast.makeText(EX07_14.this, str, Toast.LENGTH_SHORT).show(); } } @Override public void surfaceChanged (SurfaceHolder surfaceholder, int format, int w, int h) { // TODO Auto-generated method stub Log.i(TAG, "Surface Changed"); } @Override public void surfaceCreated(SurfaceHolder surfaceholder) { // TODO Auto-generated method stub Log.i(TAG, "Surface Changed"); } @Override public void surfaceDestroyed(SurfaceHolder surfaceholder) { // TODO Auto-generated method stub Log.i(TAG, "Surface Destroyed"); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@drawable/blue" android:text="@string/hello" /> <SurfaceView android:id="@+id/mSurfaceView1" android:layout_width="100px" android:layout_height="100px"> </SurfaceView> <SeekBar android:id="@+id/seekBar" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:padding="10dip" > <ImageButton android:id="@+id/play" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/play" /> <ImageButton android:id="@+id/pause" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/pause" /> <ImageButton android:id="@+id/reset" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/reset" /> <ImageButton android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/stop" /> </LinearLayout> </LinearLayout>