File: BasicMediaPlayer.cpp

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (139 lines) | stat: -rwxr-xr-x 4,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/** Copyright (C) 2014, James Reneau.
 **
 **  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 2 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, write to the Free Software Foundation, Inc.,
 **  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **/

#include "BasicMediaPlayer.h"

BasicMediaPlayer::BasicMediaPlayer() {
	mediasleeper = new Sleeper();
}


void BasicMediaPlayer::loadFile(QString file) {
    if(QFileInfo(file).exists()){
        setMedia(QUrl::fromLocalFile(QFileInfo(file).absoluteFilePath()));
    }else{
        setMedia(QUrl::fromUserInput(file));
    }
    waitForSeekable(2000);
}

void BasicMediaPlayer::waitForSeekable(int ms) {
	// wait for isSeekable up to the time in ms
    if(!isSeekable()) {
		QEventLoop loop;
		QTimer timer;
		timer.setSingleShot(true);
		timer.setInterval(ms);
		loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()) );
        loop.connect(this, SIGNAL(seekableChanged(bool)), &loop, SLOT(quit()));
        loop.connect(this, SIGNAL(error(QMediaPlayer::Error)), &loop, SLOT(quit()));
		loop.exec();
	}
}

void BasicMediaPlayer::waitForState(QMediaPlayer::State newstate, int ms) {
	// if player state is not newstate then wait up to ms
	if(state()!=newstate) {
		// thanks http://qt-project.org/forums/viewthread/19869
		QTimer *timer = new QTimer(this);
		timer->start (ms);
		connect (timer, SIGNAL (timeout()), this, SIGNAL (QMediaPlayer::stateChanged()));
	}
}

int BasicMediaPlayer::state() {
    // media state 0-stop, 1-play, 2-pause
    // this eventually needs to be removed and the base class state() should work
	//
	// with QT5.1 it has been observed that
	// some media files do not report stop at the end of the file
	// and continue to report playing forever
	// if playing check of the position increases after a short sleep time
	
	int s;
	qint64 starttime, endtime;
	s = QMediaPlayer::state();
	if (s==QMediaPlayer::PlayingState) {
        starttime = QMediaPlayer::position();
		mediasleeper->sleepRQM(30);
        endtime = QMediaPlayer::position();
		if (starttime==endtime) {
			stop();
			s = QMediaPlayer::StoppedState; // stopped
		}
	}
	return(s);
}

void BasicMediaPlayer::stop() {
	// force stop to reset position at the begining of the file
	// and to totally stop
	mediasleeper->wake();
	setPosition(0);
	QMediaPlayer::stop();
//	waitForState(QMediaPlayer::StoppedState, 1000);
}

void BasicMediaPlayer::wait() {
	waitForSeekable(500);
	// wait for the media file to complete
	if (state()==QMediaPlayer::PlayingState) {
		mediasleeper->sleepMS(QMediaPlayer::duration()-QMediaPlayer::position());
	}
	setPosition(0);
	QMediaPlayer::stop();
}

bool BasicMediaPlayer::seek(double time) {
	long int ms = time * 1000L;
	waitForSeekable(500);
	//qDebug ("mediaStatus %d\n",mediaStatus()); 
	if(isSeekable()) {
		if (ms!=QMediaPlayer::position()) QMediaPlayer::setPosition(ms);
		return true;
	} else {
		return false;
	}
}

double BasicMediaPlayer::length() {
	return QMediaPlayer::duration() / (double)(1000.0);
}

double BasicMediaPlayer::position() {
	return QMediaPlayer::position() / (double)(1000.0);
}

void BasicMediaPlayer::play() {
	QMediaPlayer::play();
// not needed for w7 qt 5.2
// need to check xp vistz ubuntu
//waitForState(QMediaPlayer::PlayingState, 500);
}

void BasicMediaPlayer::pause() {
	QMediaPlayer::pause();
// not needed for w7 qt 5.2
// need to check xp vistz ubuntu
//	waitForState(QMediaPlayer::PausedState, 500);
}

int BasicMediaPlayer::error() {
	return QMediaPlayer::error();
}