没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
3页
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_p
资源推荐
资源详情
资源评论
Android 使用使用mediaplayer播放播放res/raw文件夹中的音乐的实例文件夹中的音乐的实例
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();
资源评论
weixin_38675232
- 粉丝: 3
- 资源: 970
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功