android:使用mediaplayer播放res/raw文件夹中的音乐

本文介绍了如何在Android应用中使用MediaPlayer组件从res/raw目录播放音乐。通过创建LinearLayout布局,添加按钮和文本控件实现播放、暂停和停止功能。详细展示了Java代码实现播放逻辑,并确保资源的正确释放。
摘要由CSDN通过智能技术生成

(1)在res文件夹中新建一个文件夹重命名为raw,并且将要播放的音乐放到raw文件夹里面




(2)修改layout目录下的xml布局文件,添加3个按钮空间和一个文本控件,用于提示当前播放状态和 播放暂停 停止等功能。具体代码如下

[java]  view plain   copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/hint"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="单击播放开始播放音乐" />  
  11.   
  12.     <LinearLayout  
  13.         android:orientation="horizontal"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content" >  
  16.           
  17.      <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="播放" />  
  22.   
  23.     <Button  
  24.         android:id="@+id/button2"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="暂停" />  
  28.   
  29.     <Button  
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值