Android学习04音频的播放之SoundPool与MediaPlayer

本文介绍了Android中两种音频播放方式:SoundPool和MediaPlayer。SoundPool适合短促的音效,如按键声,而MediaPlayer适用于长时间的音频播放。文章通过实例展示了如何在布局文件中添加控件,并在Activity类中实现播放、暂停音频的功能。

   Android平台中关于音频的播放有两种方式,一种是SoundPool,一种是MediaPlayer.SoundPool适合短促但反映速度要求高的情况

(如按键声),而MediaPlayer则适合较长但对时间要求不高的情况。

  SoundPool只能用于播放音效,因为超过大约5.6秒的声音便播放不出来,而且加载超过大约5.6秒的音效还会导致其它声音播放的问题

  在res目录下新建raw文件夹把音频文件加入其中

 

 修改布局文件main.xml的代码,修改后完整代码如下:

 

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
  android:id="@+id/textView"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="没有播放任何声音"
    /><!-- 向线性布局中添加一个TextView控件 -->
    <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"  
     android:text="使用MediaPlayer播放声音"   
    /><!-- 向线性布局中添加一个Button控件 -->
    <Button
     android:id="@+id/button2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="暂停MediaPlayer声音"     
    /><!-- 向线性布局中添加一个Button控件 -->
    <Button
     android:id="@+id/button3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="使用SoundPool播放声音"      
    /><!-- 向线性布局中添加一个Button控件 -->
    <Button
     android:id="@+id/button4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="暂停SoundPool声音"      
    /><!-- 向线性布局中添加一个Button控件 -->
</LinearLayout>

 

Activity类代码如下:

package karant.zhan;
import java.util.HashMap;//引入HashMap类
import android.app.Activity;//引入Activity类
import android.content.Context;//引入Context类
import android.media.AudioManager;//引入AudioManager类
import android.media.MediaPlayer;//引入MediaPlayer类
import android.media.SoundPool;//引入SoundPool类
import android.os.Bundle;//引入Bundle类
import android.view.View;//引入View类
import android.view.View.OnClickListener;//引入OnClickListener类
import android.widget.Button;//引入Button类
import android.widget.TextView;//引入TextView类
public class MusicPlayer extends Activity implements OnClickListener{
    Button button1;//四个按钮的引用
    Button button2;
    Button button3;
    Button button4;
    TextView textView;//TextView的引用
 MediaPlayer mMediaPlayer;
 SoundPool soundPool;//声音
 HashMap<Integer, Integer> soundPoolMap;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {//重写onCreate回调方法
        super.onCreate(savedInstanceState);
        initSounds();//初始化声音
        setContentView(R.layout.main);    //设置显示的用户界面
        textView = (TextView) this.findViewById(R.id.textView);//得到TextView的引用
        button1 = (Button) this.findViewById(R.id.button1);//得到button的引用
        button2 = (Button) this.findViewById(R.id.button2);
        button3 = (Button) this.findViewById(R.id.button3);
        button4 = (Button) this.findViewById(R.id.button4);
        button1.setOnClickListener(this);//为四个按钮添加监听
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }
 public void initSounds(){//初始化声音的方法
  mMediaPlayer = MediaPlayer.create(this, R.raw.bull);//初始化MediaPlayer
     soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
     soundPoolMap = new HashMap<Integer, Integer>();  
     soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));
 }
 public void playSound(int sound, int loop) {//用SoundPoll播放声音的方法
     AudioManager mgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);  
     float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);  
     float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);      
     float volume = streamVolumeCurrent/streamVolumeMax;  
     soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);//播放声音
 }   
 public void onClick(View v) {//实现接口中的方法
  // TODO Auto-generated method stub
  if(v == button1){//点击了使用MediaPlayer播放声音按钮
   textView.setText("使用MediaPlayer播放声音");
   if(!mMediaPlayer.isPlaying()){
    mMediaPlayer.start();//播放声音
   }
  }
  else if(v == button2){//点击了暂停MediaPlayer声音按钮
   textView.setText("暂停了MediaPlayer播放的声音");
   if(mMediaPlayer.isPlaying()){
    mMediaPlayer.pause();//暂停声音
   }
  }
  else if(v == button3){//点击了使用SoundPool播放声音按钮
   textView.setText("使用SoundPool播放声音");
   this.playSound(1, 0);
  }
  else if(v == button4){//点击了暂停SoundPool声音按钮
   textView.setText("暂停了SoundPool播放的声音");
   soundPool.pause(1);//暂停SoundPool的声音
  }  
 }
}

 

界面如下:

音乐可以正常播放

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值