您的位置:首页 > 移动开发 > Android开发

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

简介

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一篇翻译博客官方文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: