Hagsten / Talkify

Javascript Text to speech library
216 stars 32 forks source link

how to properly pause or stop reading after completed reading a block of test? #58

Open danielignatius opened 3 years ago

danielignatius commented 3 years ago

This is a great script. But the playlist.pause(); or the player.pause(); is not firing after the onPause fn is fired. I cant seem to stop or pause from reading the next block of text. Any advice is appreciated.

    var player = new talkify.TtsPlayer()
    .enableTextHighlighting()
    .subscribeTo({
        onSentenceComplete: function () { console.log("onSentenceComplete :)") },
        onBeforeItemPlaying: function () { console.log("onBeforeItemPlaying :)") },
        onItemLoaded: function () { console.log("onItemLoaded :)") },
        onPause: function () {
            player.pause();
            console.log("onPause :)"); 
        },
    })
    .forceVoice({name: "Zira"});

    var playlist = new talkify.playlist()
    .begin()
    .usingPlayer(player)
    .withRootSelector('.browse-wrap')
    .withTextInteraction()
    .subscribeTo({
        onPause: function () {
            playlist.pause();
            player.pause();
            console.log("onPause :))"); 
        },
    })
    .build();

    playlist.play();
Hagsten commented 3 years ago

Hi @danielignatius , the onPause function is a callback that is invoked when the player pauses. It is typically invoked after a call to player.pause()

To pause playback, you can either call the players pause method or the playlists pause method.

danielignatius commented 3 years ago

Hi @Hagsten , thank you for the advice. I had a put a playlist.pause(); or the player.pause(); on the onPause function because it was pausing before reading the next block of text. In this case, can you share with me how to stop the web reader from reading text 2 after reading text 1. See screenshot. Is there an event I can use to catch and pause the playlist or player?

Thanks @Hagsten

Screen Shot 2021-09-01 at 8 45 25 am
Hagsten commented 3 years ago

What is the purpose of pausing in the middle of the playlist?

I would say that you just include whichever paragraphs you need in the playlist by using withElements (https://github.com/Hagsten/Talkify#playlist-fluent-builder) to narrow down your scope. The playlist will always play all its paragraphs until it has played the last. Or keeping your own playlist and just using the player instance which you can control as you want :)

danielignatius commented 3 years ago

@Hagsten Thanks for sharing your insight on how the playlist actually works.

I actually have two sections that I want the text to be read out for my audience. One section on the top (A) and the other section at the other side of the page (B). I don't want B to be read out immediately after A has been read out. Is there a way to gracefully stop B from reading out immediately after A has been read out? It would be great to stop reading B out without the audience clicking on the stop button.

I only want B to be read out if the audience click on the text or sentence.

Your suggestion is appreciated.