How to play the note's sound while selecting it by the plugin?

• Dec 24, 2021 - 09:27

While user selecting a note by mouse or keyboard, the MuseScore plays that note's sound.
However, if it was selected by a plugin using something like curScore.selection.select(cursor.element.notes[0]), it does select the note but doesn't play the sound.
I tried some workarounds by using some commands in cmd() but haven't got any results so far.
Anyone got any ideas to achieve this?


Comments

Sorry, my previous comment doesnt work, it playback notes only during creating them.

But for select playback, commad cmd("next-chord") should work (it is like navigating in score)

In reply to by sammik

Thank you so much!!
Here is my final solution using your cmd("next-chord") suggestion and it worked very well and can handle any situations:

function playCursor(cursor) //plays the note's sound at current cursor position, Special Thanks to Sammik's idea of using cmd("prev-chord") & cmd("next-chord") as workarounds : https://musescore.org/en/node/327715
    {
        cursor.prev();
        if(cursor.element == null) //if cursor is at the first note of the score
        {
            cmd("prev-chord");
            cursor.next(); //restores cursor's position
            return;
        } 
        if(cursor.element.type != 93) // if previous element is a rest
        {
            cmd("prev-chord");
            cmd("next-chord");
            cursor.next(); 
            return;
        }
        //if previous element is a note, in order to prevent playing the sound of previous note, use cursor to select that note.
        curScore.selection.select(cursor.element.notes[0]);
        cmd("next-chord");
        cursor.next(); 
        return;
    }

Do you still have an unanswered question? Please log in first to post your question.