Android 使用mediaplayer播放res/raw文件夹中的音乐的实例
作者:lqh 时间:2023-12-19 08:28:11
Android 使用mediaplayer播放res/raw文件夹中的音乐的实例
(1)在res文件夹中新建一个文件夹重命名为raw,并且将要播放的音乐放到raw文件夹里面
(2)修改layout目录下的xml布局文件,添加3个按钮空间和一个文本控件,用于提示当前播放状态和 播放暂停 停止等功能。具体代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单击播放开始播放音乐" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
</LinearLayout>
(3)打开MainActivity 在该类中,定义所需的成员变量,具体代码如下
private MediaPlayer mp;//mediaPlayer对象
private Button play,pause,stop;//播放 暂停/继续 停止 按钮
private TextView hint;//显示当前播放状态
private boolean isPause=false;//是否暂停
(4)在onCreate()方法中,获取播放 暂停/继续 停止 按钮 提示当前状态的文本框,并为mediaplayer对象创建播放的对象,具体代码如下。
play=(Button) findViewById(R.id.button1);
pause=(Button) findViewById(R.id.button2);
stop=(Button) findViewById(R.id.button3);
hint=(TextView) findViewById(R.id.hint);
hint.setTextSize(20);
mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//创建mediaplayer对象
(5)编写用于播放音乐的无返回值的play()方法。在该方法中首先调用mediaplayer对象的reset()方法重置mediaplayer对象,然后重新为其设置要播放的音频文件。最后调用start()方法开始播放音频
private void play(){
try{
mp.reset();
mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//重新设置要播放的音频
mp.start();//开始播放
hint.setText("正在播放音频...");
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}catch(Exception e){
e.printStackTrace();//输出异常信息
}
}
(6)为mediaplayer对象添加完成时间 * ,用于当音乐播放完毕后重新开始播放音乐
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
play();//重新开始播放
}
});
(7)为播放按钮添加单击事件 *
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play();
if(isPause){
pause.setText("暂停");
isPause=false;
}
}
});
(8)为暂停按钮添加单击事件 *
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()&&!isPause){
mp.pause();
isPause=true;
pause.setText("继续");
hint.setText("暂停播放音频...");
play.setEnabled(true);
}else{
mp.start();
pause.setText("暂停");
hint.setText("继续播放音频...");
isPause=false;
play.setEnabled(false);
}
}
});
(9)为停止按钮添加单击事件 *
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
hint.setText("停止播放音频...");
pause.setEnabled(false);
stop.setEnabled(false);
play.setEnabled(true);
}
});
(10)一定要记得这个。重写Activity的onDestroy()方法,用于在当前Activity销毁时,停止正在播放的音频,并释放mediaplayer所占用的资源,否则你每打开一次就会播放一次,并且上次播放的不会停止 你可以试试的,我解释不清楚
protected void onDestroy() {
// TODO Auto-generated method stub
if(mp.isPlaying()){
mp.stop();
}
mp.release();//释放资源
super.onDestroy();
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/su20145104009/article/details/50640025
标签:Android,mediaplayer,res/raw,音乐
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何通过XML方式配置并实现Mybatis
2021-12-21 02:49:04
![](https://img.aspxhome.com/file/2023/0/125340_0s.png)
使用String类型小数值转换为Long类型
2023-04-14 10:34:56
Input系统之InputReader处理触摸事件案例
2022-12-18 07:32:35
springboot中validator数据校验功能的实现
2021-07-31 17:43:50
![](https://img.aspxhome.com/file/2023/2/69322_0s.png)
Spring项目中使用Junit单元测试并配置数据源的操作
2022-06-02 05:32:27
详解 C# 中XML对象的序列化和反序列化
2022-06-24 12:55:27
![](https://img.aspxhome.com/file/2023/6/82216_0s.png)
Androidstudio调用摄像头拍照并保存照片
2022-01-23 18:33:57
![](https://img.aspxhome.com/file/2023/0/137590_0s.jpg)
Java虚拟机内存区域划分详解
2023-06-20 22:04:24
![](https://img.aspxhome.com/file/2023/6/57516_0s.png)
如何把spring boot项目部署到tomcat容器中
2023-10-08 18:53:51
Android8.1原生系统网络感叹号消除的方法
2022-09-29 18:38:52
![](https://img.aspxhome.com/file/2023/4/138334_0s.png)
Java对xls文件进行读写操作示例代码
2023-08-04 17:55:31
Android 更新RecyclerView的好方法
2023-11-24 09:39:59
javaweb上传下载实例完整版解析(上)
2021-07-31 05:40:23
![](https://img.aspxhome.com/file/2023/4/63114_0s.jpg)
Java深入讲解二十三种设计模式之中的策略模式
2022-11-25 13:23:15
![](https://img.aspxhome.com/file/2023/5/99855_0s.png)
HighCharts图表控件在ASP.NET WebForm中的使用总结(全)
2022-07-13 02:11:12
![](https://img.aspxhome.com/file/2023/9/108849_0s.png)
java设置session过期时间的实现方法
2022-02-18 20:25:29
Java中OAuth2.0第三方授权原理与实战
2021-09-27 01:48:57
![](https://img.aspxhome.com/file/2023/4/70994_0s.jpg)
C# 运用params修饰符来实现变长参数传递的方法
2023-07-21 22:27:55
Android RecycleView滑动停止后自动吸附效果的实现代码(滑动定位)
2023-06-05 21:23:40
![](https://img.aspxhome.com/file/2023/5/139655_0s.gif)
Springboot与vue实现数据导出方法具体介绍
2023-11-06 21:00:34