# Voice player

Điều khiển phát và transcript của tin nhắn thoại.

## Key rules

- Kiểm tra nguồn trước khi truyền vào player.
- Element `chatty-voice-player` ở mục Standard là chuẩn chatty-ui port theo; các mục theo surface mô tả component React đang chạy.

## Use when / Avoid

Dùng cho tệp thoại trong luồng tin nhắn.

### Use instead

| Component | Use when |
| --- | --- |
| `VoicePlayer` | Phát audio đã được caller kiểm tra. |
| `FileCard` | Không phát được và cần hiển thị tệp dự phòng. |

## Live preview

Example (Standard) — Element chatty-voice-player chưa có src: độ dài tạm 0:42, control bị vô hiệu như preview React.

```html
<chatty-voice-player duration="42" download="voice-message.mp3" style="width: 320px">
  <svg
    slot="download-icon"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    aria-hidden="true"
  >
    <path d="M12 4v11m-5-5 5 5 5-5M5 20h14" />
  </svg>
</chatty-voice-player>
```

Example (Standard) — Element chatty-voice-player chưa có src: độ dài tạm 0:42, control bị vô hiệu như preview React.

```html
<chatty-voice-player duration="42" download="voice-message.mp3" style="width: 320px">
  <svg
    slot="download-icon"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    aria-hidden="true"
  >
    <path d="M12 4v11m-5-5 5 5 5-5M5 20h14" />
  </svg>
</chatty-voice-player>
```

Example (Standard) — Element chatty-voice-player chưa có src: độ dài tạm 0:42, control bị vô hiệu như preview React.

```html
<chatty-voice-player duration="42" download="voice-message.mp3" style="width: 320px">
  <svg
    slot="download-icon"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    aria-hidden="true"
  >
    <path d="M12 4v11m-5-5 5 5 5-5M5 20h14" />
  </svg>
</chatty-voice-player>
```

Example (Current (React), being replaced) — Player thật chưa có nguồn audio; không gọi mạng.

```jsx
import React from 'react';
import {AppProvider, Button} from '@shopify/polaris';
import {VoicePlayer} from '@avada/chatty-ui/components';

const source = '';

// Stand-in for DownloadIcon: @shopify/polaris-icons is not in the guide lockfile.
const DownloadIcon = () => (
  <svg viewBox="0 0 20 20" aria-hidden="true">
    <path d="M10 3a.75.75 0 0 1 .75.75v7.19l2.22-2.22a.75.75 0 1 1 1.06 1.06l-3.5 3.5a.75.75 0 0 1-1.06 0l-3.5-3.5a.75.75 0 1 1 1.06-1.06l2.22 2.22V3.75A.75.75 0 0 1 10 3Zm-5.25 11.5a.75.75 0 0 1 .75.75v.25h9v-.25a.75.75 0 0 1 1.5 0v1a.75.75 0 0 1-.75.75H4.75a.75.75 0 0 1-.75-.75v-1a.75.75 0 0 1 .75-.75Z" />
  </svg>
);

function StorefrontVoicePlayer() {
  return (
    <VoicePlayer
      src={source}
      durationSec={42}
      downloadControl={false}
      labels={{play: 'Play', pause: 'Pause', seek: 'Seek', speed: 'Playback speed'}}
    />
  );
}

function AdminVoicePlayer() {
  return (
    <AppProvider i18n={{}}>
      <VoicePlayer
        src={source}
        durationSec={42}
        fileName="voice-message.mp3"
        downloadControl={
          <Button
            icon={DownloadIcon}
            variant="plain"
            download="voice-message.mp3"
            accessibilityLabel="Download voice message"
          />
        }
        labels={{
          play: 'Play voice message',
          pause: 'Pause voice message',
          seek: 'Seek voice message',
          download: 'Download voice message',
          speed: 'Playback speed',
          position: (current, duration) => `Played ${current} of ${duration}`
        }}
      />
    </AppProvider>
  );
}

export function Catalogue({surface}) {
  if (surface === 'admin') return <AdminVoicePlayer />;

  return <StorefrontVoicePlayer />;
}
```

Example (Current (React), being replaced) — Player Admin với nhãn và control tải xuống của inbox.

