﻿# nf-audio {#audio}

Custom audio player with seeking, segment playback, speed, volume and replaceable controls.

Import the components below; see the package README for standalone installation.

## Basic usage {#example-1}

### Basic usage
```vue
<script setup lang="ts">
import { NfButton, NfAudio } from '@94ai-plus/common-ui'
import { ElDivider } from 'element-plus'

   import { ref  } from 'vue'
   const betweenStart = ref(0.3)
   const betweenEnd = ref(1.6)
   const audioRef = ref()
   const url = 'https://front.94ai.com/xccjh-images/%E5%B0%8F%E4%B8%83-%E5%A5%B3-%E5%93%88%E5%93%88%E5%93%88%E5%93%88%E5%93%88%E5%93%88%E5%93%88%E5%93%88.wav'
   const jumpToTime = 0.5
   const backSecond = 0.3
   const forwardSecond = 0.6
  function download(){
    result.value = 'Download request received'

  }
  function betweenPlay() {
      audioRef.value.betweenPlay()
  }
  const jumpAudioRef = ref(null)
  function justLocated() {
    jumpAudioRef.value?.jumpToTime(0.5, false)
  }

const result = ref('')
</script>

<template>
<div class="audio-examples">
  <div>
    <el-divider>Regular version</el-divider>
    <div style='display:flex;align-items:center;flex-wrap:wrap;gap:12px'>
       <nf-button @click='betweenPlay' style='margin-right: 10px'>interval play</nf-button>
       <nf-audio
          debuglog
          ref='audioRef'
          :src='url'
          @download='download'
          :jump-time='jumpToTime'
          :back-second='backSecond'
          :forward-second='forwardSecond'
          :between-start='betweenStart'
          :between-end='betweenEnd'
      />
    </div>
    <br />
    <el-divider>Only jumps but does not play automatically</el-divider>
    <div style='display:flex;align-items:center;flex-wrap:wrap;gap:12px'>
      <nf-button @click='justLocated' style='margin-right: 10px'>Jump</nf-button>
      <nf-audio
          ref='jumpAudioRef'
          :src='url'
      />
    </div>
    <el-divider>Simple version</el-divider>
    <nf-audio
        :src='url'
        @download='download'
        :show-more-btn='false'
    />
    <el-divider>no background</el-divider>
     <nf-audio
        :background='false'
        size="small"
        :src='url'
        :show-more-btn='false'
    />
    <br />
     <nf-audio
        :background='false'
        :src='url'
        disabled
        size="small"
        @download='download'
        :jump-time='jumpToTime'
        :back-second='backSecond'
        :forward-second='forwardSecond'
    />
    <el-divider>Disable</el-divider>
    <nf-audio
        disabled
        :src='url'
        :back-second='backSecond'
        :forward-second='forwardSecond'
        :jump-time='jumpToTime'
    />
    <br />
     <nf-audio
        disabled
        :src='url'
        :show-more-btn='false'
    />
     <el-divider>small size</el-divider>
     <nf-audio
        size="small"
        disabled
        :src='url'
        :show-more-btn='false'
    />
    <br />
     <nf-audio
        :src='url'
        size="small"
        @download='download'
        :jump-time='jumpToTime'
        :back-second='backSecond'
        :forward-second='forwardSecond'
        :download-enabled='false'
    />
  </div>
<output>{{ result }}</output></div>
</template>

<style scoped>
.audio-examples {max-width:100%} .audio-examples .nf-audio {max-width:100%}
</style>
```

## Attribute {#props}

