W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
音頻播放的主要工作是將音頻數(shù)據(jù)轉(zhuǎn)碼為可聽見的音頻模擬信號并通過輸出設(shè)備進(jìn)行播放,同時對播放任務(wù)進(jìn)行管理。
接口名 | 描述 |
---|---|
AudioRenderer(AudioRendererInfo audioRendererInfo, PlayMode pm) throws IllegalArgumentException | 構(gòu)造函數(shù),設(shè)置播放相關(guān)音頻參數(shù)和播放模式,使用默認(rèn)播放設(shè)備。 |
AudioRenderer(AudioRendererInfo audioRendererInfo, PlayMode pm, AudioDeviceDescriptor outputDevice) throws IllegalArgumentException | 構(gòu)造函數(shù),設(shè)置播放相關(guān)音頻參數(shù)、播放模式和播放設(shè)備。 |
boolean start() | 播放音頻流。 |
boolean write(byte[] data, int offset, int size) | 將音頻數(shù)據(jù)以 byte 流寫入音頻接收器以進(jìn)行播放。 |
boolean write(short[] data, int offset, int size) | 將音頻數(shù)據(jù)以 short 流寫入音頻接收器以進(jìn)行播放。 |
boolean write(float[] data, int offset, int size) | 將音頻數(shù)據(jù)以 float 流寫入音頻接收器以進(jìn)行播放。 |
boolean write(java.nio.ByteBuffer data, int size) | 將音頻數(shù)據(jù)以 ByteBuffer 流寫入音頻接收器以進(jìn)行播放。 |
boolean pause() | 暫停播放音頻流。 |
boolean stop() | 停止播放音頻流。 |
boolean release() | 釋放播放資源。 |
AudioDeviceDescriptor getCurrentDevice() | 獲取當(dāng)前工作的音頻播放設(shè)備。 |
boolean setPlaybackSpeed(float speed) | 設(shè)置播放速度。 |
boolean setPlaybackSpeed(AudioRenderer.SpeedPara speedPara) | 設(shè)置播放速度與音調(diào)。 |
boolean setVolume(ChannelVolume channelVolume) | 設(shè)置指定聲道上的輸出音量。 |
boolean setVolume(float vol) | 設(shè)置所有聲道上的輸出音量。 |
static int getMinBufferSize(int sampleRate, AudioStreamInfo.EncodingFormat format, AudioStreamInfo.ChannelMask channelMask) | 獲取Stream播放模式所需的buffer大小。 |
State getState() | 獲取音頻播放的狀態(tài)。 |
int getRendererSessionId() | 獲取音頻播放的 session ID。 |
int getSampleRate() | 獲取采樣率。 |
int getPosition() | 獲取音頻播放的幀數(shù)位置。 |
boolean setPosition(int position) | 設(shè)置起始播放幀位置。 |
AudioRendererInfo getRendererInfo() | 獲取音頻渲染信息。 |
boolean duckVolume() | 降低音量并將音頻與另一個擁有音頻焦點(diǎn)的應(yīng)用程序混合。 |
boolean unduckVolume() | 恢復(fù)音量。 |
SpeedPara getPlaybackSpeed() | 獲取播放速度、音調(diào)參數(shù)。 |
boolean setSpeed(SpeedPara speedPara) | 設(shè)置播放速度、音調(diào)參數(shù)。 |
Timestamp getAudioTime() | 獲取播放時間戳信息。 |
boolean flush() | 刷新當(dāng)前的播放流數(shù)據(jù)隊(duì)列。 |
static float getMaxVolume() | 獲取播放流可設(shè)置的最大音量。 |
static float getMinVolume() | 獲取播放流可設(shè)置的最小音量。 |
StreamType getStreamType() | 獲取播放流的音頻流類型。 |
AudioStreamInfo audioStreamInfo = new AudioStreamInfo.Builder().sampleRate(
AudioStreamInfo.SAMPLE_RATE_UNSPECIFIED)
.audioStreamFlag(AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_NONE)
.encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_INVALID)
.channelMask(AudioStreamInfo.ChannelMask.CHANNEL_INVALID)
.streamUsage(AudioStreamInfo.StreamUsage.STREAM_USAGE_UNKNOWN)
.build();
以真實(shí)的播放pcm流為例:
AudioStreamInfo audioStreamInfo = new AudioStreamInfo.Builder().sampleRate(44100) // 44.1kHz
.audioStreamFlag(AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_MAY_DUCK) // 混音
.encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT) // 16-bit PCM
.channelMask(AudioStreamInfo.ChannelMask.CHANNEL_OUT_STEREO) // 雙聲道
.streamUsage(AudioStreamInfo.StreamUsage.STREAM_USAGE_MEDIA) // 媒體類音頻
.build();
AudioRendererInfo audioRendererInfo = new AudioRendererInfo.Builder().audioStreamInfo(audioStreamInfo)
.audioStreamOutputFlag(AudioRendererInfo.AudioStreamOutputFlag.AUDIO_STREAM_OUTPUT_FLAG_NONE)
.bufferSizeInBytes(0)
.distributedDeviceId("")
.isOffload(false)
.sessionID(AudioRendererInfo.SESSION_ID_UNSPECIFIED)
.build();
以真實(shí)的播放pcm流為例:
AudioRendererInfo audioRendererInfo = new AudioRendererInfo.Builder().audioStreamInfo(audioStreamInfo)
.audioStreamOutputFlag(AudioRendererInfo.AudioStreamOutputFlag.AUDIO_STREAM_OUTPUT_FLAG_DIRECT_PCM) // pcm格式的輸出流
.bufferSizeInBytes(100)
.distributedDeviceId("E54***5E8") // 使用分布式設(shè)備E54***5E8播放
.isOffload(false) // false表示分段傳輸buffer并播放,true表示整個音頻流一次性傳輸?shù)紿AL層播放
.build();
AudioInterrupt audioInterrupt = new AudioInterrupt();
AudioManager audioManager = new AudioManager();
audioInterrupt.setStreamInfo(streamInfo);
audioInterrupt.setInterruptListener(new AudioInterrupt.InterruptListener() {
@Override
public void onInterrupt(int type, int hint) {
if (type == AudioInterrupt.INTERRUPT_TYPE_BEGIN
&& hint == AudioInterrupt.INTERRUPT_HINT_PAUSE) {
renderer.pause();
} else if (type == AudioInterrupt.INTERRUPT_TYPE_BEGIN
&& hint == AudioInterrupt.INTERRUPT_HINT_NONE) {
} else if (type == AudioInterrupt.INTERRUPT_TYPE_END && (
hint == AudioInterrupt.INTERRUPT_HINT_NONE
|| hint == AudioInterrupt.INTERRUPT_HINT_RESUME)) {
renderer.play();
} else {
}
}
});
audioManager.activateAudioInterrupt(audioInterrupt);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: