Qt 4.8
avmediaobject.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "avmediaobject.h"
43 #include "avaudiooutput.h"
44 
45 #include <QtCore/QUrl>
46 #import <Foundation/NSString.h>
47 #import <Foundation/NSURL.h>
48 #import <AVFoundation/AVAudioPlayer.h>
49 
50 @interface AudioPlayerDelegate : NSObject <AVAudioPlayerDelegate> {
52 }
53 
54 - (id)initWithMediaObject:(AVMediaObject *)obj;
55 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
56 
57 @end
58 
59 @implementation AudioPlayerDelegate
60 
61 - (id)initWithMediaObject:(AVMediaObject *)obj
62 {
63  if ((self = [self init])) {
64  mediaObject = obj;
65  }
66  return self;
67 }
68 
69 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
70 {
71  Q_UNUSED(flag)
72  Q_UNUSED(player)
74 }
75 
76 @end
77 
79 public:
81  : player(0)
82  {
83  delegate = [[AudioPlayerDelegate alloc] initWithMediaObject:parent];
84  }
86  {
87  [delegate release];
88  }
89 
90  AVAudioPlayer *player;
92 };
93 
95  : QObject(parent),
96  m_tickInterval(0),
97  m_state(Phonon::LoadingState),
98  m_errorType(Phonon::NoError),
99  m_output(0),
100  d(new AVMediaObjectPrivate(this))
101 {
102 }
103 
105 {
106  if (m_state == Phonon::PlayingState)
107  stop();
108  [d->player release];
109  delete d;
110 }
111 
113 {
114  if (m_state == state)
115  return;
116  Phonon::State oldState = m_state;
117  m_state = state;
118  emit stateChanged(m_state, oldState);
119 }
120 
122 {
123  if (!d->player) {
124  m_errorType = Phonon::NormalError;
125  m_errorString = tr("Media source has not been set.");
126  return false;
127  }
128  return true;
129 }
130 
132 {
133  if (!checkPlayer())
134  return;
135  if (![d->player play]) {
136  m_errorType = Phonon::NormalError;
137  m_errorString = tr("Failed to play media source.");
138  return;
139  }
140  changeState(Phonon::PlayingState);
141 }
142 
144 {
145  if (!checkPlayer())
146  return;
147  [d->player pause];
148  changeState(Phonon::PausedState);
149 }
150 
152 {
153  if (!checkPlayer())
154  return;
155  [d->player stop];
156  d->player.currentTime = 0;
158 }
159 
160 void AVMediaObject::seek(qint64 milliseconds)
161 {
162  if (!checkPlayer())
163  return;
164  d->player.currentTime = milliseconds/1000.;
165 }
166 
168 {
169  return m_tickInterval;
170 }
171 
173 {
174  m_tickInterval = newTickInterval;
175  //TODO
176 }
177 
179 {
180  return false;
181 }
182 
184 {
185  return true;
186 }
187 
189 {
190  if (!checkPlayer())
191  return 0;
192  return (qint64)(d->player.currentTime * 1000);
193 }
194 
196 {
197  return m_state;
198 }
199 
201 {
202  return m_errorString;
203 }
204 
205 Phonon::ErrorType AVMediaObject::errorType() const
206 {
207  return m_errorType;
208 }
209 
211 {
212  if (!checkPlayer())
213  return 0;
214  return d->player.duration;
215 }
216 
217 Phonon::MediaSource AVMediaObject::source() const
218 {
219  return m_mediaSource;
220 }
221 
222 void AVMediaObject::setSource(const Phonon::MediaSource &source)
223 {
224  if (d->player) {
225  stop();
226  [d->player release];
227  d->player = 0;
228  }
230  NSString *urlString = [NSString stringWithCString:source.url().toEncoded().constData()
231  encoding:NSASCIIStringEncoding];
232  NSURL *url = [NSURL URLWithString:urlString];
233  d->player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
234  if (!d->player) {
235  m_errorString = tr("Failed to create player for '%1'").arg(source.url().toString());
236  changeState(Phonon::ErrorState);
237  return;
238  }
239  d->player.delegate = d->delegate;
240  [d->player prepareToPlay];
243  emit totalTimeChanged((qint64)(d->player.duration * 1000));
244 }
245 
246 void AVMediaObject::setNextSource(const Phonon::MediaSource &source)
247 {
248  setSource(source);
249 }
250 
252 {
253  // not implemented
254  return 0;
255 }
256 
258 {
259  // not implemented
260 }
261 
263 {
264  // not implemented
265  return 0;
266 }
267 
269 {
270  // not implemented
271 }
272 
274 {
275  if (m_output) {
276  disconnect(m_output, SIGNAL(volumeChanged(qreal)), this, SLOT(setVolume(qreal)));
277  }
278  m_output = audioOutput;
279  if (m_output) {
280  connect(m_output, SIGNAL(volumeChanged(qreal)), this, SLOT(setVolume(qreal)));
282  }
283 }
284 
286 {
287  if (!d->player) // do nothing, will be set when player is created
288  return;
289  [d->player setVolume:qMin((float)1.0, (float)newVolume)];
290 }
291 
293 {
296  emit finished();
297 }
qint32 transitionTime() const
double d
Definition: qnumeric_p.h:62
AVAudioOutput * m_output
double qreal
Definition: qglobal.h:1193
QString m_errorString
bool hasVideo() const
int qint32
Definition: qglobal.h:937
void setVolume(qreal newVolume)
Phonon::MediaSource m_mediaSource
qint64 currentTime() const
qint32 m_tickInterval
#define SLOT(a)
Definition: qobjectdefs.h:226
AVMediaObjectPrivate * d
void changeState(Phonon::State state)
qint64 totalTime() const
AudioPlayerDelegate * delegate
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void currentSourceChanged(const MediaSource &)
void setSource(const Phonon::MediaSource &source)
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool checkPlayer() const
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void seek(qint64 milliseconds)
void setNextSource(const Phonon::MediaSource &source)
void handlePlayerFinished()
Phonon::State m_state
Phonon::ErrorType m_errorType
void totalTimeChanged(qint64 length)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
qreal volume() const
void setPrefinishMark(qint32 newPrefinishMark)
void stateChanged(Phonon::State newstate, Phonon::State oldstate)
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
static bool init
#define emit
Definition: qobjectdefs.h:76
__int64 qint64
Definition: qglobal.h:942
Phonon::State state() const
QString errorString() const
AVMediaObject(QObject *parent)
AVMediaObject * mediaObject
void setTickInterval(qint32 newTickInterval)
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void setAudioOutput(AVAudioOutput *audioOutput)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
Phonon::ErrorType errorType() const
State
Definition: qaudio.h:59
AVMediaObjectPrivate(AVMediaObject *parent)
qint32 tickInterval() const
AVAudioPlayer * player
void aboutToFinish()
bool isSeekable() const
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
void setTransitionTime(qint32)
qint32 prefinishMark() const
Phonon::MediaSource source() const