[Extension development] How to advance to nth track

Hi all,

as mentioned before, I want to resume previous played albums. I used mpc play n before to start playing the nth track, then seek sec.

I looked at PlaybackController.play(tlid=None), but that doesn’t take an index. It seems the tlid is increased per track added over time. My next idea was to just do multiple PlaybackController.next(). The docs state

If it was paused, it will still be paused

but for me that is not the case. If I do multiple next() on my Raspi Zero mopidy will suddenly start playing. With one sometimes two quick next() it changes state to playing without playing, with more it does start playing music.

Is there an easy solution to advance to the nth track in the tracklist?

Note: I created a shell script to reproduce the strange next() behaviour:

echo clear
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

echo add
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": { "uris": [ "spotify:album:1xpGyKyV26uPstk1Elgp9Q" ] } }' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

sleep 3

echo play
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

sleep 3

echo pause
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.pause"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

sleep 3

echo state
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.get_state"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

echo nexts
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.next"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.next"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.next"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.next"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.next"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

sleep 3

echo state
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.get_state"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

echo time
curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.get_time_position"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
echo

Allright, now i just do an extra call, get the full tracklist and get the n-th element with tlid to play. I discovered another timing issue, I need to wait half a sec before I can seek. But now it works.

def playAlbum(self, uri, index=0, sec=0):
    self.core.tracklist.clear()
    self.core.tracklist.add(uris=[uri])      
    tl = self.core.tracklist.get_tl_tracks().get()
    if tl and len(tl)>index:
        self.core.playback.play(tlid=tl[index].tlid)   
        if sec > 0: 
            time.sleep(0.5)              
            self.core.playback.seek(sec*1000)    
    else:    
        # just play it
        self.core.playback.play()

Overall it would be great to have an option to move forward to nth position in the tracklist without triggering play - to keep the logical order:

  1. Load album
  2. Forward to nth track
  3. Seek to x second
  4. Start playing