PricingBlog

Prevent simultaneous audio playing

  • toddledoodle-1274915129749672039

    m

    1 year ago

    Hi, I am brand new to toddle and web app dev. I created an repeated item audio playlist with src from Airtable API. How do I prevent the audio to play simultaneously? Thank you for your help!
  • max.kayr-1274932354850488393

    Max

    1 year ago

    Hi! How did you set up your logic and the play functionality? A few screenshots might help 🙂
  • tomthebigtree-1274954908881260565

    Tom Ireland

    1 year ago

    Yeah, as Max mentioned, it would be good to see some screenshots. If you're using the audio element, check the autoplay attribute is not set to true - it's false by default.
  • toddledoodle-1275058381056380959

    m

    1 year ago

    @Tom Ireland yes, autoplay is false. I set it up as a repeated li audio item with the src from my Airtable. I didn't set anything else (or know how to). It shows up fine and it is not auto-playing by default. But if I hit play on one and then another one, the first one should pause automatically for better user experience. Hope that makes sense. Thanks again! 🙂
    1275058380846923837-Screenshot_2024-08-19_at_7.40.51_AM.png
  • tomthebigtree-1275063878014992408

    Tom Ireland

    1 year ago

    I see. Thanks for sharing. You'd need some way to identify what's currently playing and then pause it I think. I'm not sure there are any specific events for audio but it may be possible to do it another way. I've not done much with audio in toddle, so couldn't give any pointers. @Max - You done much with audio and come across this problem?

    🤞someone else in the community might have some advice. If I get chance later and you don't have a solution, I'll see if I can have a play on a test branch myself (pun not intended). 😄
  • toddledoodle-1275071763834863779

    m

    1 year ago

    @Tom Ireland Thank you so much for trying to help. I should mention that my background is web design, and I am trying to build a searchable, sortable, and filterable audio playlist for my client with a database from Airtable. So all these are very new to me, but I am here to learn by building a real project. 🙂 @Max if you have a solution, I'd appreciate your help! 🙂
  • tomthebigtree-1275077397942636634

    Tom Ireland

    1 year ago

    I had a wee look and I think you'd need access to the events as per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio#events and they're not available in the events list just now. You could likely create a custom action if you're comfortable doing so.
  • toddledoodle-1275089378280210596

    m

    1 year ago

    @Tom Ireland thank you for your help! I embedded a script element within the repeated audio element and added javascript below from chat GPT and it totally works, lol. Though I am thankful that I got the outcome I wanted, I am sure this is not the right way to approach it in Toddle environment. Am I correct to say that?

    let currentAudio = null;

    document.querySelectorAll('audio').forEach(audio => {
    audio.addEventListener('play', function () {
    if (currentAudio && currentAudio !== this) {
    currentAudio.pause();
    currentAudio.currentTime = 0;
    }
    currentAudio = this;
    });
    });
  • tomthebigtree-1275110978295693342

    Tom Ireland

    1 year ago

    That's one way to do it, @m . If I'm reading it correctly, all that JS is doing is getting all of the audio elements, checking if currentAudio is not the one playing, pausing what is playing and then playing the one selected. I think. 😄

    In toddle, you'd do something similar e.g. have a variable set to null, then set the value of the variable to be the one that is playing when play is selected, then pause existing audio if a new one is selected and update the variable. But, you need a play event (as per the JS addEventListener) in order to handle that better in toddle. You could do the same thing with custom actions and regain a bit more control over it but what you've done will achieve the same thing - a few ways to skin a cat. Might be worth posting a suggestion for audio-specific events in the undefined channel and link to this discussion for context. Would be good to see more events added for other elements, particularly media. 🙂
  • toddledoodle-1275239875033038991

    m

    1 year ago

    @Tom Ireland I tried but couldn't get it to work, too complex for my level of skill now. I will post the play event in suggestion and update it here if they implemented! Thanks again for your time! 🙂
  • Tod-1275239876232745000

    Tod

    1 year ago

    Great energy @m! Your continuous contribution to the toddle Community just made you advance to Community Level 1!
  • tomthebigtree-1275348215608381542

    Tom Ireland

    1 year ago

    No worries, @m . I'm pleased to here that you have something working in the interim. 🙂