# \[Extension development\] How to advance to nth track

**URL:** <https://discourse.mopidy.com/t/extension-development-how-to-advance-to-nth-track/3997>\
**Category:** Misc\
**Created:** [May 10, 2020, 8:37pm UTC](https://discourse.mopidy.com/t/extension-development-how-to-advance-to-nth-track/3997 "2020-05-10T20:37:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluepuma77](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/bluepuma77/32/1476_2.png) [@bluepuma77](https://discourse.mopidy.com/u/bluepuma77)\
**Post date:** [May 10, 2020, 8:37pm UTC](https://discourse.mopidy.com/t/extension-development-how-to-advance-to-nth-track/3997/1 "2020-05-10T20:37:11Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/bluepuma77/32/1476_2.png) [@bluepuma77](https://discourse.mopidy.com/u/bluepuma77)\
**Post date:** [May 10, 2020, 9:42pm UTC](https://discourse.mopidy.com/t/extension-development-how-to-advance-to-nth-track/3997/2 "2020-05-10T21:42:35Z")

</div>

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()
```

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/bluepuma77/32/1476_2.png) [@bluepuma77](https://discourse.mopidy.com/u/bluepuma77)\
**Post date:** [May 11, 2020, 10:47am UTC](https://discourse.mopidy.com/t/extension-development-how-to-advance-to-nth-track/3997/3 "2020-05-11T10:47:24Z")

</div>

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
