GCAudio.qml 3.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* GCompris - GCAudio.qml
 *
 * Copyright (C) 2014 Johnny Jazeix
 *
 * Authors:
 *   Johnny Jazeix <jazeix@gmail.com>
 *
 *   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; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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, see <http://www.gnu.org/licenses/>.
 */
21
22
23
24
25
import QtQuick 2.0
import QtMultimedia 5.0
import GCompris 1.0

Item {
26
    id: gcaudio
Bruno Coudoin's avatar
Bruno Coudoin committed
27
    property bool muted
28
29

    property alias source: audio.source
30
    property alias errorString: audio.errorString
31
32
    property var playbackState: (audio.error == Audio.NoError) ? 
                                    audio.playbackState : Audio.StoppedState;
33
    property bool autoPlay
34
    property var files: []
35

36
    signal error
37
    signal done
38

Bruno Coudoin's avatar
Bruno Coudoin committed
39
40
41
42
43
44
    // @param file is optional
    function play(file) {

        if(file)
            source = file

45
46
47
48
49
        if(!muted) {
            audio.play()
        }
    }

50
51
52
53
54
    function stop() {
        if(audio.playbackState != Audio.StoppedState)
            audio.stop()
    }

55
    function append(file) {
56
57
        if(audio.playbackState !== Audio.PlayingState
           || audio.status === Audio.EndOfMedia
58
           || audio.status === Audio.NoMedia
59
60
61
           || audio.status === Audio.InvalidMedia) {
            // Setting the source to "" on Linux fix a case where the sound is no more played
            source = ""
62
63
64
65
66
67
68
            source = file
            play()
        } else {
            files.push(file)
        }
    }

Bruno Coudoin's avatar
Bruno Coudoin committed
69
70
71
72
73
    // Add the given duration in ms before the play of the next file
    function silence(duration_ms) {
        silenceTimer.interval = duration_ms
    }

74
75
76
77
78
79
    function clearQueue() {
        while(files.length > 0) {
            files.pop();
        }
    }

Bruno Coudoin's avatar
Bruno Coudoin committed
80
81
82
    function _playNextFile() {
        var nextFile = files.shift()
        if(nextFile) {
83
            audio.source = ""
Bruno Coudoin's avatar
Bruno Coudoin committed
84
85
            audio.source = nextFile
            audio.play()
86
        } else {
87
            audio.source = ""
Bruno Coudoin's avatar
Bruno Coudoin committed
88
            gcaudio.done()
89
        }
Bruno Coudoin's avatar
Bruno Coudoin committed
90
91
    }

92
93
    Audio {
        id: audio
94
        autoPlay: gcaudio.autoPlay && !gcaudio.muted
95
        onError: {
96
97
            // This file cannot be played, remove it from the source asap
            source = ""
Bruno Coudoin's avatar
Bruno Coudoin committed
98
99
100
101
            if(files.length)
                _playNextFile()
            else
                gcaudio.error()
102
        }
103
        onStopped: {
Bruno Coudoin's avatar
Bruno Coudoin committed
104
105
106
107
            if(silenceTimer.interval)
                silenceTimer.start()
            else
                _playNextFile()
108
        }
109
    }
Bruno Coudoin's avatar
Bruno Coudoin committed
110
111
112
113
114
115
116
117
118
119

    Timer {
        id: silenceTimer
        repeat: false
        onTriggered: {
            interval = 0
            _playNextFile()
        }
    }

120
}