| parameters | Description | Type | Optional value | Default value |
| --- | --- | --- | --- | --- |
| src | audio recording file | string | — | '' |
| size | Size | string | mini | undefined |
| disabled | Whether to disable | boolean | — | false |
| background | Whether to display the background; false to remove the background. | boolean | — | true |
| jump-time | Specify frame skip time | number | — | 0 |
| show-jump-time | Turn off frame skipping | number（unit seconds) | — | true |
| back-second | Specify the number of seconds to rewind, 0 turns off the rewind function | number | — | 0 |
| forward-second | Specify the number of seconds to fast forward, 0 turns off the fast forward function | number | — | 0 |
| init-volumn-value | Specify the default volume, ranging from 0 to 1 | number / string | — | 0.8 |
| init-speed-value | Specify default playback speed | number / string | — | 1 |
| multiple-array | Double speed option | Array | — | ['1.0', '1.25', '1.5', '1.75', '2.0', '2.5', '3.0', '3.5', '4.0', '10.0'] |
| show-more-btn | Pass false to turn off double speed and downloading | boolean | — | true |
| speed-desc | Multiplication function description | string | — | undefined |
| download-desc | Download function description | string | — | undefined |
| speed-pre-desc | Speed option description prefix, default such as 1x, 2x | string | — | 'x ' |
| cross-time-tip | The start time cannot be greater than the total duration | string | — | undefined |
| unload-tip | Audio resources cannot be played at the moment | string | — | undefined |
| between-start | Interval playback start time, in seconds. | number | — | 0 |
| between-end | Interval playback end time, unit second; 0 means playback to the end. | number | — | 0 |
| high-precision | Turn on AudioWorklet auxiliary timing, which requires security context; falls back to RAF/timeupdate when unavailable, and does not guarantee a fixed error. | boolean | — | false |
| debuglog | Output fallback logs when a high-precision clock is unavailable. | boolean | — | false |
| download-enabled | Whether to enable downloading functions (including copying audio addresses and opening new tabs) | boolean | — | true |
| copySuccessDesc | Prompt for successful copying of audio address. | string | — | Follow language pack |
| copyAddressDesc | Copy the menu text of the audio address. | string | — | Follow language pack |
| openWindowDesc | Menu text for new tab opening. | string | — | Follow language pack |

## Event {#events}

| event name | Description | callback parameters |
| --- | --- | --- |
| download | Download callback | e:Event |
| volumeChange | Volume change | volume: number（0…100） |
| speedChange | speed change | speed: speed of change |
| jumpToTime | frame skipping | (time: time to jump to,autoPlay:Whether to play automatically Boolean Defaulttrue) |
| updateProgress | Positioning progress | offsetX: Positioning offset |
| pauseOrPlay | play/pause | paused: Whether to pause |
| forward | fast forward | time: Fast forward to time |
| back | Rewind | time: Fast rewind to time |
| error | Resource loading error | error: Event / Error |
| canPlay | Resource loaded successfully | totalTime: string（mm:ss） |
| updateTime | Native timeupdate event. | event: Event |
| native media events | Transparently transmit native events such as play, pause, ended, loadedmetadata, canplay, seeking, seeked, volumechange, ratechange, etc. | event: Event |

## Slot {#slots}

| Name | Description |
| --- | --- |
| jump | Customized frame skipping |
| play | Customized playback and pause |
| time | Customized play time |
| fastBack | Customized rewind |
| progress | Customized progress |
| fastForward | Customized fast forward |
| volume | Custom volume |
| more | Customize more |
| volume-mute | Custom mute icon. |
| volume-unmute | Custom unmute icon. |
| more-btn-list | Customize more menu content. |
| speed-btn-list | Customize double speed menu content. |

## Expose {#methods}

| Name | Type | Description |
| --- | --- | --- |
| locationToTime | Function | frame skipping |
| handleBack | Function | Rewind |
| handleForward | Function | fast forward |
| handlePauseOrPlay | Function | Pause or play |
| pause | Function | pause |
| play | Function | play |
| betweenPlay | Function | interval play |
| jumpToTime(time, autoPlay = true) | void | Jump to a specified number of seconds; autoplay can be turned off. |
| changeSpeed(value) | void | Set playback speed. |
| copyAddress() | Promise&lt;boolean&gt; | Copy the audio address. |
| openWindow() | void | Open the audio address in a new tab. |

## Instructions for use {#configuration-notes}

The audio example follows the recording address of common-ui. download only dispatches download requests, and file downloads are handled by the consumer. High-precision mode requires a security context; falls back when unavailable, and does not promise millisecond accuracy.