```jsx
import React from 'react';
import {AppProvider, Button} from '@shopify/polaris';
import {VoicePlayer} from '@avada/chatty-ui/components';

const source = '';

// Stand-in for DownloadIcon: @shopify/polaris-icons is not in the guide lockfile.
const DownloadIcon = () => (
  <svg viewBox="0 0 20 20" aria-hidden="true">
    <path d="M10 3a.75.75 0 0 1 .75.75v7.19l2.22-2.22a.75.75 0 1 1 1.06 1.06l-3.5 3.5a.75.75 0 0 1-1.06 0l-3.5-3.5a.75.75 0 1 1 1.06-1.06l2.22 2.22V3.75A.75.75 0 0 1 10 3Zm-5.25 11.5a.75.75 0 0 1 .75.75v.25h9v-.25a.75.75 0 0 1 1.5 0v1a.75.75 0 0 1-.75.75H4.75a.75.75 0 0 1-.75-.75v-1a.75.75 0 0 1 .75-.75Z" />
  </svg>
);

function StorefrontVoicePlayer() {
  return (
    <VoicePlayer
      src={source}
      durationSec={42}
      downloadControl={false}
      labels={{play: 'Play', pause: 'Pause', seek: 'Seek', speed: 'Playback speed'}}
    />
  );
}

function AdminVoicePlayer() {
  return (
    <AppProvider i18n={{}}>
      <VoicePlayer
        src={source}
        durationSec={42}
        fileName="voice-message.mp3"
        downloadControl={
          <Button
            icon={DownloadIcon}
            variant="plain"
            download="voice-message.mp3"
            accessibilityLabel="Download voice message"
          />
        }
        labels={{
          play: 'Play voice message',
          pause: 'Pause voice message',
          seek: 'Seek voice message',
          download: 'Download voice message',
          speed: 'Playback speed',
          position: (current, duration) => `Played ${current} of ${duration}`
        }}
      />
    </AppProvider>
  );
}

export function Catalogue({surface}) {
  if (surface === 'admin') return <AdminVoicePlayer />;

  return <StorefrontVoicePlayer />;
}
```

Example (Current (React), being replaced) — Player AI Mode thật, không có control tải xuống.

```jsx
import React from 'react';
import {AppProvider, Button} from '@shopify/polaris';
import {VoicePlayer} from '@avada/chatty-ui/components';

const source = '';

// Stand-in for DownloadIcon: @shopify/polaris-icons is not in the guide lockfile.
const DownloadIcon = () => (
  <svg viewBox="0 0 20 20" aria-hidden="true">
    <path d="M10 3a.75.75 0 0 1 .75.75v7.19l2.22-2.22a.75.75 0 1 1 1.06 1.06l-3.5 3.5a.75.75 0 0 1-1.06 0l-3.5-3.5a.75.75 0 1 1 1.06-1.06l2.22 2.22V3.75A.75.75 0 0 1 10 3Zm-5.25 11.5a.75.75 0 0 1 .75.75v.25h9v-.25a.75.75 0 0 1 1.5 0v1a.75.75 0 0 1-.75.75H4.75a.75.75 0 0 1-.75-.75v-1a.75.75 0 0 1 .75-.75Z" />
  </svg>
);

function StorefrontVoicePlayer() {
  return (
    <VoicePlayer
      src={source}
      durationSec={42}
      downloadControl={false}
      labels={{play: 'Play', pause: 'Pause', seek: 'Seek', speed: 'Playback speed'}}
    />
  );
}

function AdminVoicePlayer() {
  return (
    <AppProvider i18n={{}}>
      <VoicePlayer
        src={source}
        durationSec={42}
        fileName="voice-message.mp3"
        downloadControl={
          <Button
            icon={DownloadIcon}
            variant="plain"
            download="voice-message.mp3"
            accessibilityLabel="Download voice message"
          />
        }
        labels={{
          play: 'Play voice message',
          pause: 'Pause voice message',
          seek: 'Seek voice message',
          download: 'Download voice message',
          speed: 'Playback speed',
          position: (current, duration) => `Played ${current} of ${duration}`
        }}
      />
    </AppProvider>
  );
}

export function Catalogue({surface}) {
  if (surface === 'admin') return <AdminVoicePlayer />;

  return <StorefrontVoicePlayer />;
}
```

## Standard

Example (Standard) — Element chatty-voice-player chưa có src: độ dài tạm 0:42, control bị vô hiệu như preview React.

```html
<chatty-voice-player duration="42" download="voice-message.mp3" style="width: 320px">
  <svg
    slot="download-icon"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    aria-hidden="true"
  >
    <path d="M12 4v11m-5-5 5 5 5-5M5 20h14" />
  </svg>
</chatty-voice-player>
```

Prototype  (cùng `voice-player.styles.js`) là chuẩn: chatty-ui port 1:1, và `chatty-ui/custom-elements.json` sinh từ chính prototype. Nhãn đi qua property `.strings`; icon tải về đi qua slot. Chưa surface nào dùng element thật; các mục Classic, AI Mode và Admin bên dưới mô tả React đang chạy.

