Android音视频-音频播放
2017-11-09 17:55
471 查看
在前面我们了解了音频的录制,录制了以后当然要可以播放了,下面记录一些Android中播放音频的几种方式。
AudioTrack
接近底层,提供强大的控制能力,支持低延迟播放,流媒体播放等。AudioTrack只能播放已经解码的PCM流,如果是文件的话只支持wav格式的音频文件,因为wav格式的音频文件大部分都是PCM流。AudioTrack不创建解码器,所以只能播放不需要解码的wav文件和AudioRecord配套服用。
MediaPlayer
适合在后台长时间播放本地音乐或者在线的流式资源,MediaPlayer可以播放多种格式的声音文件,例如MP3,AAC,WAV,OGG,MIDI等。MediaPlayer会在framework层创建对应的音频解码器。MediaPlayer在framework层还是会创建AudioTrack,把解码后的PCM数流传递给AudioTrack,AudioTrack再传递给AudioFlinger进行混音,然后才传递给硬件播放,所以是MediaPlayer包含了AudioTrack。和MediaRecorder配套使用。
SoundPool
适合播放短音频片段,游戏声音,铃声,按键音等等,可以同时播放多个音频。
ExoPlayer
最近看到的一个Google出来的开源播放器也可以播放语音和视频。
我们从AudioTrack的构造方法开始看:
上面的参数有几个是我们在音频采集里面介绍过了的,我们看看不清楚的构造参数。
streamType
这个参数代表着当前应用使用的哪一种音频管理策略,当系统有多个进程需要播放音频时,这个管理策略会决定最终的展现效果,该参数的可选的值以常量的形式定义在 AudioManager 类中,主要包括:
STREAM_VOCIE_CALL:电话声音
STREAM_SYSTEM:系统声音
STREAM_RING:铃声
STREAM_MUSCI:音乐声
STREAM_ALARM:警告声
STREAM_NOTIFICATION:通知声
mode
static
静态的数据,一次性交付给AudioTrack处理的。好处是简单高效,只需要一次操作完成数据的传递;缺点是不能播放较大的音频,通常播放一些小的例如铃声系统提示等消耗内存小的。
streaming
流模式播放数据,数据按照一定的规律不断的传递给接收方的。理论上可以用于播放任何音频场景,通常在如下采用:
大音频文件;
音频属性要求高,例如采样率高,深度大的数据;
音频数据是实时产生的,这种情况只能用流模式来播放了;
配置AudioTrack参数,初始化内部的音频播放缓冲区
开始播放
开启一个子线程不断的向AudioTrack的缓冲区写入音频数据。这个过程要及时,否则就会出现“underrun”的错误
停止播放,释放资源
示例代码如下:
点击查看
注意:
当要播放网络资源的时候要添加INTERNET权限
当应用在后台的时候或者熄屏的时候要保持CPU运转我们的MediaPlayer播放器,要添加WAKE_LOCK权限,并且调用MediaPlayer.setScreenOnWhilePlaying() 或者MediaPlayer.setWakeMode()方法。
创建MediaPlayer
调用方法setDataSource设置装载文件
准备播放
开始播放
播放的资源类型有很多种,具体看代码示例:
点击查看
点击查看
官方说明文档
GitHub地址
csdn一篇翻译博客官方文档
AudioTrack
接近底层,提供强大的控制能力,支持低延迟播放,流媒体播放等。AudioTrack只能播放已经解码的PCM流,如果是文件的话只支持wav格式的音频文件,因为wav格式的音频文件大部分都是PCM流。AudioTrack不创建解码器,所以只能播放不需要解码的wav文件和AudioRecord配套服用。
MediaPlayer
适合在后台长时间播放本地音乐或者在线的流式资源,MediaPlayer可以播放多种格式的声音文件,例如MP3,AAC,WAV,OGG,MIDI等。MediaPlayer会在framework层创建对应的音频解码器。MediaPlayer在framework层还是会创建AudioTrack,把解码后的PCM数流传递给AudioTrack,AudioTrack再传递给AudioFlinger进行混音,然后才传递给硬件播放,所以是MediaPlayer包含了AudioTrack。和MediaRecorder配套使用。
SoundPool
适合播放短音频片段,游戏声音,铃声,按键音等等,可以同时播放多个音频。
ExoPlayer
最近看到的一个Google出来的开源播放器也可以播放语音和视频。
AudioTrack
简介
AudioTrack管理和播放简单的音频资源。用于PCM音频流的播放,使用它的write方法把数据推到AudioTrack上面播放。在数据传送上有两种方式:static和streaming两种方式,下面的构造方法中有介绍我们从AudioTrack的构造方法开始看:
/** * Class constructor. * @param streamType the type of the audio stream. See * {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM}, * {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC}, * {@link AudioManager#STREAM_ALARM}, and {@link AudioManager#STREAM_NOTIFICATION}. * @param sampleRateInHz the initial source sample rate expressed in Hz. * {@link AudioFormat#SAMPLE_RATE_UNSPECIFIED} means to use a route-dependent value * which is usually the sample rate of the sink. * {@link #getSampleRate()} can be used to retrieve the actual sample rate chosen. * @param channelConfig describes the configuration of the audio channels. * See {@link AudioFormat#CHANNEL_OUT_MONO} and * {@link AudioFormat#CHANNEL_OUT_STEREO} * @param audioFormat the format in which the audio data is represented. * See {@link AudioFormat#ENCODING_PCM_16BIT}, * {@link AudioFormat#ENCODING_PCM_8BIT}, * and {@link AudioFormat#ENCODING_PCM_FLOAT}. * @param bufferSizeInBytes the total size (in bytes) of the internal buffer where audio data is * read from for playback. This should be a nonzero multiple of the frame size in bytes. * <p> If the track's creation mode is {@link #MODE_STATIC}, * this is the maximum length sample, or audio clip, that can be played by this instance. * <p> If the track's creation mode is {@link #MODE_STREAM}, * this should be the desired buffer size * for the <code>AudioTrack</code> to satisfy the application's * latency requirements. * If <code>bufferSizeInBytes</code> is less than the * minimum buffer size for the output sink, it is increased to the minimum * buffer size. * The method {@link #getBufferSizeInFrames()} returns the * actual size in frames of the buffer created, which * determines the minimum frequency to write * to the streaming <code>AudioTrack</code> to avoid underrun. * See {@link #getMinBufferSize(int, int, int)} to determine the estimated minimum buffer size * for an AudioTrack instance in streaming mode. * @param mode streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM} * @throws java.lang.IllegalArgumentException * @deprecated use {@link Builder} or * {@link #AudioTrack(AudioAttributes, AudioFormat, int, int, int)} to specify the * {@link AudioAttributes} instead of the stream type which is only for volume control. */ public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, int mode)
上面的参数有几个是我们在音频采集里面介绍过了的,我们看看不清楚的构造参数。
streamType
这个参数代表着当前应用使用的哪一种音频管理策略,当系统有多个进程需要播放音频时,这个管理策略会决定最终的展现效果,该参数的可选的值以常量的形式定义在 AudioManager 类中,主要包括:
STREAM_VOCIE_CALL:电话声音
STREAM_SYSTEM:系统声音
STREAM_RING:铃声
STREAM_MUSCI:音乐声
STREAM_ALARM:警告声
STREAM_NOTIFICATION:通知声
mode
static
静态的数据,一次性交付给AudioTrack处理的。好处是简单高效,只需要一次操作完成数据的传递;缺点是不能播放较大的音频,通常播放一些小的例如铃声系统提示等消耗内存小的。
streaming
流模式播放数据,数据按照一定的规律不断的传递给接收方的。理论上可以用于播放任何音频场景,通常在如下采用:
大音频文件;
音频属性要求高,例如采样率高,深度大的数据;
音频数据是实时产生的,这种情况只能用流模式来播放了;
使用AudioTrack
使用AudioTrack类步骤如下:配置AudioTrack参数,初始化内部的音频播放缓冲区
开始播放
开启一个子线程不断的向AudioTrack的缓冲区写入音频数据。这个过程要及时,否则就会出现“underrun”的错误
停止播放,释放资源
示例代码如下:
点击查看
MediaPlayer
简介
MediaPlayer是Android多媒体框架中用来播放音频或者视频的一个API,可以播放本地的资源,也可以设置网页URL来播放网络上面的资源文件。注意:
当要播放网络资源的时候要添加INTERNET权限
当应用在后台的时候或者熄屏的时候要保持CPU运转我们的MediaPlayer播放器,要添加WAKE_LOCK权限,并且调用MediaPlayer.setScreenOnWhilePlaying() 或者MediaPlayer.setWakeMode()方法。
使用MediaPlayer
MediaPlayer使用步骤:创建MediaPlayer
调用方法setDataSource设置装载文件
准备播放
开始播放
播放的资源类型有很多种,具体看代码示例:
点击查看
SoundPool
这个用于播放短的音频文件,对播放文件大小有限制。可以批量的播放音频文件,启动相比MediaPlayer低。放一个在项目中使用来进行拍照按钮声音的工具类点击查看
ExoPlayer
Google出的一款比MediaPlayer还好用的播放器,没有细细研究,列一些资料以后有时间或者需求看。官方说明文档
GitHub地址
csdn一篇翻译博客官方文档
相关文章推荐
- Android中的多媒体音频和视频播放
- Android的多媒体技术——MediaPlayer实现音频与视频的播放
- android-MediaPlayer的基本使用-播放音频视频
- Android 音频,视频资源管理与播放
- Android 播放音频和视频
- Android学习记录--音频视频播放
- 在Android中播放音频和视频--值得学习
- android之MediaPlayer播放音频或者视频文件
- 如何让音频跟视频在ios跟android上自动播放
- android 学习笔记 播放音频 和视频
- Android基础入门教程——9.2 MediaPlayer播放音频与视频
- Android多媒体---SurfaceView(播放视频)+Mediaplayer(音频)
- Android使用MediaPlayer只播放视频不播放音频的方法
- Android中关于assets和raw播放音频视频的实践
- Android 音视频开发(三):使用 AudioTrack 播放PCM音频
- 在Android中播放音频和视频
- Android 多媒体——音频播放录制、视频播放、相机
- Android开发之MediaPlayer多媒体(音频,视频)播放工具类
- android webview——webview播放音频/视频的问题
- 在Android中播放音频和视频