Android开发之MediaPlayer基本使用方法详解
作者:Jacob-wj 时间:2022-09-05 12:21:38
本文实例讲述了Android MediaPlayer基本使用方法。分享给大家供大家参考,具体如下:
使用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();
}
}
布局文件main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:text="play" android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="pause" android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="stop" android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
程序说明:
这个例子只是描述了MediaPlayer的基本使用步骤和方式,MediaPlayer还有多种使用方式和方法,并不只局限于例子所介绍的一种。具体来看:
1)如何获得MediaPlayer实例:
可以使用直接new的方式:
MediaPlayer mp = new MediaPlayer();
也可以使用create的方式,如:
MediaPlayer mp = MediaPlayer.create(this, R.raw.test);
//这时就不用调用setDataSource了
2) 如何设置要播放的文件:
MediaPlayer要播放的文件主要包括3个来源:
a. 用户在应用中事先自带的resource资源
例如:
MediaPlayer.create(this,R.raw.test);
b. 存储在SD卡或其他文件路径下的媒体文件
例如:
mp.setDataSource("/sdcard/test.mp3");
c. 网络上的媒体文件
例如:
mp.setDataSource("http://www.citynorth.cn/music/confucius.mp3");
MediaPlayer的setDataSource一共四个方法:
① setDataSource(String path)
② setDataSource(FileDescriptor fd)
③ setDataSource(Context context, Uri uri)
④ setDataSource(FileDescriptor fd, long offset, long length)
3)对播放器的主要控制方法:
Android通过控制播放器的状态的方式来 * 体文件的播放,其中:
prepare()
和prepareAsync()
提供了同步和异步两种方式设置播放器进入prepare状态,需要注意的是,如果MediaPlayer实例是由create方法创建的,那么第一次启动播放前不需要再调用prepare()了,因为create方法里已经调用过了。start()
是真正启动文件播放的方法,pause()
和stop()
比较简单,起到暂停和停止播放的作用,seekTo()
是定位方法,可以让播放器从指定的位置开始播放,需要注意的是该方法是个异步方法,也就是说该方法返回时并不意味着定位完成,尤其是播放的网络文件,真正定位完成时会触发OnSeekComplete.onSeekComplete(),如果需要是可以调用setOnSeekCompleteListener(OnSeekCompleteListener)设置 * 来处理的。release()
可以释放播放器占用的资源,一旦确定不再使用播放器时应当尽早调用它释放资源。reset()
可以使播放器从Error状态中恢复过来,重新会到Idle状态。
4)设置播放器的 * :
MediaPlayer提供了一些设置不同 * 的方法来更好地对播放器的工作状态进行监听,以期及时处理各种情况,
如:
setOnCompletionListener(MediaPlayer.OnCompletionListener listener)
、setOnErrorListener(MediaPlayer.OnErrorListener listener)
等,设置播放器时需要考虑到播放器可能出现的情况设置好监听和处理逻辑,以保持播放器的健壮性。
希望本文所述对大家Android程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring boot 动态生成接口实现类的场景分析
![](https://img.aspxhome.com/file/2023/7/64207_0s.png)
处理java异步事件的阻塞和非阻塞方法分析
![](https://img.aspxhome.com/file/2023/1/72341_0s.png)
如何在Android studio 中使用单例模式
MyBatis-Plus找不到Mapper.xml文件的几种解决方法
C#深拷贝方法探究及性能比较(多种深拷贝)
![](https://img.aspxhome.com/file/2023/5/77235_0s.png)
Spring如何利用@Value注解读取yml中的map配置
Java线程的生命周期和状态控制_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/2/77132_0s.png)
Android studio实现简单的计算器
![](https://img.aspxhome.com/file/2023/3/98623_0s.jpg)
Java面试题之HashMap 的 hash 方法原理是什么
![](https://img.aspxhome.com/file/2023/8/80948_0s.jpg)
Maven安装及MyEclipse中使用Maven
![](https://img.aspxhome.com/file/2023/7/69307_0s.png)
c#线程同步使用详解示例
关于Feign调用服务Headers传参问题
![](https://img.aspxhome.com/file/2023/5/71025_0s.jpg)
C# List<T>的用法小结
利用Postman和Chrome的开发者功能探究项目(毕业设计项目)
![](https://img.aspxhome.com/file/2023/9/117519_0s.png)
Java操作pdf的工具类itext的处理方法
Android 定时器实现图片的变换
Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔
![](https://img.aspxhome.com/file/2023/1/94241_0s.png)
Android中ImageView的使用方法
![](https://img.aspxhome.com/file/2023/4/108244_0s.jpg)
C# 设计模式系列教程-简单工厂模式
![](https://img.aspxhome.com/file/2023/3/75623_0s.png)
深入理解Java设计模式之建造者模式
![](https://img.aspxhome.com/file/2023/8/94218_0s.jpg)