android 循环播报电话号码或数字,并比较MediaPlayer、SoundPool、AudioTrack

android 提供各种音频播放的方法,目前介绍3钟

MediaPlayer

目前最多人用的播放工具,支持多种音视频文件的播放
详细使用可以看看这篇文章
https://blog.csdn.net/world_kun/article/details/79788250

这是一个集成度较高的工具类,拥有较全的方法,用于播报号码来说是大材小用,因为他的提取机制,速度上会受到一定的影响。
会自动解析音频文件
播放过程是个并发机制,声音会叠加一起。但是支持了结束的回调方法。因此采用递归的方法进行语音播报

1先引入音频文件,实现结束方法

        ZERO=MediaPlayer.create(mcontext,R.raw.zerov);
        ONE=MediaPlayer.create(mcontext,R.raw.onev);
        TWO=MediaPlayer.create(mcontext,R.raw.twov);
        THREE=MediaPlayer.create(mcontext,R.raw.threev);
        FOUR=MediaPlayer.create(mcontext,R.raw.fourv);
        FIVE=MediaPlayer.create(mcontext,R.raw.fivev);
        SIX=MediaPlayer.create(mcontext,R.raw.sixv);
        SEVEN=MediaPlayer.create(mcontext,R.raw.sevenv);
        EIGHT=MediaPlayer.create(mcontext,R.raw.eightv);
        NINE=MediaPlayer.create(mcontext,R.raw.ninev);
        ZERO.setOnCompletionListener(this);
        ONE.setOnCompletionListener(this);
        TWO.setOnCompletionListener(this);
        THREE.setOnCompletionListener(this);
        FOUR.setOnCompletionListener(this);
        FIVE.setOnCompletionListener(this);
        SIX.setOnCompletionListener(this);
        SEVEN.setOnCompletionListener(this);
        EIGHT.setOnCompletionListener(this);
        NINE.setOnCompletionListener(this);
    @Override
    public void onCompletion(MediaPlayer mp) {
        if(i!=0) {
            nextSound();
        }
    }

2实现递归方法和传入

    public void playSound(String number){
        if(number!=null&&(!number.equals(""))){
           chars=number.toCharArray();
           i=0;
            nextSound();
        }
    }
    private void nextSound(){
        if(chars!=null&&chars.length>0&&i<chars.length){
            switch (chars[i]){
                case '0':
                    ZERO.start();
                    break;
                case '1':
                    ONE.start();
                    break;
                case '2':
                    TWO.start();
                    break;
                case '3':
                    THREE.start();
                    break;
                case '4':
                    FOUR.start();
                    break;
                case '5':
                    FIVE.start();
                    break;
                case '6':
                    SIX.start();
                    break;
                case '7':
                    SEVEN.start();
                    break;
                case '8':
                    EIGHT.start();
                    break;
                case '9':
                    NINE.start();
                    break;
            }

            if(++i>=chars.length){
                i=0;
            }
        }

    }

3传入电话号码使用

  soundUtil.playSound(number.getText().toString());

11位手机号码约为3500毫秒念完。

SoundPool

为实现短促,变换的声音,经常使用到此方法,将音频文件引入音频池
可参考此文章使用
https://www.jianshu.com/p/526bc0d80afe

性能和速度上比MediaPlayer好,但不能监听结束事件。同样也是默认并发播放,但能通过设置改为串行播放。
使用上比MediaPlayer简单。
1初始化方法

     musicId = new HashMap();
        pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        musicId.put('0', pool.load(mcontext, R.raw.zerov, 0));
        musicId.put('1', pool.load(mcontext, R.raw.onev, 0));
        musicId.put('2', pool.load(mcontext, R.raw.twov, 0));
        musicId.put('3', pool.load(mcontext, R.raw.threev, 0));
        musicId.put('4', pool.load(mcontext, R.raw.fourv, 0));
        musicId.put('5', pool.load(mcontext, R.raw.fivev, 0));
        musicId.put('6', pool.load(mcontext, R.raw.sixv, 0));
        musicId.put('7', pool.load(mcontext, R.raw.sevenv, 0));
        musicId.put('8', pool.load(mcontext, R.raw.eightv, 0));
        musicId.put('9', pool.load(mcontext, R.raw.ninev, 0));

2.实现方法

    public void playPool(String number){
        if(number!=null&&(!number.equals(""))){
            chars=number.toCharArray();
            for (int j = 0; j < chars.length; j++) {
                System.out.println("chars[j]="+chars[j]+'='+musicId.get(chars[j]));
                pool.play(musicId.get(chars[j]),1,1,0,0,1);
            }

        }
    }

3使用

 soundUtil.playPool(number.getText().toString());

资源加载速度加快,2500毫秒左右念完

AudioTrack

用的较少,比较基础
可直接参考此处
https://developer.android.google.cn/reference/android/media/AudioTrack?hl=zh-tw

直接将byte流传入播放

约为1500毫秒

    原文作者:蔡振辉
    原文地址: https://www.jianshu.com/p/c863b0fe3bc2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