### Element

| Element | Module |
| --- | --- |
| `chatty-voice-player` | @avada/chatty-ui/elements/voice-player.js |

### API

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `src` | string | `''` | URL audio đã kiểm; rỗng thì các control bị vô hiệu. |
| attribute | `duration` | number | `0` | Độ dài tạm (giây) tới khi audio báo độ dài thật. |
| attribute | `download` | string | `''` | Tên file; có giá trị và có src thì hiện link tải. |
| event | `play` | Event | – | Bắt đầu phát. |
| event | `pause` | Event | – | Dừng hoặc phát hết. |
| event | `click` | MouseEvent | – | Kích hoạt native của nút play, tốc độ. |
| slot | `download-icon` | – | – | Icon của link tải. |
| part | `base` | – | – | Hàng player. |
| part | `play` | – | – | Nút play/pause. |
| part | `track` | – | – | Vùng thanh tua; mũi tên trái/phải tua, Space/Enter phát hoặc dừng. |
| part | `time` | – | – | Thời gian đã phát / độ dài. |
| part | `speed` | – | – | Nút tốc độ, đổi vòng qua các mức. |
| part | `download` | – | – | Link tải. |
| css-property | `--chatty-voice-player-background` | – | – | Nền. |
| css-property | `--chatty-voice-player-color` | – | – | Màu icon và chữ. |
| css-property | `--chatty-voice-player-border-color` | – | – | Màu viền. |
| css-property | `--chatty-voice-player-track` | – | – | Màu phần chưa phát và nền nút tốc độ. |

## Classic

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `VoicePlayer` | @avada/chatty-ui/components | `.Avada-Faqs_Popup` |
| Voice message audio | — | `.Avada-Faqs_Popup` |
| `.Avada-Faq-VoiceMessage` | — | `.Avada-Faq-VoiceMessage` |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Audio ẩn và nút play. |
| 2 | Thanh seek, thời gian, tốc độ và download. |

### Placement / order

| Context | Order |
| --- | --- |
| Voice player | Play, track, timing rồi download. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `src` | string | URL audio đã kiểm tra | Caller | Thiếu nguồn thì nút và seek disabled. |
| `durationSec` | number | Thời lượng dự phòng | 0 | Thay bằng thời lượng đo được. |
| `labels` | object | Nhãn điều khiển | Caller | Cung cấp tên accessible. |
| `downloadControl` | node hoặc false | Control của surface | Caller | False ẩn download. |
| `file` | object | URL và metadata | Caller | Wrapper chỉ truyền URL an toàn vào player. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Player | Tối thiểu 46px; 58px ở mobile/coarse pointer | 6px | 6px | 14px | Kế thừa | Play 32px | Rộng 280px, tối đa theo container |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Foreground | `--voice-player-foreground` | Theo caller | Theo caller |
| Background | `--voice-player-background` | Theo caller | Theo caller |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Không nguồn | src rỗng | Play và seek disabled. | Native disabled |
| Đang phát | playing | Icon và nhãn chuyển sang pause. | aria-label từ caller |

### Behaviour

| Interaction | Result |
| --- | --- |
| Seek bằng phím | Trái hoặc phải tua 5 giây; Enter và Space đổi trạng thái phát. |
| Speed | Chuyển lần lượt giữa các tốc độ. |
| Không phát được | Hiện phản hồi lỗi hoặc trạng thái transcript của wrapper. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Play | Play | Nhãn play do wrapper truyền. |
| Pause | Pause | Nhãn pause do wrapper truyền. |

### Source

| File | Evidence |
| --- | --- |
|  | Player API và hành vi |
|  | Layout và focus |
|  | Lazy import Classic |
|  | Caller Classic từ voice block Widget v2 |
|  | Props và feedback Classic |
|  | Width và minimum height Classic |

## AI Mode

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `VoicePlayer` | @avada/chatty-ui/components | `.chatty-ai-mode` |
| Voice message bubble | — | `.chatty-ai-mode` |
| `.chatty-voice-message` | — | `.chatty-voice-message` |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Audio ẩn và nút play. |
| 2 | Thanh seek, thời gian, tốc độ và download. |

### Placement / order

