Class: Rockbox::Api::Playback

Inherits:
Object
  • Object
show all
Defined in:
lib/rockbox/api/playback.rb

Constant Summary collapse

TRACK_FIELDS =
<<~GQL
  fragment TrackFields on Track {
    id title artist album genre disc trackString yearString
    composer comment albumArtist grouping
    discnum tracknum layer year bitrate frequency
    filesize length elapsed path
    albumId artistId genreId albumArt
  }
GQL

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Playback

Returns a new instance of Playback.



18
19
20
# File 'lib/rockbox/api/playback.rb', line 18

def initialize(http)
  @http = http
end

Instance Method Details

#current_trackRockbox::Track?

Returns:



37
38
39
40
# File 'lib/rockbox/api/playback.rb', line 37

def current_track
  data = @http.execute("#{TRACK_FIELDS}\nquery CurrentTrack { currentTrack { ...TrackFields } }")
  Track.from_hash(data[:current_track])
end

#file_positionInteger

Returns:

  • (Integer)


49
50
51
# File 'lib/rockbox/api/playback.rb', line 49

def file_position
  @http.execute("query FilePosition { getFilePosition }")[:get_file_position]
end

#flush_and_reloadObject



70
# File 'lib/rockbox/api/playback.rb', line 70

def flush_and_reload; @http.execute("mutation FlushReload { flushAndReloadTracks }");       nil; end

#next!Object



67
# File 'lib/rockbox/api/playback.rb', line 67

def next!;            @http.execute("mutation Next { next }");                              nil; end

#next_trackRockbox::Track?

Returns:



43
44
45
46
# File 'lib/rockbox/api/playback.rb', line 43

def next_track
  data = @http.execute("#{TRACK_FIELDS}\nquery NextTrack { nextTrack { ...TrackFields } }")
  Track.from_hash(data[:next_track])
end

#pauseObject



65
# File 'lib/rockbox/api/playback.rb', line 65

def pause;            @http.execute("mutation Pause { pause }");                            nil; end

#play(elapsed: 0, offset: 0) ⇒ Object


Transport controls




57
58
59
60
61
62
63
# File 'lib/rockbox/api/playback.rb', line 57

def play(elapsed: 0, offset: 0)
  @http.execute(
    "mutation Play($elapsed: Long!, $offset: Long!) { play(elapsed: $elapsed, offset: $offset) }",
    { elapsed: elapsed, offset: offset }
  )
  nil
end

#play_album(album_id, shuffle: nil, position: nil) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/rockbox/api/playback.rb', line 93

def play_album(album_id, shuffle: nil, position: nil)
  @http.execute(
    "mutation PlayAlbum($albumId: String!, $shuffle: Boolean, $position: Int) { " \
    "playAlbum(albumId: $albumId, shuffle: $shuffle, position: $position) }",
    { album_id: album_id, shuffle: shuffle, position: position }.compact
  )
  nil
end

#play_all_tracks(shuffle: nil, position: nil) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/rockbox/api/playback.rb', line 138

def play_all_tracks(shuffle: nil, position: nil)
  @http.execute(
    "mutation PlayAllTracks($shuffle: Boolean, $position: Int) { " \
    "playAllTracks(shuffle: $shuffle, position: $position) }",
    { shuffle: shuffle, position: position }.compact
  )
  nil
end

#play_artist(artist_id, shuffle: nil, position: nil) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/rockbox/api/playback.rb', line 102

def play_artist(artist_id, shuffle: nil, position: nil)
  @http.execute(
    "mutation PlayArtist($artistId: String!, $shuffle: Boolean, $position: Int) { " \
    "playArtistTracks(artistId: $artistId, shuffle: $shuffle, position: $position) }",
    { artist_id: artist_id, shuffle: shuffle, position: position }.compact
  )
  nil
end

#play_directory(path, recurse: nil, shuffle: nil, position: nil) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/rockbox/api/playback.rb', line 120

def play_directory(path, recurse: nil, shuffle: nil, position: nil)
  @http.execute(
    "mutation PlayDirectory($path: String!, $recurse: Boolean, $shuffle: Boolean, $position: Int) { " \
    "playDirectory(path: $path, recurse: $recurse, shuffle: $shuffle, position: $position) }",
    { path: path, recurse: recurse, shuffle: shuffle, position: position }.compact
  )
  nil
end

#play_liked_tracks(shuffle: nil, position: nil) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/rockbox/api/playback.rb', line 129

def play_liked_tracks(shuffle: nil, position: nil)
  @http.execute(
    "mutation PlayLikedTracks($shuffle: Boolean, $position: Int) { " \
    "playLikedTracks(shuffle: $shuffle, position: $position) }",
    { shuffle: shuffle, position: position }.compact
  )
  nil
end

#play_playlist(playlist_id, shuffle: nil, position: nil) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/rockbox/api/playback.rb', line 111

def play_playlist(playlist_id, shuffle: nil, position: nil)
  @http.execute(
    "mutation PlayPlaylist($playlistId: String!, $shuffle: Boolean, $position: Int) { " \
    "playPlaylist(playlistId: $playlistId, shuffle: $shuffle, position: $position) }",
    { playlist_id: playlist_id, shuffle: shuffle, position: position }.compact
  )
  nil
end

#play_track(path) ⇒ Object


Single-call play helpers




85
86
87
88
89
90
91
# File 'lib/rockbox/api/playback.rb', line 85

def play_track(path)
  @http.execute(
    "mutation PlayTrack($path: String!) { playTrack(path: $path) }",
    { path: path }
  )
  nil
end

#previous!Object



68
# File 'lib/rockbox/api/playback.rb', line 68

def previous!;        @http.execute("mutation Previous { previous }");                      nil; end

#resumeObject



66
# File 'lib/rockbox/api/playback.rb', line 66

def resume;           @http.execute("mutation Resume { resume }");                          nil; end

#seek(position_ms) ⇒ Object

Parameters:

  • position_ms (Integer)

    absolute target position, in milliseconds.



73
74
75
76
77
78
79
# File 'lib/rockbox/api/playback.rb', line 73

def seek(position_ms)
  @http.execute(
    "mutation Seek($newTime: Int!) { fastForwardRewind(newTime: $newTime) }",
    { new_time: position_ms }
  )
  nil
end

#statusInteger

Returns one of PlaybackStatus constants.

Returns:



27
28
29
# File 'lib/rockbox/api/playback.rb', line 27

def status
  @http.execute("query PlaybackStatus { status }")[:status]
end

#status_nameSymbol

Returns :stopped | :playing | :paused | :unknown.

Returns:

  • (Symbol)

    :stopped | :playing | :paused | :unknown



32
33
34
# File 'lib/rockbox/api/playback.rb', line 32

def status_name
  PlaybackStatus.name(status)
end

#stopObject



69
# File 'lib/rockbox/api/playback.rb', line 69

def stop;             @http.execute("mutation Stop { hardStop }");                          nil; end