Add basic behavior to play, show catalog and more basic feature
This commit is contained in:
1 parent
df847becc8
commit
8e6392a84c
17 files changed
+1455
-34
No files matched your search
@@ -0,0 +1,162 @@
|
||||
import QtQuick 2.5
|
||||
|
||||
// Surface vidéo avec une interface unique, quel que soit le lecteur disponible :
|
||||
// - "fbx" : VideoPlayer de libfbxqml (fbx.media) — absent du Freebox Player testé,
|
||||
// conservé en premier choix au cas où, mais jamais vérifié ;
|
||||
// - "qtmultimedia" : Video de QtMultimedia — c'est lui qui lit sur la box (HLS, reprise,
|
||||
// pause et déplacement vérifiés ; positions en millisecondes).
|
||||
// Les deux sont créés dynamiquement : un module absent ne fait pas échouer le chargement.
|
||||
//
|
||||
// position / duration en millisecondes ; playerState : "idle", "loading", "playing",
|
||||
// "paused", "ended" ou "error". Les états bruts du lecteur sont écrits dans le log.
|
||||
Item {
|
||||
id: root
|
||||
|
||||
property string source: ""
|
||||
property real position: 0
|
||||
property real duration: 0
|
||||
property string playerState: "idle"
|
||||
property string errorString: ""
|
||||
property string backendName: ""
|
||||
signal ended()
|
||||
|
||||
property var _backend: null
|
||||
property string _lastRaw: ""
|
||||
|
||||
function play() {
|
||||
if (_backend)
|
||||
_backend.play();
|
||||
}
|
||||
|
||||
function pause() {
|
||||
if (_backend)
|
||||
_backend.pause();
|
||||
}
|
||||
|
||||
function togglePause() {
|
||||
if (playerState === "playing")
|
||||
pause();
|
||||
else
|
||||
play();
|
||||
}
|
||||
|
||||
function seek(ms) {
|
||||
if (!_backend)
|
||||
return;
|
||||
var target = Math.max(0, duration > 0 ? Math.min(ms, duration - 1000) : ms);
|
||||
console.log("Lecteur : déplacement à " + Math.round(target / 1000) + " s");
|
||||
_backend.seek(target);
|
||||
}
|
||||
|
||||
function stop() {
|
||||
if (!_backend)
|
||||
return;
|
||||
_backend.stop();
|
||||
if (backendName === "fbx" && _backend.close)
|
||||
_backend.close();
|
||||
}
|
||||
|
||||
function _create() {
|
||||
var attempts = [
|
||||
{ name: "fbx", qml: 'import QtQuick 2.5; import fbx.media 1.0; VideoPlayer { anchors.fill: parent }' },
|
||||
{ name: "qtmultimedia", qml: 'import QtQuick 2.5; import QtMultimedia 5.0; '
|
||||
+ 'Video { anchors.fill: parent; fillMode: VideoOutput.PreserveAspectFit }' }
|
||||
];
|
||||
for (var i = 0; i < attempts.length && !_backend; i++) {
|
||||
try {
|
||||
_backend = Qt.createQmlObject(attempts[i].qml, root, "VideoSurface-" + attempts[i].name);
|
||||
backendName = attempts[i].name;
|
||||
} catch (e) {
|
||||
console.log("Lecteur " + attempts[i].name + " indisponible : " + e);
|
||||
}
|
||||
}
|
||||
if (!_backend) {
|
||||
_fail("Aucun lecteur vidéo disponible.");
|
||||
return;
|
||||
}
|
||||
console.log("Lecteur vidéo : " + backendName);
|
||||
if (backendName === "fbx" && _backend.error) {
|
||||
_backend.error.connect(function (message, fatal) {
|
||||
console.log("Lecteur fbx : erreur " + message + (fatal ? " (fatale)" : ""));
|
||||
if (fatal)
|
||||
_fail(String(message));
|
||||
});
|
||||
}
|
||||
if (source !== "")
|
||||
_open();
|
||||
}
|
||||
|
||||
function _open() {
|
||||
errorString = "";
|
||||
playerState = "loading";
|
||||
_backend.source = source;
|
||||
_backend.play();
|
||||
}
|
||||
|
||||
function _fail(message) {
|
||||
errorString = message;
|
||||
playerState = "error";
|
||||
}
|
||||
|
||||
// Lecture périodique de l'état du lecteur (identique pour les deux backends).
|
||||
function _poll() {
|
||||
if (!_backend || playerState === "error")
|
||||
return;
|
||||
var raw;
|
||||
if (backendName === "fbx") {
|
||||
raw = "mediaState=" + _backend.mediaState + " playbackState=" + _backend.playbackState
|
||||
+ " status=" + _backend.status;
|
||||
position = _backend.position || 0;
|
||||
duration = _backend.duration || 0;
|
||||
var media = String(_backend.mediaState);
|
||||
var playback = String(_backend.playbackState);
|
||||
if (media === "ended")
|
||||
_setState("ended");
|
||||
else if (playback === "playing")
|
||||
_setState("playing");
|
||||
else if (playback === "paused")
|
||||
_setState("paused");
|
||||
else
|
||||
_setState("loading");
|
||||
} else {
|
||||
// Valeurs de MediaPlayer : playbackState 1 = lecture, 2 = pause ;
|
||||
// status 7 = fin du média, 8 = média invalide.
|
||||
raw = "status=" + _backend.status + " playbackState=" + _backend.playbackState;
|
||||
position = _backend.position || 0;
|
||||
duration = _backend.duration > 0 ? _backend.duration : 0;
|
||||
if (_backend.status === 8 || (_backend.errorString && _backend.error !== 0))
|
||||
_fail(_backend.errorString || "Média illisible.");
|
||||
else if (_backend.status === 7)
|
||||
_setState("ended");
|
||||
else if (_backend.playbackState === 1)
|
||||
_setState(_backend.status === 4 || _backend.status === 5 ? "loading" : "playing");
|
||||
else if (_backend.playbackState === 2)
|
||||
_setState("paused");
|
||||
}
|
||||
if (raw !== _lastRaw) {
|
||||
_lastRaw = raw;
|
||||
console.log("Lecteur " + backendName + " : " + raw + " position=" + Math.round(position)
|
||||
+ " durée=" + Math.round(duration));
|
||||
}
|
||||
}
|
||||
|
||||
function _setState(state) {
|
||||
if (state === playerState)
|
||||
return;
|
||||
playerState = state;
|
||||
if (state === "ended")
|
||||
ended();
|
||||
}
|
||||
|
||||
onSourceChanged: if (_backend && source !== "") _open()
|
||||
|
||||
Timer {
|
||||
interval: 250
|
||||
repeat: true
|
||||
running: root._backend !== null && root.source !== ""
|
||||
onTriggered: root._poll()
|
||||
}
|
||||
|
||||
Component.onCompleted: _create()
|
||||
Component.onDestruction: stop()
|
||||
}
|
||||
Reference in new issue
Block a user