新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS音頻采集開(kāi)發(fā)指導(dǎo)
場(chǎng)景介紹

海拉爾網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站成立與2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
音頻采集的主要工作是通過(guò)輸入設(shè)備將聲音采集并轉(zhuǎn)碼為音頻數(shù)據(jù),同時(shí)對(duì)采集任務(wù)進(jìn)行管理。
接口說(shuō)明
| 接口名 | 描述 |
|---|---|
| AudioCapturer(AudioCapturerInfo audioCapturerInfo) throws IllegalArgumentException | 構(gòu)造函數(shù),設(shè)置錄音相關(guān)音頻參數(shù),使用默認(rèn)錄音設(shè)備。 |
| AudioCapturer(AudioCapturerInfo audioCapturerInfo, AudioDeviceDescriptor devInfo) throws IllegalArgumentException | 構(gòu)造函數(shù),設(shè)置錄音相關(guān)音頻參數(shù)并指定錄音設(shè)備。 |
| static int getMinBufferSize(int sampleRate, int channelCount, int audioFormat) | 獲取指定參數(shù)條件下所需的最小緩沖區(qū)大小。 |
| boolean addSoundEffect(UUID type, String packageName) | 增加錄音的音頻音效。 |
| boolean start() | 開(kāi)始錄音。 |
| int read(byte[] data, int offset, int size) | 讀取音頻數(shù)據(jù)。 |
| int read(byte[] data, int offset, int size, boolean isBlocking) | 讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 byte 數(shù)組中。 |
| int read(float[] data, int offsetInFloats, int sizeInFloats) | 阻塞式讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 float 數(shù)組中。 |
| int read(float[] data, int offsetInFloats, int sizeInFloats, boolean isBlocking) | 讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 float 數(shù)組中。 |
| int read(short[] data, int offsetInShorts, int sizeInShorts) | 阻塞式讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 short 數(shù)組中。 |
| int read(short[] data, int offsetInShorts, int sizeInShorts, boolean isBlocking) | 讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 short 數(shù)組中。 |
| int read(java.nio.ByteBuffer buffer, int sizeInBytes) | 阻塞式讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 ByteBuffer 對(duì)象中。 |
| int read(java.nio.ByteBuffer buffer, int sizeInBytes, boolean isBlocking) | 讀取音頻數(shù)據(jù)并寫(xiě)入傳入的 ByteBuffer 對(duì)象中。 |
| boolean stop() | 停止錄音。 |
| boolean release() | 釋放錄音資源。 |
| AudioDeviceDescriptor getSelectedDevice() | 獲取輸入設(shè)備信息。 |
| AudioDeviceDescriptor getCurrentDevice() | 獲取當(dāng)前正在錄制音頻的設(shè)備信息。 |
| int getCapturerSessionId() | 獲取錄音的 session ID。 |
| Set | 獲取已經(jīng)激活的音頻音效列表。 |
| AudioCapturer.State getState() | 獲取音頻采集狀態(tài)。 |
| int getSampleRate() | 獲取采樣率。 |
| int getAudioInputSource() | 獲取錄音的輸入設(shè)備信息。 |
| int getBufferFrameCount() | 獲取以幀為單位的緩沖區(qū)大小。 |
| int getChannelCount() | 獲取音頻采集通道數(shù)。 |
| AudioStreamInfo.EncodingFormat getEncodingFormat() | 獲取音頻采集的音頻編碼格式。 |
| boolean getAudioTime(Timestamp timestamp, Timestamp.Timebase timebase) | 獲取一個(gè)即時(shí)的捕獲時(shí)間戳。 |
開(kāi)發(fā)步驟
- 構(gòu)造音頻流參數(shù)的數(shù)據(jù)結(jié)構(gòu) AudioStreamInfo,推薦使用 AudioStreamInfo.Builder 類(lèi)來(lái)構(gòu)造,模板如下,模板中設(shè)置的均為 AudioStreamInfo.Builder 類(lèi)的默認(rèn)值,根據(jù)音頻流的具體規(guī)格來(lái)設(shè)置具體參數(shù)。
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();
- (可選)通過(guò)采集的采樣率、聲道數(shù)和數(shù)據(jù)格式,調(diào)用 getMinBufferSize 方法獲取采集任務(wù)所需的最小 buffer,參照該 buffer 值設(shè)置步驟3中 AudioCapturerInfo的bufferSizeInBytes。
- 使用步驟 1 創(chuàng)建的音頻流構(gòu)建音頻播放的參數(shù)結(jié)構(gòu) AudioCapturerInfo,推薦使用 AudioCapturerInfo.Builder類(lèi)來(lái)構(gòu)造,根據(jù)音頻采集的具體規(guī)格來(lái)設(shè)置具體參數(shù)。以真實(shí)的錄制 pcm 流為例:
AudioStreamInfo audioStreamInfo = new AudioStreamInfo.Builder().encodingFormat(
AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT) // 16-bit PCM
.channelMask(AudioStreamInfo.ChannelMask.CHANNEL_IN_STEREO) // 雙聲道
.sampleRate(44100) // 44.1kHz
.build();
AudioCapturerInfo audioCapturerInfo = new AudioCapturerInfo.Builder().audioStreamInfo(audioStreamInfo)
.build();
- (可選)設(shè)置采集設(shè)備,如麥克風(fēng)、耳機(jī)等。通過(guò) AudioManager.getDevices(AudioDeviceDescriptor.DeviceFlag.INPUT_DEVICES_FLAG) 獲取到設(shè)備支持的輸入設(shè)備,然后依照 AudioDeviceDescriptor.DeviceType 選擇要選用的輸入設(shè)備類(lèi)型。
- 通過(guò)構(gòu)造方法獲取 AudioCapturer 類(lèi)的實(shí)例化對(duì)象,其中步驟 3 的參數(shù)為必選參數(shù),通過(guò)步驟 4 獲取的指定錄音設(shè)備為可選參數(shù)。
- (可選)設(shè)置采集音效,如降噪、回聲消除等。使用 addSoundEffect(UUID type, String packageName)進(jìn)行音效設(shè)置,其中 UUID 參考類(lèi) SoundEffect 中提供的靜態(tài)變量。
- (可選)構(gòu)造音頻采集回調(diào),首先繼承抽象類(lèi) AudioCapturerCallback,并實(shí)現(xiàn)抽象方法 onCapturerConfigChanged(List
configs),然后調(diào)用 AudioManager 類(lèi)的 registerAudioCapturerCallback(AudioCapturerCallback cb )方法進(jìn)行音頻采集回調(diào)注冊(cè)。代碼示例如下:
private AudioManager audioManager = new AudioManager();
public void main() {
AudioCapturerCallback cb = new AudioCapturerCallback() {
@Override
public void onCapturerConfigChanged(List configs) {
configs.forEach(config -> doSomething(config));
}
};
audioManager.registerAudioCapturerCallback(cb);
}
private void doSomething(AudioCapturerConfig config) {
...
}
- 調(diào)用 AudioCapturer 實(shí)例化對(duì)象的 start() 方法啟動(dòng)采集任務(wù)。
- 采集的音頻數(shù)據(jù)讀取為 byte 流,循環(huán)調(diào)用 read 方法進(jìn)行數(shù)據(jù)讀取。
- 調(diào)用 AudioCapturer 實(shí)例化對(duì)象的 stop() 方法停止采集。
- 播放任務(wù)結(jié)束后,調(diào)用 AudioCapturer 實(shí)例化對(duì)象的release() 釋放資源。
網(wǎng)頁(yè)名稱(chēng):創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS音頻采集開(kāi)發(fā)指導(dǎo)
分享路徑:http://www.dlmjj.cn/article/dhoohgd.html


咨詢(xún)
建站咨詢(xún)
