Android Soundpool和Mediaplayer的简单demo

在学习Android开发前就一直想做一个类似于play商店中那样可以点击发出声音的简单应用,结果开学前两周忙于课程设计忘记了,闲下来才开始做,发现还是比较容易的,在这个过程中也掌握了soundpool和mediaplayer的使用方法。
程序主要包括主程序mainactivity和布局文件activity_mian.xml,需要在资源文件夹res中新建一个名为raw的文件夹,里面存放音频文件,mp3类型的可以播放,别的格式没怎么测试。
俺感觉soundpool和mediaplayer的区别是soundpool处理的可能是经常播放的短暂声音,例如五子棋程序中的落子声音,mediaplayer处理的是一般的长资源文件。

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.example.knighthart.pajipajipaji;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//接口方式设置响应事件并初始化控件
private Button btnplay;
private Button btnpause;
private Button btnstop;
private Button buttonplay;
private SoundPool soundPool;
private MediaPlayer mPlayer = null;
private boolean isRelease = true; //判断是否MediaPlayer是否释放的标志
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
//bindViews是对于控件和soundpool的初始化
}

private void bindViews() {
btnplay = (Button) findViewById(R.id.btn_play);
btnpause = (Button) findViewById(R.id.btn_pause);
btnstop = (Button) findViewById(R.id.btn_stop);
buttonplay=(Button)findViewById(R.id.button_play);

btnplay.setOnClickListener(this);
btnpause.setOnClickListener(this);
btnstop.setOnClickListener(this);
buttonplay.setOnClickListener(this);
soundPool= new SoundPool(10, AudioManager.STREAM_MUSIC,5);
soundPool.load(this,R.raw.pajipajipaji,1);
}

@Override
public void onClick(View v) {
//设置响应事件
switch (v.getId()){
case R.id.btn_play:
if(isRelease){
mPlayer = MediaPlayer.create(this, R.raw.silentwindbell);
isRelease = false;
}
mPlayer.start();//开始播放
Toast toast1=Toast.makeText(getApplicationContext(), "我有一首silent wind bell献给篝管理", Toast.LENGTH_LONG);
toast1.show();
btnplay.setEnabled(false);
btnpause.setEnabled(true);
btnstop.setEnabled(true);
break;
case R.id.btn_pause:
mPlayer.pause(); //停止播放
btnplay.setEnabled(true);
btnpause.setEnabled(false);
btnstop.setEnabled(false);
break;
case R.id.btn_stop:
mPlayer.reset(); //重置MediaPlayer
mPlayer.release(); //释放MediaPlayer
isRelease = true;
btnplay.setEnabled(true);
btnpause.setEnabled(false);
btnstop.setEnabled(false);
break;
case R.id.button_play:
Toast toast2=Toast.makeText(getApplicationContext(), "四斋蒸鹅心", Toast.LENGTH_LONG);
toast2.show();
soundPool.play(1,1, 1, 0, 0, 1);
break;
}
}
}

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context="com.example.knighthart.pajipajipaji.MainActivity">

<Button
android:id="@+id/button_play"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="啪叽啪叽啪叽~"
tools:layout_editor_absoluteX="159dp"
tools:layout_editor_absoluteY="230dp" />

<Button
android:id="@+id/btn_play"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/button_play"
android:layout_marginTop="50dp"
android:text="播放" />

<Button
android:id="@+id/btn_pause"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_play"
android:layout_alignStart="@+id/button_play"
android:text="暂停" />

<Button
android:id="@+id/btn_stop"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/btn_pause"
android:text="停止" />

</RelativeLayout>

主界面

 主界面

具体用法参考Android中播放声音的两种方法

官方API文档: