Android开发之MediaPlayer基本使用方法详解-创新互联
本文实例讲述了Android MediaPlayer基本使用方法。分享给大家供大家参考,具体如下:
从事川西大数据中心,服务器租用,云主机,虚拟主机,域名注册,CDN,网络代维等服务。使用MediaPlayer播放音频或者视频的最简单例子:
Java代码部分:
public class MediaPlayerStudy extends Activity { private Button bplay,bpause,bstop; private MediaPlayer mp = new MediaPlayer(); @Override public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bplay =(Button)findViewById(R.id.play); bpause =(Button)findViewById(R.id.pause); bstop =(Button)findViewById(R.id.stop); bplay.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { try { mp.setDataSource("/sdcard/test.mp3"); mp.prepare(); mp.start(); } catch (IllegalArgumentException e){ e.printStackTrace(); } catch (IllegalStateException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.setOnCompletionListener(new OnCompletionListener(){ @Override public void onCompletion(MediaPlayer mp){ mp.release(); } }); } }); bpause.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(mp != null){ mp.pause(); } } }); bstop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(mp != null){ mp.stop(); } } }); } @Override protected void onDestroy() { if(mp != null) mp.release(); super.onDestroy(); } }
分享名称:Android开发之MediaPlayer基本使用方法详解-创新互联
标题网址:http://myzitong.com/article/dpspsj.html