|
| 1 | +--- |
| 2 | +id: voice-recordings |
| 3 | +title: Voice recordings |
| 4 | +--- |
| 5 | + |
| 6 | +import VoiceRecordingScreenshot from "../assets/voice-recording-screenshot.png"; |
| 7 | +import StartRecording from "../assets/start-voice-recording.png"; |
| 8 | +import RecordingDemo from "../assets/voice-recording-demo.png"; |
| 9 | +import Playback from "../assets/voice-recording-playback.png"; |
| 10 | + |
| 11 | +The Stream API allows you to send voice recordings as attachments. This is an example attachment: |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "type": "voiceRecording", |
| 16 | + "title": "audio_recording_2024-09-26T14:41:24.473Z.wav", |
| 17 | + "asset_url": "https://dublin.stream-io-cdn.com/...", |
| 18 | + "waveform_data": [ |
| 19 | + 0.03389095297537962, |
| 20 | + 0.03389095297537962, |
| 21 | + 0.19684165186582253 //... |
| 22 | + ], |
| 23 | + "duration": 3.119, |
| 24 | + "file_size": 97964, |
| 25 | + "mime_type": "audio/wav" |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Playing voice recordings |
| 30 | + |
| 31 | +The SDK can play these recordings without any additional setup: |
| 32 | + |
| 33 | +<img src={VoiceRecordingScreenshot} width="500" /> |
| 34 | + |
| 35 | +If you'd like to use your own UI, you can provide your own template using the [custom templates service](../../services/CustomTemplatesService/#voicerecordingattachmenttemplate). |
| 36 | + |
| 37 | +## Creating voice recordings |
| 38 | + |
| 39 | +However, if you want to let users to create voice recordings, you have to configure the SDK with a few easy steps. |
| 40 | + |
| 41 | +### Start recording button |
| 42 | + |
| 43 | +The [message input](../../components/MessageInputComponent) component can optionally display the start recording button. This is how you can enable it: |
| 44 | + |
| 45 | +```html |
| 46 | +<stream-message-input |
| 47 | + [displayVoiceRecordingButton]="true" |
| 48 | +></stream-message-input> |
| 49 | +``` |
| 50 | + |
| 51 | +This is how the input looks with the button enabled: |
| 52 | + |
| 53 | +<img src={StartRecording} width="500" /> |
| 54 | + |
| 55 | +You can also display your own start button, the easiest way is to use the `message-input-start` or `message-input-end` attributes which will instert the element before or after the textarea element: |
| 56 | + |
| 57 | +```html |
| 58 | +<stream-message-input #input [displayVoiceRecordingButton]="false"> |
| 59 | + <button message-input-start (click)="input.startVoiceRecording()"> |
| 60 | + Start recording |
| 61 | + </button> |
| 62 | +</stream-message-input> |
| 63 | +``` |
| 64 | + |
| 65 | +### Import the `VoiceRecorderModule` |
| 66 | + |
| 67 | +The `VoiceRecorderModule` contains the voice recorder component, and the services needed to make a recording. You have to import it like this: |
| 68 | + |
| 69 | +```typescript |
| 70 | +import { StreamChatModule, VoiceRecorderModule } from "stream-chat-angular"; |
| 71 | + |
| 72 | +@NgModule({ |
| 73 | + declarations: [AppComponent], |
| 74 | + imports: [ |
| 75 | + // other imports... |
| 76 | + StreamChatModule, |
| 77 | + VoiceRecorderModule, |
| 78 | + // ...more imports |
| 79 | + ], |
| 80 | + bootstrap: [AppComponent], |
| 81 | +}) |
| 82 | +export class AppModule {} |
| 83 | +``` |
| 84 | + |
| 85 | +### Display the voice recorder component |
| 86 | + |
| 87 | +You have to provide the voice recorder template to the message input component. The SDK provides the `VoiceRecorderComponent` for this: |
| 88 | + |
| 89 | +```html |
| 90 | +<stream-message-input [displayVoiceRecordingButton]="true"> |
| 91 | + <ng-template voice-recorder let-service="service"> |
| 92 | + <stream-voice-recorder |
| 93 | + [voiceRecorderService]="service" |
| 94 | + ></stream-voice-recorder> |
| 95 | + </ng-template> |
| 96 | +</stream-message-input> |
| 97 | +``` |
| 98 | + |
| 99 | +The message input provides a [`VoiceRecorderService`](../../services/VoiceRecorderService) instance which is used by the two components to communicate. |
| 100 | + |
| 101 | +If you want to use your own UI, just simply provide you own component here that uses `VoiceRecorderService` to communicate with the message input component. |
| 102 | + |
| 103 | +### Voice recorder component |
| 104 | + |
| 105 | +That's it. We can now start a voice recording: |
| 106 | + |
| 107 | +<img src={RecordingDemo} width="500" /> |
| 108 | + |
| 109 | +The recording can be paused and resumed. Once a user is finished recording they can play it back: |
| 110 | + |
| 111 | +<img src={Playback} width="500" /> |
| 112 | + |
| 113 | +If they are not happy with the recording, they can simply discard it. Once the reacording is finalized, they can hit the send button. |
| 114 | + |
| 115 | +### Sending modes |
| 116 | + |
| 117 | +There are two ways to send voice recordings: |
| 118 | + |
| 119 | +1. Once a recording is finalized you can immediately send a message with the recording, this is the default mode. |
| 120 | +2. Once a recording is finalized you can return to the message composer to continue editing the message. |
| 121 | + |
| 122 | +This how you can change between the modes: |
| 123 | + |
| 124 | +```typescript |
| 125 | +constructor(private messageInputService: MessageInputConfigService) { |
| 126 | + // Defaults to true |
| 127 | + // Set false if you want to return to the message composer after a recording was added to the message |
| 128 | + this.messageInputService.sendVoiceRecordingImmediately = true; |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Error states |
| 133 | + |
| 134 | +If any time during the recording an error occurs, the recording will be stopped, and an error message will be emitted via the [`NotificationService`](../../services/NotificationService). The built-in `stream-notification-list` component will display this error message. |
| 135 | + |
| 136 | +### Custom recording title |
| 137 | + |
| 138 | +You can use your own method the generate the title of the recording using the `customGenerateRecordingTitle` field of the `AudioRecorderService`. |
| 139 | + |
| 140 | +```typescript |
| 141 | +constructor(private audioRecorder: AudioRecorderService, private chatService: ChatClientService) { |
| 142 | + this.audioRecorder.customGenerateRecordingTitle = ( |
| 143 | + options: MediaRecordingTitleOptions |
| 144 | + ) => { |
| 145 | + const extension = options.mimeType.match(/\/([^/;]+)/)?.[1] || ""; |
| 146 | + return `${ |
| 147 | + this.chatService.chatClient.user?.name |
| 148 | + }-${new Date().toISOString()}.${extension}`; |
| 149 | + }; |
| 150 | +} |
| 151 | +``` |
0 commit comments