@ipcom/asterisk-ari 0.0.149 → 0.0.151

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -1182,42 +1182,73 @@ var ChannelInstance = class {
1182
1182
  * Plays media on the channel
1183
1183
  */
1184
1184
  async play(options, playbackId) {
1185
- if (!options.media) {
1186
- throw new Error("Media URL is required");
1187
- }
1188
- try {
1189
- const mediaPath = options.media.replace("sound:", "");
1190
- console.log("Formatted media path:", mediaPath);
1191
- if (!this.channelData) {
1192
- console.log("Fetching channel details...");
1193
- this.channelData = await this.getDetails();
1194
- }
1195
- if (!this.channelData?.id) {
1196
- throw new Error("Channel ID is not available");
1185
+ return new Promise(async (resolve, reject) => {
1186
+ try {
1187
+ if (!options.media) {
1188
+ throw new Error("Media URL is required");
1189
+ }
1190
+ if (!this.channelData) {
1191
+ this.channelData = await this.getDetails();
1192
+ }
1193
+ const playback = this.client.Playback(playbackId || v4_default());
1194
+ let playbackStarted = false;
1195
+ const failureListener = (event) => {
1196
+ if ("playback" in event && event.playback) {
1197
+ if (event.playback.state === "failed") {
1198
+ console.error("Playback falhou:", {
1199
+ playbackId: playback.id,
1200
+ channelId: this.id,
1201
+ state: event.playback.state
1202
+ });
1203
+ if (!playbackStarted) {
1204
+ reject(
1205
+ new Error(
1206
+ `Playback failed to start: ${event.playback.state}`
1207
+ )
1208
+ );
1209
+ }
1210
+ }
1211
+ }
1212
+ };
1213
+ const startListener = (event) => {
1214
+ if ("playback" in event && event.playback) {
1215
+ console.log("Playback come\xE7ou:", {
1216
+ playbackId: playback.id,
1217
+ channelId: this.id,
1218
+ state: event.playback.state
1219
+ });
1220
+ playbackStarted = true;
1221
+ setTimeout(async () => {
1222
+ try {
1223
+ const status = await playback.get();
1224
+ if (status.state === "playing") {
1225
+ resolve(playback);
1226
+ } else {
1227
+ reject(
1228
+ new Error(`Playback started but state is ${status.state}`)
1229
+ );
1230
+ }
1231
+ } catch (_err) {
1232
+ reject(new Error("Failed to verify playback status"));
1233
+ }
1234
+ }, 500);
1235
+ }
1236
+ };
1237
+ playback.once("PlaybackStarted", startListener);
1238
+ playback.once("PlaybackFinished", failureListener);
1239
+ await this.baseClient.post(
1240
+ `/channels/${this.id}/play/${playback.id}`,
1241
+ options
1242
+ );
1243
+ setTimeout(() => {
1244
+ if (!playbackStarted) {
1245
+ reject(new Error("Playback timeout - n\xE3o iniciou"));
1246
+ }
1247
+ }, 5e3);
1248
+ } catch (error) {
1249
+ reject(error);
1197
1250
  }
1198
- console.log("Channel state:", this.channelData.state);
1199
- const playback = this.client.Playback(playbackId || v4_default());
1200
- return new Promise((resolve, reject) => {
1201
- playback.once("PlaybackFinished", (event) => {
1202
- console.log("Playback finished: ", event);
1203
- });
1204
- this.baseClient.post(`/channels/${this.id}/play/${playback.id}`, options).then(() => {
1205
- console.log("Playback request successful");
1206
- resolve(playback);
1207
- }).catch((error) => {
1208
- console.error("Error starting playback:", error);
1209
- reject(error);
1210
- });
1211
- });
1212
- } catch (error) {
1213
- const message = getErrorMessage(error);
1214
- console.error(`Error playing media on channel ${this.id}:`, {
1215
- error: message,
1216
- options,
1217
- channelState: this.channelData?.state
1218
- });
1219
- throw new Error(`Failed to play media: ${message}`);
1220
- }
1251
+ });
1221
1252
  }
1222
1253
  /**
1223
1254
  * Gets the current channel details