这里,首先附上软件运行后的界面:

1.在播放音乐中,我介绍过利用MediaPlayer来播放音乐,这个类占用资源多,并且不支持播放多个音频,在Android中还提供了一个播放音频的类,即SoundPool类,也叫音频池,可以同时播放多个短小的音频,占用的资源也比较少,主要适合在应用程序中播放按键音和消息提示音,在一些小游戏中的密集和短暂的声音就是用到这个类,使用SoundPool类播放音频,首先要创建SoundPool对象,然后加载所要播放的音频,最后调用play()方法播放音频,我一直觉得这个类可以用来做猜歌曲的小游戏,所以下面将介绍如何使用这个类来完成猜歌的小应用。

2.首先,我先处理好我所需要的音频文件,我比较喜欢音乐,其中大部分歌手的音乐我都听,这里我就采用香港Beyond乐队的一些经典歌曲来使用,我利用一个音频处理软件,对Beyond的8首歌曲进行截取,截取的都是高潮部分,这样比较容易猜一点,截取完的8个音频文件如下图所示:

3.做好音频处理工作后,打开Eclipse软件工具,新建一个安卓项目,项目名称为MusicGuessTest,首先,进行界面布局,我布局的界面不美观,也不太好看,打开res目录下的layout目录下的activity_main.xml文件,代码附上:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/title"        android:textColor="#99CCFF"        android:textSize="20sp" />   <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/oneMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button1" />                <Button            android:id="@+id/guess1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/twoMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button2" />        <Button            android:id="@+id/guess2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/threeMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button3" />        <Button            android:id="@+id/guess3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/fourMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button4" />        <Button            android:id="@+id/guess4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/fiveMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button5" />        <Button            android:id="@+id/guess5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/sixMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button6" />        <Button            android:id="@+id/guess6"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/sevenMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button7" />        <Button            android:id="@+id/guess7"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/eightMusicButtton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button8" />        <Button            android:id="@+id/guess8"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="guess"            android:text="@string/guess" />    </LinearLayout></LinearLayout>

4.接下来就把上面我已经处理好的音频文件复制到项目中来,在此项目中的res目录下新建一个raw文件夹,把8个音频文件放入进来,接下来就可以写MainActivity.java的代码了,打开src目录下的包下的MainActivity.java的代码,其中点击每一个播放音乐的按钮就播放一小段Beyond乐队的歌曲的高潮部分,点击右边的我猜?你猜?歌曲按钮就弹出个对话框,其中有个编辑框供我们输入答案,点击对话框中的提交按钮便可知道你猜的是对是错,不多说,首先,我们有用到一个item.xml文件,这个文件放在res目录下的layout目录下面,用来显示对话框中的编辑框效果

item.xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <EditText    android:id="@+id/editText1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:inputType="text"/>    </LinearLayout>


MainActivity.java文件,里面有简单注释,用来播放一小段音频和显示对话框:

package com.xg.musicguesstest;import java.util.HashMap;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.media.AudioManager;import android.media.SoundPool;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private SoundPool soundpool;//声明一个SoundPool对象private HashMap<Integer,Integer> soundmap=new HashMap<Integer,Integer>();//创建一个HashMap对象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*获取播放八首一小段Beyond乐队的音乐按钮*/Button oneMusic=(Button)findViewById(R.id.oneMusicButtton);//获取“播放第一首音乐”按钮Button twoMusic=(Button)findViewById(R.id.twoMusicButtton);//获取“播放第二首音乐”按钮Button threeMusic=(Button)findViewById(R.id.threeMusicButtton);//获取“播放第三首音乐”按钮Button fourMusic=(Button)findViewById(R.id.fourMusicButtton);//获取“播放第四首音乐”按钮Button fiveMusic=(Button)findViewById(R.id.fiveMusicButtton);//获取“播放第五首音乐”按钮Button sixMusic=(Button)findViewById(R.id.sixMusicButtton);//获取“播放第六首音乐”按钮Button sevenMusic=(Button)findViewById(R.id.sevenMusicButtton);//获取“播放第七首音乐”按钮Button eightMusic=(Button)findViewById(R.id.eightMusicButtton);//获取“播放第八首音乐”按钮soundpool=new SoundPool(8, AudioManager.STREAM_MUSIC, 0);//创建一个SoundPool对象,该对象可以容纳8个音频流//将要播放的音频流保存到HashMap对象中soundmap.put(1, soundpool.load(MainActivity.this, R.raw.one, 1));soundmap.put(2, soundpool.load(MainActivity.this, R.raw.two, 1));soundmap.put(3, soundpool.load(MainActivity.this, R.raw.three, 1));soundmap.put(4, soundpool.load(MainActivity.this, R.raw.four, 1));soundmap.put(5, soundpool.load(MainActivity.this, R.raw.five, 1));soundmap.put(6, soundpool.load(MainActivity.this, R.raw.six, 1));soundmap.put(7, soundpool.load(MainActivity.this, R.raw.seven, 1));soundmap.put(8, soundpool.load(MainActivity.this, R.raw.eight, 1));//分别为那八个播放Beyond乐队音频的按钮添加事件监听器,在Onclick()方法中播放指定的音频oneMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(1), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频one.mp3,并设置为不循环播放}});twoMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频two.mp3,并设置为不循环播放}});threeMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频three.mp3,并设置为不循环播放}});fourMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频four.mp3,并设置为不循环播放}});fiveMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频five.mp3,并设置为不循环播放}});sixMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(6), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频six.mp3,并设置为不循环播放}});sevenMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(7), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频seven.mp3,并设置为不循环播放}});eightMusic.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsoundpool.play(soundmap.get(8), 1, 1, 0, 0, 1);//播放指定音频,即在res目录下的raw目录下的音频eight.mp3,并设置为不循环播放}});} public void guess(View v){switch (v.getId()) {case R.id.guess1:openDialog();break;case R.id.guess2:openDialog2();break;case R.id.guess3:openDialog3();break;case R.id.guess4:openDialog4();break;case R.id.guess5:openDialog5();break;case R.id.guess6:openDialog6();break;case R.id.guess7:openDialog7();break;case R.id.guess8:openDialog8();break;default:break;}}private void openDialog() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你猜到的歌曲一的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("光辉岁月")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog2() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲二的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("海阔天空")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog3() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲三的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("真的爱你")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog4() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲四的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data=="喜欢你"){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog5() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲五的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("情人")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog6() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲六的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("再见理想")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog7() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲七的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("曾是拥有")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}private void openDialog8() {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setMessage("请在下面的编辑框中输入你所猜到的歌曲八的歌曲名");final View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);builder.setView(view);builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {EditText editText1=(EditText)view.findViewById(R.id.editText1);String data=editText1.getText().toString();if(data.equals("遥望")){Toast.makeText(MainActivity.this, "恭喜你,答对了!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "对不起,答错了!请重新猜这首音乐的歌曲名...", Toast.LENGTH_SHORT).show();}}});builder.create().show();}}


