Android开发实现的简单媒体播放器功能示例
本文实例讲述了Android开发实现的简单媒体播放器功能。分享给大家供大家参考,具体如下:
创新互联公司从2013年创立,是专业互联网技术服务公司,拥有项目网站建设、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元嘉祥做网站,已为上家服务,为嘉祥各地企业和个人服务,联系电话:13518219792
一、概述:
因为播放视频需要很大的内存,所以必须使用surfaceview ,
surfaceview 里实现了双缓冲的功能,
二、全部代码:
/** * @描述 使用surfaceview 创建视频 * @项目名称 App_Basic * @包名 com.example.basic.media * @类名 MediaPlayerActivity * @author chenlin * @date 2013-3-25 上午7:36:32 * @version 1.0 */ public class MediaPlayerActivity extends BaseActivity { private MediaPlayer mPlayer; private Button mBtnPlay; private Button mBtnPause; private SurfaceView mSurfaceView; private int mCurrentPosition; private SeekBar seekBar; private boolean isPlaying; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSurfaceView = new SurfaceView(this); seekBar = new SeekBar(this); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); if (mPlayer != null && isPlaying) { mPlayer.seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceView.getHolder().addCallback(new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mPlayer!= null && mPlayer.isPlaying()) { mCurrentPosition = mPlayer.getCurrentPosition(); stop(); } } @Override public void surfaceCreated(SurfaceHolder holder) { if (mCurrentPosition > 0) { play(mCurrentPosition); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } }); } public void play(final int currentPosition){ try { mPlayer = new MediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); Uri myUri = Uri.fromFile(new File("sdcard/mp3/1.mp3")); mPlayer.setDataSource(getApplicationContext(), myUri); mPlayer.setLooping(true); mPlayer.setDisplay(mSurfaceView.getHolder()); mPlayer.prepareAsync(); //mPlayer.prepare(); mBtnPlay.setEnabled(false); mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mBtnPlay.setEnabled(true); mPlayer.release(); } }); mPlayer.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { mBtnPlay.setEnabled(true); mPlayer.release(); isPlaying = false; return false; } }); mPlayer.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { mPlayer.start(); mPlayer.seekTo(currentPosition); seekBar.setMax(mPlayer.getDuration()); new Thread(new Runnable() { @Override public void run() { isPlaying = true; while(isPlaying){ seekBar.setProgress(mPlayer.getCurrentPosition()); SystemClock.sleep(500); } } }); } }); } catch (Exception e) { e.printStackTrace(); } } public void pause(){ if ("接续".equals(mBtnPause.getText().toString())) { mBtnPause.setText("暂停"); mPlayer.start(); return; } if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.pause(); mBtnPause.setText("继续"); return; } } public void reset(){ if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.seekTo(0); return; } play(0); } public void stop(){ if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.stop(); mPlayer.release(); mPlayer = null; mBtnPlay.setEnabled(true); isPlaying = false; } } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
文章名称:Android开发实现的简单媒体播放器功能示例
地址分享:http://azwzsj.com/article/jojhcp.html