【Android】播放视频的简易播放器源码

一,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"); } }
二,main.xml 源码

<?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>
三,源码中所需其余图片等 ,自备就可以。经测试通过可以播放,前提需要sdcard中已经传入播放文件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值