zoukankan      html  css  js  c++  java
  • Android中播放声音

    Android系统中,有两种播放声音的方式,一种是通过MediaPlayer,另外一种是通过SoundPool。前者主要用于播放长时间的音乐,而后者用于播放小段小段的音效,像按键音这种,其优点是资源占用了小,同时能够载入多个声音片段,再根据需要选择播放。下面分别介绍这两种方式:

    1、MediaPlayer

    MediaPlayer有两种创建方式,方式一:

    1
    MediaPlayer mp = new MediaPlayer()

    通过这种方式创建MediaPlayer对象,必须在创建对象之后使用下面的语句:

    1
    2
    mp.setDataSource("filePath");
    mp.prepare();

    然后就是调用start()方法。注意使用这种方法设置播放的音乐的时候需要使用的是路径,这样对于一般的应用使用起来就不是很方便,不能直接打包到apk中。
    方式二:

    1
    mp = MediaPlayer.create(this, R.raw.music);

    通过这种方式创建MediaPlayer对象,就不需要setDataSource()和prepare()了,直接start()就好了。

    当需要停止播放音乐的时候,使用下面的方式:

    1
    2
    3
    4
    if(mp.isPlaying()){
        mp.stop();
    }
    mp.reset();

    需要注意的是,在stop之前一定要确认mp正在播放,因为如果已经停止播放了,再次stop会引发错误的,原因就是MediaPlayer有一个严格的生命周期,在错误的时期调用错误的方法,是一定会报错的。

    2、SoundPool

    SoundPool在播放音效的时候特别的好用,为什么呢?因为它可以一次load较多的声音片段。下面首先介绍其基本的使用方法:

    1
    2
    3
    4
    5
    6
    7
    //创建
    SoundPool sp = new SoundPool(10, StreamType.MUSIC, 5);
    //载入
    soundPoolMap = new HashMap<integer, integer="">();
    soundPoolMap.put(0, soundPool.load(context, R.raw.sound1, 1));
    //播放
    soundPool.play(soundPoolMap.get(0), 1, 1, 0, 0, 1);</integer,>

    各个函数中参数的具体意义不是重点,这里就不做介绍了,各位自己去查一下。我想要说的是,如果各位在主线程中直接这么做,很可能会报错(Sample X Not Ready),这是为什么呢?

    原来,SoundPool.load()这个函数是立即返回的,也就是说,不管载入好了没有,这个函数都会返回,但是实际上非常有可能声音尚未载入结束,那应该怎么做呢?实际上,系统在load完成之后,会发送广播,这时候才能播放。又因为这是一个费时的操作,所以最好新建一个线程来实现。下面是我的实现方案:

    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
    Handler handler;
    static SoundPool soundPool;
    static HashMap<integer, integer=""> soundPoolMap;
    String TAG = "wtianok";
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
     
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        soundPoolMap = new HashMap<integer, integer="">();
     
        // 载入soundPool
        new LoadSoundPoolThread().run(this);
     
        handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                if (msg.what == MessageType.SOUND_POOL_READY.ordinal()) {
                    Log.d(TAG, "finish load message received");
                    //do somethind
                }
            }
        }
    }
     
    private class LoadSoundPoolThread extends Thread {
        public void run(Context context) {
            Log.d(TAG, "start to load soundpool");
            soundPool.setOnLoadCompleteListener(new MyOnLoadCompleteListener());
            soundPool.load(context, R.raw.sound1, 1);
            soundPool.load(context, R.raw.sound2, 1);
            soundPool.load(context, R.raw.sound3, 1);
            soundPool.load(context, R.raw.sound4, 1);
            soundPool.load(context, R.raw.sound5, 1);
            soundPool.load(context, R.raw.sound6, 1);
            soundPool.load(context, R.raw.sound7, 1);
        }
     
        private class MyOnLoadCompleteListener implements
                OnLoadCompleteListener {
            int count = 1;
     
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) { //注意这里的形参
                Log.d(TAG, "load " + count + " sound");
                soundPoolMap.put(count, sampleId);
                if (count == 7) {
                    Log.d(TAG, "finish load soundpool");
                    MainActivity.soundPool = soundPool;
                    sendMsg(MessageType.SOUND_POOL_READY);
                    count = 1;
                }
                count++;
            }
        }
    }
     
    private void sendMsg(MessageType micTestStartRecord) {
        Message message = Message.obtain();
        message.what = micTestStartRecord.ordinal();
        handler.sendMessage(message);
    }</integer,></integer,>


    需要注意的是,在完成载入之后,我使用了下面这一句:

    1
    MainActivity.soundPool = soundPool;

    如果不加这一句,我在主线程中play的时候,依然会报Sample X Not Ready错误,而如果在收到广播之后调用play是没有问题的。仔细看,发现在接收广播的onLoadComplete()函数中,实际上调用的是函数的局部参数soundPool,而不是全局的soundPool,但是从代码来看,load的时候调用的确实全局的soundPool,那为什么用全局的soundPool就不能播放呢?我到现在还是没有想通,但是项目还是要继续,只好加了上面那一句,将全局的soundPool重新赋值,事实证明,这样做可以。

    结伴旅游,一个免费的交友网站:www.jieberu.com

    推推族,免费得门票,游景区:www.tuituizu.com

  • 相关阅读:
    CommonJS和AMD/CMD
    map 有{}的时候需要有return 没有{}的时候不需要有return
    sublime3添加插件
    C++—模板(1)模板与函数模板
    函数的调用过程(栈帧)
    Linux-进程描述(5)之进程环境
    Linux-进程描述(4)之进程优先级与进程创建执行
    多态(2)纯虚函数与重载、重写(覆盖)、重定义(隐藏)
    多态(1)静态多态与动态多态以及虚函数相关
    Linux-进程描述(3)之进程状态僵尸进程与孤儿进程
  • 原文地址:https://www.cnblogs.com/rabbit-bunny/p/4203600.html
Copyright © 2011-2022 走看看