cordova-plugin-media icon indicating copy to clipboard operation
cordova-plugin-media copied to clipboard

Getting status on file loaded

Open capiztrr opened this issue 3 years ago • 7 comments

Im building an streaming app with ionic and capacitor and i need some way to detect that the stream url did in fact load

here's an example:

this.media.create(urlLiveBroadcast); this.media.play() it doesnt work because the urlLiveBroadcast takes like 5 seconds to load, and there is no status event to handle that

if i create the media within the app initialization it keeps using the network before you tap de "play" button

how should i proceed?

capiztrr avatar Sep 10 '20 04:09 capiztrr

Hello @capiztrr, I think we have the same issue.

When we are on a fast network (i. e. WiFi), the stream url loads almost immediately and the play() method works as expected. However, when we are on 3G/4G, the stream sometimes does not start the play. The plugin still reports Status = 2. Did you find a good solution?

One possibility is to use a timeout function to wait for a few seconds before playing. But this is also a long shot because it will still not work in some cases, and in other cases you wait longer than necessary.

andreas-aeschlimann avatar Sep 18 '20 10:09 andreas-aeschlimann

i didnt find any solution yet.

Yes, you can hardcode like 10 seconds before calling the play method, but its a really weak solution

capiztrr avatar Sep 18 '20 12:09 capiztrr

i didnt find any solution yet.

Yes, you can hardcode like 10 seconds before calling the play method, but its a really weak solution

Indeed. Also I found that Android starts playing immediately for around 5 seconds and then starts to buffering for 5 seconds. Really confusing for the listener because it is unclear whether the stream is actually buffering or broken.

andreas-aeschlimann avatar Sep 18 '20 12:09 andreas-aeschlimann

I never got it working on android. I had to use HTML Audio for android

capiztrr avatar Sep 18 '20 12:09 capiztrr

I'm using ionic and cordova. I have no issue with version 5.0.3 and Android, but I get the same issue on iOS:

  • if I create the Media and call play right after, I do see Media.MEDIA_RUNNING status, but I don't hear anything
  • if I put a 5s timeout between the 2 calls, it works

I reverted back to version 4.0.0 only on iOS to fix the issue.

I'm streaming audio from the Internet using HTTPS.

Heshyo avatar Jan 25 '21 03:01 Heshyo

I solved this issue in the following way: I added a new method

async start(url) {
    this.player = this.media.create(url);
    await this.startPlay();
}
startPlay() {
    return new Promise((resolve, reject) => {
      let playInterval = setInterval(() => {
        this.player.play({ numberOfLoops: 1, playAudioWhenScreenIsLocked: true });
        this.player.getCurrentPosition().then((position) => {
          if (position > 0) {
            resolve(position);
            clearInterval(playInterval);
          }
        });
      }, 2000);
    })
}

HussamBarbour avatar Apr 20 '21 21:04 HussamBarbour

I believe this is the same problem as https://github.com/apache/cordova-plugin-media/issues/256. I solved it in my fork by setting automaticallyWaitsToMinimizeStalling to YES (tested on iPad and iPhone with iOS 15, HTTP static MP3 ad live streams, without any adverse effect).

goffioul avatar Apr 28 '22 05:04 goffioul