【Android】播放视频的简易播放器源码
2012-04-05 13:35
387 查看
一,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中已经传入播放文件
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中已经传入播放文件
相关文章推荐
- android新手 调用系统默认播放器播放视频在视频没有放完时如何关闭播放器
- Android 调用系统播放器播放视频
- Android 百度云媒体 等播放器播放4:3等多种比例的视频 大小配置的问题
- Android 音视频深入 十三 OpenSL ES 制作音乐播放器,能暂停和调整音量(附源码下载)
- Android 音视频深入 十四 FFmpeg与OpenSL ES 播放mp3音乐,能暂停(附源码
- Android 音视频深入 六 使用FFmpeg播放视频(附源码下载)
- Rexsee API介绍:Android视频播放,Rexsee的VideoPlayer函数说明与源码
- android 编写简易视频播放器
- android平台源码编译,播放yuv的视频
- android开发视频播放器------Vitamio第一个例子 播放url类型文件
- android开发--指定播放器来播放视频文件
- Android视频播放项目总结之 让别的程序能调用我的视频播放器
- android调用系统播放器播放视频-小坑一个
- Android 音视频深入 二 AudioTrack播放pcm(附源码下载)
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- android 简易的播放器(实现了 播放、暂停、下一首和上一首)
- android2.0源码下视频无法播放的解决办法
- Android 音视频深入 三 MP4解码播放视频 (附源码下载)
- Android 音视频深入 十一 FFmpeg和AudioTrack播放声音(附源码下载)
- Android SurfaceView播放视频源码