@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.
@@ -1202,42 +1202,73 @@ 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");
1205
+ return new Promise(async (resolve, reject) => {
1206
+ try {
1207
+ if (!options.media) {
1208
+ throw new Error("Media URL is required");
1209
+ }
1210
+ if (!this.channelData) {
1211
+ this.channelData = await this.getDetails();
1212
+ }
1213
+ const playback = this.client.Playback(playbackId || v4_default());
1214
+ let playbackStarted = false;
1215
+ const failureListener = (event) => {
1216
+ if ("playback" in event && event.playback) {
1217
+ if (event.playback.state === "failed") {
1218
+ console.error("Playback falhou:", {
1219
+ playbackId: playback.id,
1220
+ channelId: this.id,
1221
+ state: event.playback.state
1222
+ });
1223
+ if (!playbackStarted) {
1224
+ reject(
1225
+ new Error(
1226
+ `Playback failed to start: ${event.playback.state}`
1227
+ )
1228
+ );
1229
+ }
1230
+ }
1231
+ }
1232
+ };
1233
+ const startListener = (event) => {
1234
+ if ("playback" in event && event.playback) {
1235
+ console.log("Playback come\xE7ou:", {
1236
+ playbackId: playback.id,
1237
+ channelId: this.id,
1238
+ state: event.playback.state
1239
+ });
1240
+ playbackStarted = true;
1241
+ setTimeout(async () => {
1242
+ try {
1243
+ const status = await playback.get();
1244
+ if (status.state === "playing") {
1245
+ resolve(playback);
1246
+ } else {
1247
+ reject(
1248
+ new Error(`Playback started but state is ${status.state}`)
1249
+ );
1250
+ }
1251
+ } catch (_err) {
1252
+ reject(new Error("Failed to verify playback status"));
1253
+ }
1254
+ }, 500);
1255
+ }
1256
+ };
1257
+ playback.once("PlaybackStarted", startListener);
1258
+ playback.once("PlaybackFinished", failureListener);
1259
+ await this.baseClient.post(
1260
+ `/channels/${this.id}/play/${playback.id}`,
1261
+ options
1262
+ );
1263
+ setTimeout(() => {
1264
+ if (!playbackStarted) {
1265
+ reject(new Error("Playback timeout - n\xE3o iniciou"));
1266
+ }
1267
+ }, 5e3);
1268
+ } catch (error) {
1269
+ reject(error);
1217
1270
  }
1218
- console.log("Channel state:", this.channelData.state);
1219
- const playback = this.client.Playback(playbackId || v4_default());
1220
- return new Promise((resolve, reject) => {
1221
- playback.once("PlaybackFinished", (event) => {
1222
- console.log("Playback finished: ", event);
1223
- });
1224
- this.baseClient.post(`/channels/${this.id}/play/${playback.id}`, options).then(() => {
1225
- console.log("Playback request successful");
1226
- resolve(playback);
1227
- }).catch((error) => {
1228
- console.error("Error starting playback:", error);
1229
- reject(error);
1230
- });
1231
- });
1232
- } catch (error) {
1233
- const message = getErrorMessage(error);
1234
- console.error(`Error playing media on channel ${this.id}:`, {
1235
- error: message,
1236
- options,
1237
- channelState: this.channelData?.state
1238
- });
1239
- throw new Error(`Failed to play media: ${message}`);
1240
- }
1271
+ });
1241
1272
  }
1242
1273
  /**
1243
1274
  * Gets the current channel details