human: 如何处理七牛云的实时音视频QNLocalAudioTrack

gpt: [实时音视频](https://developer.qiniu.com/rtc) > API 文档 > Web >QNLocalAudioTrack

#  QNLocalAudioTrack

最近更新时间: 2022-07-14 14:52:30

本地音频 Track

Hierarchy

  * EventEmitter 
    * [QNTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9076/WebQNTrack)
      * [QNLocalTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9085/WebQNLocalTrack)
        * [QNLocalVideoTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9061/WebQNLocalVideoTrack)
          * [QNCameraVideoTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9068/WebQNCameraVideoTrack)
          * [QNScreenVideoTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9092/WebQNScreenVideoTrack)
          * [QNCanvasVideoTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/10320/WebQNCanvasVideoTrack)
          * [QNCustomVideoTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9088/WebQNCustomVideoTrack)
        * QNLocalAudioTrack 
          * [QNMicrophoneAudioTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9064/WebQNMicrophoneAudioTrack)
          * [QNBufferSourceAudioTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9069/WebQNBufferSourceAudioTrack)
          * [QNCustomAudioTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9091/WebQNCustomAudioTrack)
      * [QNRemoteTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9082/WebQNRemoteTrack)
        * [QNRemoteVideoTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9060/WebQNRemoteVideoTrack)
        * [QNRemoteAudioTrack](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9087/WebQNRemoteAudioTrack)

Index

Properties

  * trackID
  * userID
  * tag
  * mediaElement

Event

  * ended
  * audioFrame

Core Methods

  * play
  * isAudio
  * isVideo
  * isMuted
  * getMediaStreamTrack
  * setMuted
  * destroy
  * setVolume
  * setPlaybackDevice
  * getVolumeLevel
  * getCurrentTimeDomainData
  * getCurrentFrequencyDomainData
  * getStats

Properties

trackID

  * trackID?: string
  * Track ID

对于本地 Track，当且仅当 Track 发布后才可获取到有效的 TrackID；对于远端 Track，可随时获取到有效的 TrackID。

userID

  * userID?: string
  * 用户 ID

tag

  * tag?: string
  * 自定义 tag 文本

mediaElement

  * mediaElement: HTMLVideoElement | HTMLAudioElement | undefined
  * 当前播放元素，当调用 play 方法后，可以通过该属性获取播放的媒体元素

Event

ended

  * (): void
  * Track 断开
    
        track.on('ended', function() {
        console.log('ended')
    })

audioFrame

  * (buffer: AudioBuffer): void
  * 原始音频数据返回
    
        track.on('audioFrame', function(buffer) {
        for (let channel = 0; channel < buffer.numberOfChannels; channel += 1) {
            console.log('audioFrame', channel, currentChannelData);
        } 
    })

Parameters

    * buffer: AudioBuffer 

本地音频轨道的音频帧

Core Methods

play

  * public play(container: HTMLElement, config?: QNPlayerConfig): Promise<void>
  * 播放当前 track

Parameters

    * container: HTMLElement 

挂载的 html 容器，SDK 会在该容器下创建 audio 或 video 元素来播放 track

    * Optional config: [QNPlayerConfig](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/10141/WebQNPlayerConfig)

播放配置信息

Returns void

isAudio

  * isAudio(): boolean
  * 是否是音频

Returns boolean

isVideo

  * isVideo(): boolean
  * 是否是视频

Returns boolean

isMuted

  * isMuted(): boolean
  * 是否 mute（静音）

Returns boolean

getMediaStreamTrack

  * getMediaStreamTrack(): MediaStreamTrack | undefined
  * 获取浏览器原生的 MediaStreamTrack 对象

Returns MediaStreamTrack | undefined

setMuted

  * setMuted(isMuted: boolean): void
  * 设置是否静默该 track，静默后，SDK 将会发送静默帧数据

当静默状态改变后，远端订阅 track 将会收到 [mute-state-
changed](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9082) 事件。

Parameters

    * isMuted: boolean 

是否 mute

Returns void

destroy

  * destroy(): void
  * 销毁当前流

Returns void

setVolume

  * setVolume(volume: number): void
  * 设置音量

> 音量值在 0 - 1 之间为软件缩小；1 为原始音量；大于 1 且小于 100 为软件放大。
>
> 该设置可以用于适度对采集音量做放大或者缩小。

Parameters

    * volume: number 

音量值范围为 0 - 100，默认值为 1

Returns void

setPlaybackDevice

  * setPlaybackDevice(deviceId: string): Promise<void>
  * 设置音频输出设备

Parameters

    * deviceId: string 

设备 deviceId，可以从
[QNRTC.getPlaybackDevices](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9070)
接口中获取

Returns Promise<void>

getVolumeLevel

  * getVolumeLevel(): string ｜ undefined
  * 获取当前音量

> 可通过定时调用该方法获取音量值的方式来判断当前用户是否在讲话。
>
> 在安静的环境下，获取到大于 0 的数值为预期现象，您可根据您的需求自行决定判断的阈值。

Returns string | undefined

getCurrentTimeDomainData

  * getCurrentTimeDomainData(): Uint8Array | undefined
  * 获取当前时域数据

Returns Uint8Array | undefined

getCurrentFrequencyDomainData

  * getCurrentFrequencyDomainData(): Uint8Array | undefined
  * 获取当前频域数据

Returns Uint8Array | undefined

getStats

  * getStats(): [QNLocalAudioTrackStats](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9083/WebQNLocalAudioTrackStats)
  * 获取当前频域数据

Returns
[QNLocalAudioTrackStats](https://developer.qiniu.com/rtc/9089/WebQNLocalAudioTrack/9083/WebQNLocalAudioTrackStats)

以上内容是否对您有帮助？