使用AudioTrack进行音频播放
2015-06-11 11:25
549 查看
前一段时间一直在研究Android上面的媒体播放器MediaPlayer,不巧的是发现MediaPlayer的不同版本对于网络上的mp3流支持不是很好,于是就下载了网上的Java开源mp3解码播放源码,然后包装了一下之后发现不知道如何在Android系统上进行播放解码出来的音频数据,因此在网上找了大量的相关资料后,发现在Android系统中有一个AudioTrack类,该类可是实现将音频数据输出到音频设备中。
该类的SDK文档是如下描述的:
android.media.AudioTrack.AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int
bufferSizeInBytes, int mode) throws IllegalArgumentException
public AudioTrack (int streamType,
Since: API Level 3
Class constructor.
Parameters
Throws
使用这个类可以很强轻松地将音频数据在Android系统上播放出来,下面贴出我自己写的源码:
AudioTrack
audio = new AudioTrack(
AudioManager.STREAM_MUSIC,
// 指定在流的类型
32000,
// 设置音频数据的采样率 32k,如果是44.1k就是44100
AudioFormat.CHANNEL_OUT_STEREO,
// 设置输出声道为双声道立体声,而CHANNEL_OUT_MONO类型是单声道
AudioFormat.ENCODING_PCM_16BIT,
// 设置音频数据块是8位还是16位,这里设置为16位。好像现在绝大多数的音频都是16位的了
AudioTrack.MODE_STREAM
// 设置模式类型,在这里设置为流类型,另外一种MODE_STATIC貌似没有什么效果
);
audio.play();
// 启动音频设备,下面就可以真正开始音频数据的播放了
//
打开mp3文件,读取数据,解码等操作省略 ...
byte[]
buffer = new buffer[4096];
int
count;
while(true)
{
//
最关键的是将解码后的数据,从缓冲区写入到AudioTrack对象中
audio.write(buffer,
0, 4096);
if(文件结束)
break;
}
//
最后别忘了关闭并释放资源
audio.stop();
audio.release();
该类的SDK文档是如下描述的:
android.media.AudioTrack.AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int
bufferSizeInBytes, int mode) throws IllegalArgumentException
public AudioTrack (int streamType,
int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, int mode)
Since: API Level 3Class constructor.
Parameters
streamType | the type of the audio stream. See STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING, STREAM_MUSICand STREAM_ALARM |
---|---|
sampleRateInHz | the sample rate expressed in Hertz. Examples of rates are (but not limited to) 44100, 22050 and 11025. |
channelConfig | describes the configuration of the audio channels. See CHANNEL_OUT_MONOand CHANNEL_OUT_STEREO |
audioFormat | the format in which the audio data is represented. See ENCODING_PCM_16BITand ENCODING_PCM_8BIT |
bufferSizeInBytes | the total size (in bytes) of the buffer where audio data is read from for playback. If using the AudioTrack in streaming mode, you can write data into this buffer in smaller chunks than this size. If using the AudioTrack in static mode, this is the maximum size of the sound that will be played for this instance. See getMinBufferSize(int, int, int)to determine the minimum required buffer size for the successful creation of an AudioTrack instance in streaming mode. Using values smaller than getMinBufferSize() will result in an initialization failure. |
mode | streaming or static buffer. See MODE_STATICand MODE_STREAM |
IllegalArgumentException |
---|
AudioTrack
audio = new AudioTrack(
AudioManager.STREAM_MUSIC,
// 指定在流的类型
32000,
// 设置音频数据的采样率 32k,如果是44.1k就是44100
AudioFormat.CHANNEL_OUT_STEREO,
// 设置输出声道为双声道立体声,而CHANNEL_OUT_MONO类型是单声道
AudioFormat.ENCODING_PCM_16BIT,
// 设置音频数据块是8位还是16位,这里设置为16位。好像现在绝大多数的音频都是16位的了
AudioTrack.MODE_STREAM
// 设置模式类型,在这里设置为流类型,另外一种MODE_STATIC貌似没有什么效果
);
audio.play();
// 启动音频设备,下面就可以真正开始音频数据的播放了
//
打开mp3文件,读取数据,解码等操作省略 ...
byte[]
buffer = new buffer[4096];
int
count;
while(true)
{
//
最关键的是将解码后的数据,从缓冲区写入到AudioTrack对象中
audio.write(buffer,
0, 4096);
if(文件结束)
break;
}
//
最后别忘了关闭并释放资源
audio.stop();
audio.release();
相关文章推荐
- 物联网之智能家居
- 拥抱 IoT 从这里开始
- iotop
- 物联网云平台调试手记
- 物联网,后会无期?
- VisionMobile:2015 IOT大趋势(三)数据是新通货
- 物联网技术上面临的基本问题和操作系统设计
- Go中iota是什么
- 华为推出物联网系统LiteOS
- Android Audio代码分析1 - AudioTrack使用示例
- android物联网初步,利用手机蓝牙与单片机通信,实现led灯开关和定时
- 物联网革命的核心是“网”,而不是“物”
- 物联网+ZigBee系统知识简介
- 树莓派 2 win 10 IOT
- 基于进程的资源监控系列(二)--iotop
- 【译】VisionMobile:2015 IOT大趋势(二)摆脱物联网
- 【译】VisionMobile:2015 IOT大趋势(一)简介
- 开启物联网时代
- 浅谈对物联网的理解(转载清注明出处,会根据完成进度实时更新)
- 物联网领域几个协议的比较(来自于open mobile Allionce)