Changing whitespace and fixing a bug with reconfiguring
[nqaap] / src / opt / Nqa-Audiobook-player / SimpleOSSOPlayer.py
1 import os
2 import logging
3
4 import dbus
5
6 import gtk_toolbox
7
8
9 _moduleLogger = logging.getLogger(__name__)
10
11
12 class SimplePlayer(object):
13
14         SERVICE_NAME             = "com.nokia.osso_media_server"
15         OBJECT_PATH               = "/com/nokia/osso_media_server"
16         AUDIO_INTERFACE_NAME = "com.nokia.osso_media_server.music"
17
18         def __init__(self, on_playing_done = None):
19                 #Fields
20                 self.has_file = False
21                 self.playing = False
22                 self.__elapsed = 0
23
24                 #Event callbacks
25                 self.on_playing_done = on_playing_done
26
27                 session_bus = dbus.SessionBus()
28
29                 # Get the osso-media-player proxy object
30                 oms_object = session_bus.get_object(
31                         self.SERVICE_NAME,
32                         self.OBJECT_PATH,
33                         introspect=False,
34                         follow_name_owner_changes=True,
35                 )
36                 # Use the audio interface
37                 oms_audio_interface = dbus.Interface(
38                         oms_object,
39                         self.AUDIO_INTERFACE_NAME,
40                 )
41                 self._audioProxy = oms_audio_interface
42
43                 self._audioProxy.connect_to_signal("state_changed", self._on_state_changed)
44                 self._audioProxy.connect_to_signal("end_of_stream", self._on_end_of_stream)
45
46                 error_signals = [
47                         "no_media_selected",
48                         "file_not_found",
49                         "type_not_found",
50                         "unsupported_type",
51                         "gstreamer",
52                         "dsp",
53                         "device_unavailable",
54                         "corrupted_file",
55                         "out_of_memory",
56                         "audio_codec_not_supported",
57                 ]
58                 for error in error_signals:
59                         self._audioProxy.connect_to_signal(error, self._on_error)
60
61         @gtk_toolbox.log_exception(_moduleLogger)
62         def _on_error(self, *args):
63                 self.playing = False
64
65         @gtk_toolbox.log_exception(_moduleLogger)
66         def _on_end_of_stream(self, *args):
67                 self.playing = False
68                 if self.on_playing_done is not None: # event callback
69                         self.on_playing_done(self)
70
71         @gtk_toolbox.log_exception(_moduleLogger)
72         def _on_state_changed(self, state):
73                 _moduleLogger.info("State: %s", state)
74
75         def set_file(self, file):
76                 _moduleLogger.info("set file: %s", file)
77                 if os.path.isfile(file):
78                         if self.playing:
79                                 self.stop()
80
81                         uri = "file://" + file
82                         self._audioProxy.set_media_location(uri)
83                         self.has_file = True
84                 else:
85                         _moduleLogger.error("File: %s not found" % file)
86
87         def play(self):
88                 _moduleLogger.info("Started playing")
89                 self._audioProxy.play()
90                 self.playing = True
91
92         def stop(self):
93                 self._audioProxy.stop()
94                 self.playing = False
95                 _moduleLogger.info("Stopped playing")
96
97         def elapsed(self):
98                 pos_info = self._audioProxy.get_position()
99                 if isinstance(pos_info, tuple):
100                         pos, _ = pos_info
101                         return pos
102                 else:
103                         return 0
104
105         def duration(self):
106                 pos_info = self._audioProxy.get_position()
107                 if isinstance(pos_info, tuple):
108                         _, dur = pos_info
109                         return dur
110                 else:
111                         return 0
112
113         def seek_time(self, ns):
114                 _moduleLogger.debug("Seeking to: %s", ns)
115                 self._audioProxy.seek( dbus.Int32(1), dbus.Int32(ns) )