openlp/openlp/core/ui/media/phononplayer.py

269 lines
10 KiB
Python
Raw Normal View History

2011-04-18 19:50:12 +00:00
# -*- coding: utf-8 -*-
2012-12-11 19:55:47 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2011-04-18 19:50:12 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2013-12-24 08:56:50 +00:00
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
2011-04-18 19:50:12 +00:00
# --------------------------------------------------------------------------- #
# 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; version 2 of the License. #
# #
# 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., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`~openlp.core.ui.media.phononplayer` contains the Phonon player component.
"""
2011-05-21 08:18:52 +00:00
import logging
2011-05-23 19:37:44 +00:00
import mimetypes
2011-05-21 08:18:52 +00:00
from datetime import datetime
2012-12-30 08:33:42 +00:00
from PyQt4 import QtGui
2011-04-18 19:50:12 +00:00
from PyQt4.phonon import Phonon
2013-10-13 20:36:42 +00:00
from openlp.core.common import Settings
from openlp.core.lib import translate
2012-10-04 17:28:49 +00:00
2011-11-11 16:45:25 +00:00
from openlp.core.ui.media import MediaState
2012-09-09 06:06:44 +00:00
from openlp.core.ui.media.mediaplayer import MediaPlayer
2011-04-18 19:50:12 +00:00
2011-05-21 08:18:52 +00:00
log = logging.getLogger(__name__)
2011-11-02 20:27:53 +00:00
ADDITIONAL_EXT = {
2013-09-05 22:41:24 +00:00
'audio/ac3': ['.ac3'],
'audio/flac': ['.flac'],
'audio/x-m4a': ['.m4a'],
'audio/midi': ['.mid', '.midi'],
'audio/x-mp3': ['.mp3'],
'audio/mpeg': ['.mp3', '.mp2', '.mpga', '.mpega', '.m4a'],
'audio/qcelp': ['.qcp'],
'audio/x-wma': ['.wma'],
'audio/x-ms-wma': ['.wma'],
'video/x-flv': ['.flv'],
'video/x-matroska': ['.mpv', '.mkv'],
'video/x-wmv': ['.wmv'],
'video/x-mpg': ['.mpg'],
'video/mpeg': ['.mp4', '.mts', '.mov'],
'video/x-ms-wmv': ['.wmv']
2012-10-04 17:28:49 +00:00
}
2011-11-02 20:27:53 +00:00
2011-11-11 16:45:25 +00:00
class PhononPlayer(MediaPlayer):
2011-11-02 20:27:53 +00:00
"""
A specialised version of the MediaPlayer class, which provides a Phonon
2011-11-02 20:27:53 +00:00
display.
"""
def __init__(self, parent):
"""
Constructor
"""
2013-08-31 18:17:38 +00:00
super(PhononPlayer, self).__init__(parent, 'phonon')
self.original_name = 'Phonon'
self.display_name = '&Phonon'
2011-11-02 20:27:53 +00:00
self.parent = parent
self.additional_extensions = ADDITIONAL_EXT
2011-05-23 19:37:44 +00:00
mimetypes.init()
2013-12-31 20:29:03 +00:00
for mime_type in Phonon.BackendCapabilities.availableMimeTypes():
mime_type = str(mime_type)
if mime_type.startswith('audio/'):
self._addToList(self.audio_extensions_list, mime_type)
elif mime_type.startswith('video/'):
self._addToList(self.video_extensions_list, mime_type)
2011-05-10 20:39:17 +00:00
def _addToList(self, mimetype_list, mimetype):
"""
Add mimetypes to the provided list
"""
2011-05-23 19:37:44 +00:00
# Add all extensions which mimetypes provides us for supported types.
2013-08-31 18:17:38 +00:00
extensions = mimetypes.guess_all_extensions(str(mimetype))
2011-05-23 19:37:44 +00:00
for extension in extensions:
2013-08-31 18:17:38 +00:00
ext = '*%s' % extension
if ext not in mimetype_list:
mimetype_list.append(ext)
2013-08-31 18:17:38 +00:00
log.info('MediaPlugin: %s extensions: %s' % (mimetype, ' '.join(extensions)))
2011-05-23 19:37:44 +00:00
# Add extensions for this mimetype from self.additional_extensions.
# This hack clears mimetypes' and operating system's shortcomings
# by providing possibly missing extensions.
2013-08-31 18:17:38 +00:00
if mimetype in list(self.additional_extensions.keys()):
2011-05-23 19:37:44 +00:00
for extension in self.additional_extensions[mimetype]:
2013-08-31 18:17:38 +00:00
ext = '*%s' % extension
if ext not in mimetype_list:
mimetype_list.append(ext)
2013-08-31 18:17:38 +00:00
log.info('MediaPlugin: %s additional extensions: %s' %
(mimetype, ' '.join(self.additional_extensions[mimetype])))
2011-05-23 19:37:44 +00:00
2011-06-05 21:10:49 +00:00
def setup(self, display):
"""
Set up the player widgets
"""
2013-03-06 22:54:16 +00:00
display.phonon_widget = Phonon.VideoWidget(display)
display.phonon_widget.resize(display.size())
display.media_object = Phonon.MediaObject(display)
2013-03-06 22:54:16 +00:00
Phonon.createPath(display.media_object, display.phonon_widget)
if display.has_audio:
display.audio = Phonon.AudioOutput(Phonon.VideoCategory, display.media_object)
Phonon.createPath(display.media_object, display.audio)
2013-03-06 22:54:16 +00:00
display.phonon_widget.raise_()
display.phonon_widget.hide()
self.has_own_widget = True
2011-05-17 19:50:00 +00:00
2011-06-08 13:18:05 +00:00
def check_available(self):
"""
Check if the player is available
"""
2011-05-17 19:50:00 +00:00
return True
2011-06-05 21:10:49 +00:00
def load(self, display):
"""
Load a video into the display
"""
2013-08-31 18:17:38 +00:00
log.debug('load vid in Phonon Controller')
2011-06-06 19:12:14 +00:00
controller = display.controller
2011-06-05 21:10:49 +00:00
volume = controller.media_info.volume
path = controller.media_info.file_info.absoluteFilePath()
display.media_object.setCurrentSource(Phonon.MediaSource(path))
2011-08-29 19:55:58 +00:00
if not self.media_state_wait(display, Phonon.StoppedState):
2011-05-21 08:18:52 +00:00
return False
2011-06-14 16:10:20 +00:00
self.volume(display, volume)
2011-05-21 08:18:52 +00:00
return True
2013-12-31 20:29:03 +00:00
def media_state_wait(self, display, media_state):
2011-05-21 08:18:52 +00:00
"""
Wait for the video to change its state
Wait no longer than 5 seconds.
"""
start = datetime.now()
current_state = display.media_object.state()
2013-12-31 20:29:03 +00:00
while current_state != media_state:
current_state = display.media_object.state()
2011-08-29 19:55:58 +00:00
if current_state == Phonon.ErrorState:
2011-05-21 08:18:52 +00:00
return False
2013-02-03 19:23:12 +00:00
self.application.process_events()
2011-05-21 08:18:52 +00:00
if (datetime.now() - start).seconds > 5:
return False
return True
2011-04-18 19:50:12 +00:00
2011-06-05 21:10:49 +00:00
def resize(self, display):
"""
Resize the display
"""
2013-03-06 22:54:16 +00:00
display.phonon_widget.resize(display.size())
2011-04-18 19:50:12 +00:00
def play(self, display):
"""
Play the current media item
"""
2011-06-06 19:12:14 +00:00
controller = display.controller
2011-07-25 20:56:39 +00:00
start_time = 0
2013-12-31 20:29:03 +00:00
if display.media_object.state() != Phonon.PausedState and controller.media_info.start_time > 0:
2011-07-25 20:56:39 +00:00
start_time = controller.media_info.start_time
display.media_object.play()
2012-06-09 15:46:01 +00:00
if not self.media_state_wait(display, Phonon.PlayingState):
2011-08-29 19:55:58 +00:00
return False
2012-06-09 15:46:01 +00:00
if start_time > 0:
self.seek(display, controller.media_info.start_time * 1000)
self.volume(display, controller.media_info.volume)
controller.media_info.length = int(display.media_object.totalTime() / 1000)
2013-03-23 07:28:24 +00:00
controller.seek_slider.setMaximum(controller.media_info.length * 1000)
2012-06-09 15:46:01 +00:00
self.state = MediaState.Playing
2013-03-06 22:54:16 +00:00
display.phonon_widget.raise_()
2012-06-09 15:46:01 +00:00
return True
2011-04-18 19:50:12 +00:00
def pause(self, display):
"""
Pause the current media item
"""
display.media_object.pause()
2011-08-29 19:55:58 +00:00
if self.media_state_wait(display, Phonon.PausedState):
self.state = MediaState.Paused
2011-04-18 19:50:12 +00:00
def stop(self, display):
"""
Stop the current media item
"""
display.media_object.stop()
2011-06-06 19:12:14 +00:00
self.set_visible(display, False)
2011-05-10 20:39:17 +00:00
self.state = MediaState.Stopped
def volume(self, display, vol):
"""
Set the volume
"""
2011-06-05 21:10:49 +00:00
# 1.0 is the highest value
2013-03-06 22:54:16 +00:00
if display.has_audio:
2011-06-14 16:10:20 +00:00
vol = float(vol) / float(100)
display.audio.setVolume(vol)
2011-04-18 19:50:12 +00:00
2013-03-23 07:28:24 +00:00
def seek(self, display, seek_value):
"""
Go to a particular point in the current media item
"""
2013-03-23 07:28:24 +00:00
display.media_object.seek(seek_value)
2011-04-18 19:50:12 +00:00
def reset(self, display):
"""
Reset the media player
"""
display.media_object.stop()
display.media_object.clearQueue()
2011-07-10 21:43:07 +00:00
self.set_visible(display, False)
2013-03-06 22:54:16 +00:00
display.phonon_widget.setVisible(False)
2011-05-17 19:50:00 +00:00
self.state = MediaState.Off
def set_visible(self, display, status):
"""
Set the visibility of the widget
"""
2013-03-06 22:54:16 +00:00
if self.has_own_widget:
display.phonon_widget.setVisible(status)
2011-05-10 20:39:17 +00:00
2011-06-05 21:10:49 +00:00
def update_ui(self, display):
"""
Update the UI
"""
if display.media_object.state() == Phonon.PausedState and self.state != MediaState.Paused:
self.stop(display)
2011-06-06 19:12:14 +00:00
controller = display.controller
2011-06-05 21:10:49 +00:00
if controller.media_info.end_time > 0:
if display.media_object.currentTime() > controller.media_info.end_time * 1000:
2011-05-24 20:03:57 +00:00
self.stop(display)
2011-06-05 21:10:49 +00:00
self.set_visible(display, False)
2013-03-23 07:45:08 +00:00
if not controller.seek_slider.isSliderDown():
2013-03-23 07:28:24 +00:00
controller.seek_slider.blockSignals(True)
controller.seek_slider.setSliderPosition(display.media_object.currentTime())
controller.seek_slider.blockSignals(False)
2012-10-04 17:28:49 +00:00
def get_media_display_css(self):
"""
Add css style sheets to htmlbuilder
"""
2013-09-05 22:41:24 +00:00
return ''
2012-10-15 17:35:14 +00:00
def get_info(self):
"""
Return some info about this player
"""
2012-10-15 17:35:14 +00:00
return(translate('Media.player', 'Phonon is a media player which '
2013-12-31 20:29:03 +00:00
'interacts with the operating system to provide media capabilities.') +
'<br/> <strong>' + translate('Media.player', 'Audio') +
'</strong><br/>' + str(self.audio_extensions_list) +
'<br/><strong>' + translate('Media.player', 'Video') +
'</strong><br/>' + str(self.video_extensions_list) + '<br/>')