| Context | Order |
| --- | --- |
| Voice player | Play, track, timing rồi download. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `src` | string | URL audio an toàn | Caller | Wrapper chỉ truyền URL đã kiểm tra. |
| `durationSec` | number | Thời lượng từ file | Caller | Chuyển tiếp vào player. |
| `downloadControl` | false | `false` | `false` | AI Mode ẩn download. |
| `labels` | object | Play, Pause, Seek, Playback speed, position | Caller | Nhãn lấy từ i18n của AI Mode. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Player | Tối thiểu 46px; 58px ở mobile/coarse pointer | 6px | 6px | 14px | Kế thừa | Play 32px | Rộng 280px, tối đa theo container |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Foreground | `--voice-player-foreground` | `var(--vib-1)` | `var(--vib-1)` |
| Background | `--voice-player-background` | `var(--surface-opaque)` | `var(--surface-opaque)` |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Không nguồn | URL không an toàn hoặc thiếu | Không render player; file thường là fallback khi có file. | Không đổi |
| Đang phát | playing | Icon và nhãn chuyển sang pause. | aria-label từ caller |

### Behaviour

| Interaction | Result |
| --- | --- |
| Seek bằng phím | Trái hoặc phải tua 5 giây; Enter và Space đổi trạng thái phát. |
| Speed | Chuyển lần lượt giữa các tốc độ. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Play | Play | Nhãn play do caller truyền. |
| Pause | Pause | Nhãn pause do caller truyền. |

### Source

| File | Evidence |
| --- | --- |
|  | Lazy import AI Mode |
|  | Caller, props và fallback AI Mode |
|  | Caller của attachment block |
|  | Player API và hành vi |

## Admin

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `VoicePlayer` | @avada/chatty-ui/components | .inbox-v2 |
| `VoiceMessageAudio` | @assets/pages/InboxManagement/v2/blocks/VoiceMessageAudio.jsx | .inbox-v2 |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Audio ẩn và nút play. |
| 2 | Thanh seek, thời gian, tốc độ và download. |

### Placement / order

| Context | Order |
| --- | --- |
| Voice player | Play, track, timing rồi download. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `src` | string | URL audio đã kiểm tra | Caller | Thiếu nguồn thì nút và seek disabled. |
| `durationSec` | number | Thời lượng dự phòng | 0 | Thay bằng thời lượng đo được. |
| `labels` | object | Nhãn điều khiển | Caller | Cung cấp tên accessible. |
| `downloadControl` | node hoặc false | Control của surface | Caller | False ẩn download. |
| `file` | object | URL và metadata | Caller | Wrapper kiểm tra nguồn. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Player | Theo nội dung | 6px | 6px | 14px | Kế thừa | Play 32px | Rộng theo container |
| Inbox wrapper | Theo nội dung | Framed 8px 12px | 4px | Theo bubble | body-medium khi framed | Theo player | Rộng 320px, tối đa theo container |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Foreground | `--voice-player-foreground` | Theo caller | No Dark skin |
| Background | `--voice-player-background` | Theo caller | No Dark skin |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Không nguồn | src rỗng | Play và seek disabled. | Native disabled |
| Đang phát | playing | Icon và nhãn chuyển sang pause. | aria-label từ caller |

### Behaviour

| Interaction | Result |
| --- | --- |
| Seek bằng phím | Trái hoặc phải tua 5 giây; Enter và Space đổi trạng thái phát. |
| Speed | Chuyển lần lượt giữa các tốc độ. |
| Không phát được | Wrapper có thể hiện file dự phòng. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Play voice message | Play voice message | Nhãn play do wrapper truyền. |
| Pause voice message | Pause voice message | Nhãn pause do wrapper truyền. |

### Source

| File | Evidence |
| --- | --- |
|  | Player API và hành vi |
|  | Layout và focus |
|  | Kiểm tra nguồn và caller |

## Do / Don't

Do: truyền nhãn điều khiển theo ngôn ngữ của caller.

Don't: bỏ qua bước kiểm tra nguồn audio.

## Accessibility

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| Classic | Play button và slider có nhãn từ caller | Tab, Enter, Space và phím mũi tên | Không có live region | Theo token caller |
| Admin | Play button và slider có nhãn từ caller | Tab, Enter, Space và phím mũi tên | Không có live region | Theo token caller |

## Target

```target
| Surface | Now | Target | Source |
| --- | --- | --- | --- |
| Classic · AI Mode · Admin | VoicePlayer React | Not available — new build follows Target. Element (dự kiến): `chatty-voice-player` đọc attribute `src`, giữ trạng thái phát nội bộ, event `play` / `pause`. | [MDN: Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components) |
| Classic · AI Mode · Admin | Component React theo surface | Not available — new build follows Target. Port `chatty-voice-player` từ mục Standard rồi chuyển caller từng surface. |  |
```


- [MDN: Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components)
