Code Monkey home page Code Monkey logo

Comments (9)

karolyi avatar karolyi commented on July 30, 2024 1

Thanks for the advice anyway. I'm suspecting that your solution (instantiating another player) will be the one here, but let's wait for @eshaz's response too.

from icecast-metadata-js.

BajakiGabesz avatar BajakiGabesz commented on July 30, 2024

Hi @karolyi,

I think @eshaz can answer this question better, but I was in the same shoes.
I could solve it by making the player object static and call stop before I started another source. It is working perfectly.

The next code is used in React, but it may help:
`
let streamPlayer: IcecastMetadataPlayer | undefined = undefined;

const playAsync = async (url: string): Promise => {
streamPlayer = new IcecastMetadataPlayer(
url
);
await streamPlayer.play();
}

const stop = (): void => {
await streamPlayer?.stop();
await streamPlayer?.detachAudioElement();
}
`

Hope this helps.

from icecast-metadata-js.

karolyi avatar karolyi commented on July 30, 2024

The code is a bit garbled to me but if I understand it right, you basically instantiate a new player whenever you want to play another source? If that's the case, why do you need to detach the audio element?

from icecast-metadata-js.

karolyi avatar karolyi commented on July 30, 2024

It says that detachAudioElement is only necessary if you've passed your own audio element for use with the player.

I'm assuming some former version of your code had it?

from icecast-metadata-js.

BajakiGabesz avatar BajakiGabesz commented on July 30, 2024

I just tried to copy the code and remove all the unnecessary parts, but with less success. I also use HTMLAudioElement and detachAudioElement was needed for other kinds of purposes.
Anyhow, if you only need to stop the audio, switch the URL and start, you may try to switch it by changing the underlying audio element source:
streamPlayer.audioElement.src = ""
Anyhow, it was not tested at my side, so I'm not 100% sure it is the right solution.

from icecast-metadata-js.

karolyi avatar karolyi commented on July 30, 2024

Anyhow, if you only need to stop the audio, switch the URL and start, you may try to switch it by changing the underlying audio element source:
streamPlayer.audioElement.src = ""

I was looking into that too, but the audio element has an internal blob source (possibly coming from the decoder part of the player) as a source, and with changing that to the URL you'd lose the metadata decoding ability.

from icecast-metadata-js.

BajakiGabesz avatar BajakiGabesz commented on July 30, 2024

Probably, that is another reason I was made my solution as it is, but I also play regular audio files by using my own audio element, not just streams.
So, maybe I could not give you better advice at this moment than recreate the IcecastMetadataPlayer object, but maybe the creator of this plugin could give better hints.

from icecast-metadata-js.

eshaz avatar eshaz commented on July 30, 2024

you basically instantiate a new player whenever you want to play another source?

Yes, to change the source you need to create a new instance with the new endpoint passed into the constructor. Once the instance is constructed, there's intentionally no way to change the source URL. There is internal state in the player that is set up when the first call to play() happens that makes it impossible to change the source URL in the current design. Changing the audio element source won't work and might cause unexpected behavior / errors.

It says that detachAudioElement is only necessary if you've passed your own audio element for use with the player.

Yes, this is correct. This method is only needed when you want to reuse your audio element for something else after you're done with the IcecastMetadataPlayer instance.

Here's a code example in the demo for this project that does this. After you've called stop() on your instance, you can create a new instance with the new URL, call play(), and let the old instance be garbage collected.

from icecast-metadata-js.

karolyi avatar karolyi commented on July 30, 2024

Thanks for your response reinforcing my thoughts! Closing this issue.

from icecast-metadata-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.