5.最后,再把这个例子所用到的字符串文件strings.xml附上:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">MusicGuessTest</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string><string name="title">大家一起来猜一猜下面的beyond乐队的音乐歌曲名吧!</string><string name="button1">播放第一首音乐</string><string name="button2">播放第二首音乐</string><string name="button3">播放第三首音乐</string><string name="button4">播放第四首音乐</string><string name="button5">播放第五首音乐</string><string name="button6">播放第六首音乐</string><string name="button7">播放第七首音乐</string><string name="button8">播放第八首音乐</string><string name="guess">我猜?你猜?歌曲</string></resources>


6.运行此项目后,效果如下:

这里我点击播放第一首音乐按钮,播放一小段之后音乐自动关闭,再右边对应的我猜?你猜?歌曲按钮,弹出下图所显示的对话框,下图已经输入猜到的歌曲名:

点击提交按钮后,猜对了就弹出下图所示消息提示框:

7.如果点击播放第二首音乐按钮,听我之后,点击右边的猜歌按钮,输入猜到的歌曲名,如下图所示:

其中如果答案输入有空格的文本或者错误的答案,就会弹出下图的情况:

其它的按钮类似于上面的情况,就这样!

8.在这个例子中,主要使用了SoundPool类来播放多个一小段的音频,主要的步骤有下面几个:

(1).创建SoundPool对象,SoundPool类提供了一个构造方法,用来创建SoundPool对象,格式如下:

SoundPool(int maxStreams,intstreamType,int srcQuality),其中第一个参数maxStreams用于指定可以容纳多少个音频,第二个参数streamType用于指定声音类型,第三个参数srcQuality用于指定音频的品质,默认为0.

(2).加载所要播放的音频,创建SoundPool对象后,可以调用load()方法来加载要播放的音频,这里的格式可以查看Android API,或者直接在所写的项目中查看,因为Eclipse有提示功能,通过资源ID来加载音频文件可以这样做:soundpool.load(this, R.raw.one, 1)。

(3).播放音频,调用SoundPool对象的play()方法可播放指定的音频,格式如下:

soundpool.play(soundID, leftVolume, rightVolume, priority, loop, rate),其中soundId用于指定要播放的音频,该音频通过load()方法返回所要播放的音频,leftVolume指定为左声道的音量,rightVolume指定为右声道的音量,取值范围都是0到1,priority用于指定播放音频的优先级,数值越大,优先级越高,loop用于指定循环次数,0为不循环,-1为循环,rate用于指定速率,1为正常,最低为0.5,最高为2.上面的例子播放音频使用代码为:soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1);

9.注:这里猜歌时弹出对话框要我们输入你所猜到的歌曲名时用到了中文,安卓模拟器没有中文输入法,必须给它安装一个,才可以输入中文,首先去百度一下搜狗输入法,下载一个搜狗输入法的apk文件,下载到计算机中,我是放在D盘,如果路径放置在其它地方,只要复制一下文件路径,把文件路径放在adb install后面即可,这个必须要配置了adb环境才行,如何配置就百度一下了,输入如下图的命令,安装成功后模拟器就有了搜狗输入法,在配置一下就行了:

10.以上就是此文章的全部内容,利用SoundPool来播放音频,仅供大家学习参考,写得不好,请见谅,如有错误请指出,谢谢!

更多相关文章

  1. Android游戏编程之音频编程
  2. 基于android的网络音乐播放器-本地音乐的加载和后台播放(一)
  3. Android视频编辑器(五)音频编解码、从视频中分离音频、音频混音、
  4. Android(安卓)中动画的实现
  5. Android耳机线控-播放/暂停/上一曲/下一曲
  6. android实现MP3音频录制(lame,支持暂停)
  7. Android音频焦点处理服务
  8. Android7.0系统应用包名信息
  9. 本地音乐播放器(三)

随机推荐

  1. 源码解析Android中AsyncTask的工作原理
  2. RelativeLayout
  3. 对android的android:taskAffinity初识
  4. android view的几种布局方式
  5. Android中Handler引起的内存泄露
  6. Android(安卓)软键盘
  7. android图片压缩质量参数Bitmap.Config R
  8. android 谷歌地图开发
  9. Android(安卓)远程调试 JNI 实现
  10. android 绿色开发环境