class TestPlayer(unittest.TestCase): def setUp(self): self.player = Player() def test_play(self): self.player.play(Audio('http://mp1.somafm.com:2020')) self.assertTrue(self.player.is_streaming()) def test_play_uri(self): self.player.play_uri('http://mp1.somafm.com:2020', True) self.assertTrue(self.player.is_streaming()) def test_pause(self): self.player.play(Audio('http://mp1.somafm.com:2020')) self.player.pause() self.assertTrue(self.player.is_paused()) def test_resume(self): self.player.play(Audio('http://mp1.somafm.com:2020')) self.player.pause() self.assertTrue(self.player.is_paused()) def test_stop(self): self.player.play(Audio('http://mp1.somafm.com:2020')) self.player.stop() self.assertTrue(self.player.is_stopped()) def test_set_position(self): pass def test_get_position(self): pass def test_get_duration(self): pass def test_get_progress(self): pass def test_set_volume(self): self.player.play(Audio('http://mp1.somafm.com:2020')) self.player.set_volume(10) self.assertEqual(self.player.get_volume(), 10) def test_get_volume(self): self.player.play(Audio('http://mp1.somafm.com:2020')) self.player.set_volume(10) self.assertEqual(self.player.get_volume(), 10)
class Audio(object): """ Note: Title attribute will never be blank """ def __init__(self, uri, **kwargs): self.type = kwargs['type'] if 'type' in kwargs else UNKNOWN self.artist = kwargs['artist'] if 'artist' in kwargs else u'' self.title = kwargs['title'] if 'title' in kwargs else u'' self.album = kwargs['album'] if 'album' in kwargs else u'' self.track = int(kwargs['track']) if 'track' in kwargs else -1 self.length = int(kwargs['length']) if 'length' in kwargs else -1 self.year = kwargs['year'] if 'year' in kwargs else u'' if self.type == UNKNOWN: self.type = uri_type(uri) self.uri = unicode(uri) if self.is_file(): if self.uri.find(os.sep): self.uri = unicode(os.path.abspath(self.uri)) if not kwargs: # analyze the track ourselves if no info was given info = audio_info(self.uri) if info is None: raise Exception("Not an audio file") self.update(**info) elif self.is_stream(): if not self.title: self.title = "Stream: %s" % self.uri self.player = None def update(self, **kwargs): if 'length' in kwargs: self.length = kwargs['length'] if 'track' in kwargs: self.track = kwargs['track'] if 'year' in kwargs: self.year = kwargs['year'] if 'artist' in kwargs: self.artist = kwargs['artist'] if 'title' in kwargs: self.title = kwargs['title'] if 'album' in kwargs: self.album = kwargs['album'] # FIXME: the on_finish function should only # be part of the player until the audio # file is done playing, and not remain # on the player (as it does now) def play(self, player=None, rate=None, on_finish=None): if self.player is not None: self.stop() if player is None: from pyap.player import Player self.player = Player(on_finish=on_finish) else: self.player = player if on_finish is not None: self.player.connect('audio_finished', on_finish) def cleanup_player(audio): audio.stop() self.player.connect('audio_finished', cleanup_player) self.player.play(self, rate=rate) def pause(self): if self.player is not None: self.player.pause() def resume(self): if self.player is not None: self.player.resume() def stop(self): if self.player is not None: self.player.stop() self.player = None def is_playing(self): if self.player is None: return False return self.player.is_playing() def is_streaming(self): if self.player is None: return False return self.player.is_streaming() def is_paused(self): if self.player is None: return False return self.player.is_paused() def is_stopped(self): if self.player is None: return True return self.player.is_stopped() def is_file(self): return self.type == FILE def is_stream(self): return self.type == STREAM # TODO: handle case where band name begins with "The" or other irrelevant words def __cmp__(self, audio): if self.artist and audio.artist: if self.artist == audio.artist: if self.album and audio.album: if self.album == audio.album: if self.track != -1 and audio.track != -1: return cmp(self.track, audio.track) else: return cmp(self.title, audio.title) else: if self.year and audio.year: return cmp(self.year, audio.year) else: return cmp(self.album, audio.album) else: return cmp(self.title, audio.title) else: return cmp(self.artist, audio.artist) return cmp(self.title, audio.title) def __str__(self): if self.artist: return u"%s - %s" % (self.artist, self.title) return self.title def __repr__(self): id = "" if hasattr(self, 'id'): id = str(self.id) return u"<Audio('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')>" % ( id, "Stream" if self.is_stream() else "File", self.uri, self.artist, self.title, self.album, str(self.track), str(self.length), self.year )