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

作者 : 开心源码 本文共2915个字,预计阅读时间需要8分钟 发布时间: 2022-05-12 共184人阅读

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.songma.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毫秒

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » android 循环播报电话号码或者数字,并比较MediaPlayer、SoundPool、AudioTrack

发表回复