Playback progress and position handling

This commit is contained in:
Thomas Perl 2014-02-02 15:44:18 +01:00
parent e6869016f2
commit 9001436256
3 changed files with 89 additions and 2 deletions

View file

@ -24,15 +24,84 @@ import QtMultimedia 5.0
MediaPlayer {
id: player
property int episode
property int episode: 0
property var queue: ([])
property bool isPlaying: playbackState == MediaPlayer.PlayingState
property bool inhibitPositionEvents: false
property bool seekAfterPlay: false
property int seekTargetSeconds: 0
property int lastPosition: 0
property int lastDuration: 0
property int playedFrom: 0
function playbackEpisode(episode_id) {
player.episode = episode_id;
if (episode == episode_id) {
// If the episode is already loaded, just start playing
play();
return;
}
// First, make sure we stop any seeking / position update events
sendPositionToCore(lastPosition);
player.inhibitPositionEvents = true;
player.stop();
py.call('main.play_episode', [episode_id], function (episode) {
// Load media / prepare and start playback
player.episode = episode_id;
player.source = episode.source;
player.seekTargetSeconds = episode.position;
seekAfterPlay = true;
player.play();
});
}
function seekAndSync(target_position) {
sendPositionToCore(lastPosition);
seek(target_position);
playedFrom = target_position;
}
onPlaybackStateChanged: {
if (playbackState == MediaPlayer.PlayingState) {
if (seekAfterPlay) {
// A seek was scheduled, execute now that we're playing
player.inhibitPositionEvents = false;
player.seek(seekTargetSeconds * 1000);
player.playedFrom = seekTargetSeconds * 1000;
seekAfterPlay = false;
} else {
player.playedFrom = position;
}
} else {
sendPositionToCore(lastPosition);
}
}
function sendPositionToCore(positionToSend) {
if (episode != 0 && !inhibitPositionEvents) {
var begin = playedFrom / 1000;
var end = positionToSend / 1000;
var duration = ((lastDuration > 0) ? lastDuration : 0) / 1000;
var diff = end - begin;
// Only send playback events if they are 2 seconds or longer
// (all other events might just be seeking events or wrong ones)
if (diff >= 2) {
py.call('main.report_playback_event', [episode, begin, end, duration]);
}
}
}
onPositionChanged: {
if (isPlaying && !inhibitPositionEvents) {
lastPosition = position;
lastDuration = duration;
// Directly update the playback progress in the episode list
py.playbackProgress(episode, position / duration);
}
}
}