@ipcom/asterisk-ari 0.0.148 → 0.0.150

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.
@@ -1202,55 +1202,45 @@ var ChannelInstance = class {
1202
1202
  * Plays media on the channel
1203
1203
  */
1204
1204
  async play(options, playbackId) {
1205
- if (!options.media) {
1206
- throw new Error("Media URL is required");
1207
- }
1208
- try {
1209
- const mediaPath = options.media.replace("sound:", "");
1210
- console.log("Formatted media path:", mediaPath);
1211
- if (!this.channelData) {
1212
- console.log("Fetching channel details...");
1213
- this.channelData = await this.getDetails();
1214
- }
1215
- if (!this.channelData?.id) {
1216
- throw new Error("Channel ID is not available");
1217
- }
1218
- console.log("Channel state:", this.channelData.state);
1219
- const playback = this.client.Playback(playbackId || v4_default());
1220
- const payload = {
1221
- media: options.media,
1222
- ...options.lang && { lang: options.lang }
1223
- };
1224
- console.log("Sending playback request:", {
1225
- channelId: this.id,
1226
- playbackId: playback.id,
1227
- payload
1228
- });
1229
- await this.baseClient.post(
1230
- `/channels/${this.id}/play/${playback.id}`,
1231
- payload
1232
- );
1205
+ return new Promise(async (resolve, reject) => {
1233
1206
  try {
1234
- const playbackStatus = await playback.get();
1235
- if (playbackStatus.state !== "playing") {
1236
- console.warn("Playback not in playing state:", playbackStatus);
1207
+ if (!options.media) {
1208
+ throw new Error("Media URL is required");
1237
1209
  }
1238
- playback.once("PlaybackFinished", (event) => {
1239
- console.log("Playback finished", event);
1210
+ if (!this.channelData) {
1211
+ this.channelData = await this.getDetails();
1212
+ }
1213
+ const playback = this.client.Playback(playbackId || v4_default());
1214
+ playback.once("PlaybackStarted", (event) => {
1215
+ if ("playback" in event) {
1216
+ console.log("Playback realmente iniciado:", playback.id);
1217
+ resolve(playback);
1218
+ }
1240
1219
  });
1220
+ playback.once("PlaybackFinished", async (event) => {
1221
+ if ("playback" in event && event.playback) {
1222
+ const playbackState = event.playback.state;
1223
+ if (playbackState === "failed") {
1224
+ console.error("Playback falhou:", {
1225
+ playbackId: playback.id,
1226
+ channelId: this.id,
1227
+ state: playbackState
1228
+ });
1229
+ reject(new Error(`Playback failed: ${playbackState}`));
1230
+ }
1231
+ }
1232
+ });
1233
+ await this.baseClient.post(
1234
+ `/channels/${this.id}/play/${playback.id}`,
1235
+ options
1236
+ );
1237
+ setTimeout(() => {
1238
+ reject(new Error("Playback timeout - n\xE3o recebeu evento de in\xEDcio"));
1239
+ }, 5e3);
1241
1240
  } catch (error) {
1242
- console.warn("Could not verify playback status:", error);
1241
+ reject(error);
1243
1242
  }
1244
- return playback;
1245
- } catch (error) {
1246
- const message = getErrorMessage(error);
1247
- console.error(`Error playing media on channel ${this.id}:`, {
1248
- error: message,
1249
- options,
1250
- channelState: this.channelData?.state
1251
- });
1252
- throw new Error(`Failed to play media: ${message}`);
1253
- }
1243
+ });
1254
1244
  }
1255
1245
  /**
1256
1246
  * Gets the current channel details