本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單媒體播放器功能。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元嘉祥做網(wǎng)站,已為上家服務(wù),為嘉祥各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
一、概述:
因?yàn)椴シ乓曨l需要很大的內(nèi)存,所以必須使用surfaceview ,
surfaceview 里實(shí)現(xiàn)了雙緩沖的功能,
二、全部代碼:
/** * @描述 使用surfaceview 創(chuàng)建視頻 * @項(xiàng)目名稱(chēng) App_Basic * @包名 com.example.basic.media * @類(lèi)名 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 ("接續(xù)".equals(mBtnPause.getText().toString())) { mBtnPause.setText("暫停"); mPlayer.start(); return; } if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.pause(); mBtnPause.setText("繼續(xù)"); 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; } } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
當(dāng)前標(biāo)題:Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單媒體播放器功能示例
URL鏈接:http://jinyejixie.com/article28/jojhcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、域名注冊(cè)、網(wǎng)站建設(shè)、品牌網(wǎng)站制作、Google、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)