使用AudioTrack实现声音播放

这篇文章主要讲的是AudioTrack的用法。

AudioTrack 一般用来实现播放原始音频数据(PCM)


觉得这个类没有什么好说的,咱就直接上代码吧。

步骤一:

配置需要的参数,初始化AudioTrack对象。



我们需要了解一下这些参数

streamType    这个参数表示使用哪种音频管理策略
      STREAM_VOCIE_CALL         电话声音
            STREAM_SYSTEM                系统声音
            STREAM_RING                      铃声 
            STREAM_MUSCI                   音乐声
            STREAM_ALARM                  警告声
            STREAM_NOTIFICATION     通知声

sampleRateInHz,     采样率(我在使用AudioRecord实现声音采集一文中说明) 


channelConfig,      声道数(我在使用AudioRecord实现声音采集一文中说明) 

audioFormat,        量化精度(我在使用AudioRecord实现声音采集一文中说明) 

bufferSizeInBytes,  缓存大小(我在使用AudioRecord实现声音采集一文中说明) 

mode        播放模式。一种是static方式,另一种是streaming方式。
            static方式:需要一次性将所有的数据都写入播放缓冲区,适合播放铃声,较短的音频片段。
            streaming方式:按照一定的时间间隔不间断的写入音频数据。(常用这种方式)

  
    private AudioTrack  audioTrack;
    private int  streamType = AudioManager.STREAM_MUSIC;
    private int  sampleRateInHz = 44100;
    private int  channelConfig = AudioFormat.CHANNEL_OUT_MONO;
    private int  audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private int  bufferSizeInBytes;
    private int  mode = AudioTrack.MODE_STREAM;

        bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);

        audioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,bufferSizeInBytes,mode);


步骤二:

还是播放

        audioTrack.play();

步骤三:

开启一个子线程,需要不间断的向AudioTrack缓冲区 “写入”数据

步骤四:

释放资源

int len = -1;
        try {
            while((len = dataInputStream.read(bys)) != -1 ){
                 audioTrack.write(bys,0,len);
            }
            audioTrack.stop();
            audioTrack.release();
        } catch (IOException e) {
            e.printStackTrace();
        }

AudioTrack读取PCM文件播放声音。


请多多指教

接下来一篇文章我会将带来  将PCM格式存储成WAV格式文件


发布了29 篇原创文章 · 获赞 9 · 访问量 3万+
展开阅读全文

android报错 not enough memory for AudioTrack size=131296

02-16

not enough memory for AudioTrack size=131296 我现在把全部音效的部分都注释了 但是还是闪退 ``` 02-16 14:12:11.350 11542-11542/? I/MicroDetectionWorker: #startMicroDetector [speakerMode: 0] 02-16 14:12:11.350 11542-11542/? I/AudioController: Using mInputStreamFactoryBuilder Created new AudioSource 02-16 14:12:11.351 11542-11542/? I/MicroDetectionWorker: onReady 02-16 14:12:11.361 11542-17983/? I/MicroRecognitionRunner: Starting detection. 02-16 14:12:11.361 11542-18036/? I/MicrophoneInputStream: mic_starting SR : 16000 CC : 16 SO : 6 02-16 14:12:11.371 10867-20742/? I/AudioFlinger: AudioFlinger's thread 0xe1483400 tid=20742 ready to run 02-16 14:12:11.371 10867-16145/? E/AudioFlinger: not enough memory for AudioTrack size=131296 02-16 14:12:11.371 10867-16145/? D/MemoryDealer: AudioTrack (0xe5e9c7f0, size=4194304) 0: 0xe5e9ccc0 | 0x00000000 | 0x000200E0 | A 1: 0xe5e9ce50 | 0x000200E0 | 0x000200E0 | A 2: 0xe47be120 | 0x000401C0 | 0x000200E0 | A 3: 0xe47be360 | 0x000602A0 | 0x000200E0 | A 4: 0xe47be4b0 | 0x00080380 | 0x000200E0 | A 5: 0xe47be630 | 0x000A0460 | 0x000200E0 | A 6: 0xe47be7b0 | 0x000C0540 | 0x000200E0 | A 7: 0xe47be960 | 0x000E0620 | 0x000200E0 | A 8: 0xe47beae0 | 0x00100700 | 0x000200E0 | A 9: 0xe5e9ce20 | 0x001207E0 | 0x000200E0 | A 10: 0xe5b07330 | 0x001408C0 | 0x000200E0 | A 11: 0xe5b074d0 | 0x001609A0 | 0x000200E0 | A 12: 0xe5b07670 | 0x00180A80 | 0x000200E0 | A 13: 0xe5b077f0 | 0x001A0B60 | 0x000200E0 | A 14: 0xe5b079b0 | 0x001C0C40 | 0x000200E0 | A 15: 0xe5b07b10 | 0x001E0D20 | 0x000200E0 | A 16: 0xe5b07cf0 | 0x00200E00 | 0x000200E0 | A 17: 0xe47bef10 | 0x00220EE0 | 0x000200E0 | A 18: 0xe447f0a0 | 0x00240FC0 | 0x000200E0 | A 19: 0xe447f290 | 0x002610A0 | 0x000200E0 | A 02-16 14:12:11.371 10867-16145/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 02-16 14:12:11.372 11542-18036/? E/IAudioFlinger: createRecord returned error -12 02-16 14:12:11.372 11542-18036/? E/AudioRecord: AudioFlinger could not create record track, status: -12 02-16 14:12:11.373 11542-18036/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 02-16 14:12:11.373 11542-18036/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 02-16 14:12:11.373 11542-18036/? I/MicrophoneInputStream: mic_started SR : 16000 CC : 16 SO : 6 02-16 14:12:11.375 11542-11542/? I/MicroDetectionWorker: onReady 02-16 14:12:11.376 11542-18036/? I/MicrophoneInputStream: mic_close SR : 16000 CC : 16 SO : 6 02-16 14:12:11.377 11542-17983/? I/MicroRecognitionRunner: Detection finished 02-16 14:12:11.377 11542-17983/? W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream 02-16 14:12:11.378 11542-17983/? W/ErrorProcessor: onFatalError, processing error from engine(4) com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream at com.google.android.apps.gsa.staticplugins.microdetection.d.k.a(SourceFile:91) at com.google.android.apps.gsa.staticplugins.microdetection.d.l.run(Unknown Source:14) at com.google.android.libraries.gsa.runner.a.a.b(SourceFile:32) at com.google.android.libraries.gsa.runner.a.c.call(Unknown Source:4) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4) at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4) at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6) Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space. at com.google.android.apps.gsa.speech.audio.Tee.j(SourceFile:103) at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2) at java.io.InputStream.read(InputStream.java:101) at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:17) at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458) at java.util.concurrent.FutureTask.run(FutureTask.java:266)  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)  at java.util.concurrent.FutureTask.run(FutureTask.java:266)  at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)  at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)  at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)  at java.lang.Thread.run(Thread.java:764)  at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)  02-16 14:12:11.379 11542-17983/? I/AudioController: internalShutdown 02-16 14:12:11.379 11542-12096/? I/MicroRecognitionRunner: Stopping hotword detection. 02-16 14:12:11.380 11542-11542/? I/MicroDetector: Keeping mic open: false 02-16 14:12:11.380 11542-11542/? I/MicroDetectionWorker: #onError(false) 02-16 14:12:11.380 11542-20736/? I/DeviceStateChecker: DeviceStateChecker cancelled ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览