/
TAmarokControls.py
144 lines (118 loc) · 4.47 KB
/
TAmarokControls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# ---
# Copyright (C) 2009, Thomas Eichinger <thomas.eichinger1@gmail.com>
#
# http://wiki.xmms2.xmms.se/wiki/MPRIS
#
import dbus
class TAmarokControls( ):
def __init__( self ):
bus = dbus.SessionBus()
try:
app_proxy = bus.get_object( 'org.kde.amarok', '/' )
self.app = dbus.Interface( app_proxy, 'org.freedesktop.MediaPlayer' )
player_proxy = bus.get_object( 'org.kde.amarok', '/Player' )
self.player = dbus.Interface( player_proxy, 'org.freedesktop.MediaPlayer' )
tList_proxy = bus.get_object( 'org.kde.amarok', '/TrackList')
self.trackList = dbus.Interface( tList_proxy, 'org.freedesktop.MediaPlayer' )
except dbus.exceptions.DBusException:
import sys
from PyKDE4.kdecore import ki18n, KAboutData, KCmdLineArgs
from PyKDE4.kdeui import KMainWindow, KMessageBox, KApplication
about = KAboutData("msgbox", "msgbox", ki18n ("TAmarok"), "0.1", ki18n(""), KAboutData.License_GPL,
ki18n ( "(c) 2009 Thomas Eichinger" ), ki18n(""), "", "")
KCmdLineArgs.init(sys.argv, about)
app = KApplication()
win = KMainWindow()
if KMessageBox.warningYesNo(win, "Oops, found no Amarok instance.\n Start Amarok now?" ) == 3 :
import subprocess
self.out = ""
subprocess.Popen("amarok")
import time
time.sleep(3)
app_proxy = bus.get_object( 'org.kde.amarok', '/' )
self.app = dbus.Interface( app_proxy, 'org.freedesktop.MediaPlayer' )
player_proxy = bus.get_object( 'org.kde.amarok', '/Player' )
self.player = dbus.Interface( player_proxy, 'org.freedesktop.MediaPlayer' )
tList_proxy = bus.get_object( 'org.kde.amarok', '/TrackList')
self.trackList = dbus.Interface( tList_proxy, 'org.freedesktop.MediaPlayer' )
else:
print "*E* Sorry no Amarok running"
quit()
def getPlayerIdentity( self ):
self.app.Identity()
def quit( self ):
self.app.Quit()
##################################
## Player manipulation ##
##################################
def triggerPlay( self ):
self.player.Play()
def triggerStop( self ):
self.player.Stop()
def triggerNext( self ):
self.player.Next()
def triggerPrevious( self ):
self.player.Prev()
def togglePlayPause( self ):
status = self.player.GetStatus()
if int(status[0]) is 1:
self.player.Play()
elif int(status[0]) is 0:
self.player.Pause()
def increaseVol( self, i=5 ):
now = self.player.VolumeGet()
self.setVolume( int(now)+i )
def decreaseVol( self, i=5 ):
self.setVolume( int(self.player.VolumeGet())-i )
def setVolume( self, i ):
self.player.VolumeSet( int(i) )
# The unit for position manipulation is ms
# example:
# amarok.increasePos( 1000 ) => forward for 1 sec
# amarok.decreasePos( 10000 ) => rewind for 10 sec
# amarok.setPosition( 1000*60*3.5 ) => position is 3min30sec ;)
def increasePos( self, p=5 ):
self.setPosition( int(self.player.PositionGet())+p )
def decreasePos( self, p=5 ):
self.setPosition( int(self.player.PositionGet())-p )
def setPosition( self, p ):
self.player.PositionSet( p )
def toggleTrackRepeat( self ):
status = self.player.GetStatus()
now = bool(status[2])
self.player.Repeat( not now )
def getMetaData( self, key='all' ):
data = self.player.GetMetadata()
if key is 'all':
return data
if key is 'album':
return data['album']
if key is 'artist':
return data['artist']
######################################
## TrackList manipulation ##
######################################
def main():
amarok = TAmarokControls()
## an example usage
amarok.increaseVol()
if __name__ == "__main__":
main()