|
| 1 | +import path from "path"; |
1 | 2 | import React from "react";
|
2 | 3 | import {
|
3 | 4 | observable,
|
@@ -126,6 +127,7 @@ import {
|
126 | 127 | LANGUAGE_ICON,
|
127 | 128 | LOG_ICON,
|
128 | 129 | PALETTE_ICON,
|
| 130 | + PLAY_AUDIO_ICON, |
129 | 131 | PRINT_TO_PDF_ICON
|
130 | 132 | } from "project-editor/ui-components/icons";
|
131 | 133 | import { humanize } from "eez-studio-shared/string";
|
@@ -2077,6 +2079,107 @@ export class LogActionComponent extends ActionComponent {
|
2077 | 2079 |
|
2078 | 2080 | ////////////////////////////////////////////////////////////////////////////////
|
2079 | 2081 |
|
| 2082 | +export class PlayAudioActionComponent extends ActionComponent { |
| 2083 | + static classInfo = makeDerivedClassInfo(ActionComponent.classInfo, { |
| 2084 | + enabledInComponentPalette: (projectType: ProjectType) => |
| 2085 | + projectType === ProjectType.DASHBOARD, |
| 2086 | + |
| 2087 | + properties: [ |
| 2088 | + makeExpressionProperty( |
| 2089 | + { |
| 2090 | + name: "audioFile", |
| 2091 | + type: PropertyType.MultilineText, |
| 2092 | + propertyGridGroup: specificGroup |
| 2093 | + }, |
| 2094 | + "string" |
| 2095 | + ) |
| 2096 | + ], |
| 2097 | + defaultValue: {}, |
| 2098 | + icon: PLAY_AUDIO_ICON, |
| 2099 | + componentHeaderColor: "#C9E9D2", |
| 2100 | + execute: (context: IDashboardComponentContext) => { |
| 2101 | + const audioFile = context.evalProperty<string>("audioFile"); |
| 2102 | + if (audioFile == undefined) { |
| 2103 | + context.throwError(`Invalid Audio file property`); |
| 2104 | + return; |
| 2105 | + } |
| 2106 | + |
| 2107 | + // Create an AudioContext |
| 2108 | + const audioContext = new window.AudioContext(); |
| 2109 | + |
| 2110 | + // Function to play audio |
| 2111 | + function playAudio(url: string) { |
| 2112 | + fetch(url) |
| 2113 | + .then(response => response.arrayBuffer()) |
| 2114 | + .then(arrayBuffer => |
| 2115 | + audioContext.decodeAudioData(arrayBuffer) |
| 2116 | + ) |
| 2117 | + .then(audioBuffer => { |
| 2118 | + const source = audioContext.createBufferSource(); |
| 2119 | + source.buffer = audioBuffer; |
| 2120 | + source.connect(audioContext.destination); |
| 2121 | + source.start(0); |
| 2122 | + }) |
| 2123 | + .catch(error => |
| 2124 | + console.error("Error loading audio:", error) |
| 2125 | + ); |
| 2126 | + } |
| 2127 | + |
| 2128 | + playAudio(audioFile); |
| 2129 | + |
| 2130 | + context.propagateValueThroughSeqout(); |
| 2131 | + } |
| 2132 | + }); |
| 2133 | + |
| 2134 | + audioFile: string; |
| 2135 | + |
| 2136 | + override makeEditable() { |
| 2137 | + super.makeEditable(); |
| 2138 | + |
| 2139 | + makeObservable(this, { |
| 2140 | + audioFile: observable |
| 2141 | + }); |
| 2142 | + } |
| 2143 | + |
| 2144 | + getInputs() { |
| 2145 | + return [ |
| 2146 | + { |
| 2147 | + name: "@seqin", |
| 2148 | + type: "any" as ValueType, |
| 2149 | + isSequenceInput: true, |
| 2150 | + isOptionalInput: true |
| 2151 | + }, |
| 2152 | + ...super.getInputs() |
| 2153 | + ]; |
| 2154 | + } |
| 2155 | + |
| 2156 | + getOutputs() { |
| 2157 | + return [ |
| 2158 | + { |
| 2159 | + name: "@seqout", |
| 2160 | + type: "null" as ValueType, |
| 2161 | + isSequenceOutput: true, |
| 2162 | + isOptionalOutput: true |
| 2163 | + }, |
| 2164 | + ...super.getOutputs() |
| 2165 | + ]; |
| 2166 | + } |
| 2167 | + |
| 2168 | + getBody(flowContext: IFlowContext): React.ReactNode { |
| 2169 | + if (!this.audioFile) { |
| 2170 | + return null; |
| 2171 | + } |
| 2172 | + |
| 2173 | + return ( |
| 2174 | + <div className="body"> |
| 2175 | + <pre>{path.basename(this.audioFile)}</pre> |
| 2176 | + </div> |
| 2177 | + ); |
| 2178 | + } |
| 2179 | +} |
| 2180 | + |
| 2181 | +//////////////////////////////////////////////////////////////////////////////// |
| 2182 | + |
2080 | 2183 | export class CallActionActionComponent extends ActionComponent {
|
2081 | 2184 | static classInfo = makeDerivedClassInfo(ActionComponent.classInfo, {
|
2082 | 2185 | flowComponentId: COMPONENT_TYPE_CALL_ACTION_ACTION,
|
@@ -4969,6 +5072,8 @@ registerClass("SortArrayActionComponent", SortArrayActionComponent);
|
4969 | 5072 |
|
4970 | 5073 | registerClass("LogActionComponent", LogActionComponent);
|
4971 | 5074 |
|
| 5075 | +registerClass("PlayAudioActionComponent", PlayAudioActionComponent); |
| 5076 | + |
4972 | 5077 | registerClass("ReadSettingActionComponent", ReadSettingActionComponent);
|
4973 | 5078 | registerClass("WriteSettingsActionComponent", WriteSettingsActionComponent);
|
4974 | 5079 |
|
|
0 commit comments