AVFoundation: correctly unload current media.

When loading a new media, we should unload the previous media.
This makes sure all properties set right after loading a new media
(e.g. position, volume) are correctly set on the new AVPlayer and
not on the old one.

Change-Id: I4cd71b785ccdb4cd0772cedffc3c25665f402776
Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
This commit is contained in:
Yoann Lopes
2015-09-03 18:42:52 +02:00
parent bede0a838d
commit fdac8d9c74

View File

@@ -121,19 +121,28 @@ static void *AVFMediaPlayerSessionObserverCurrentItemObservationContext = &AVFMe
- (void) unloadMedia - (void) unloadMedia
{ {
if (m_player)
[m_player setRate:0.0];
if (m_playerItem) { if (m_playerItem) {
[m_playerItem removeObserver:self forKeyPath:AVF_STATUS_KEY]; [m_playerItem removeObserver:self forKeyPath:AVF_STATUS_KEY];
[[NSNotificationCenter defaultCenter] removeObserver:self [[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification name:AVPlayerItemDidPlayToEndTimeNotification
object:m_playerItem]; object:m_playerItem];
[[NSNotificationCenter defaultCenter] removeObserver:self [[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemTimeJumpedNotification name:AVPlayerItemTimeJumpedNotification
object:m_playerItem]; object:m_playerItem];
m_playerItem = 0; m_playerItem = 0;
} }
if (m_player) {
[m_player setRate:0.0];
[m_player removeObserver:self forKeyPath:AVF_CURRENT_ITEM_KEY];
[m_player removeObserver:self forKeyPath:AVF_RATE_KEY];
[m_player release];
m_player = 0;
}
if (m_playerLayer) {
[m_playerLayer release];
m_playerLayer = 0;
}
} }
- (void) prepareToPlayAsset:(AVURLAsset *)asset - (void) prepareToPlayAsset:(AVURLAsset *)asset
@@ -203,21 +212,6 @@ static void *AVFMediaPlayerSessionObserverCurrentItemObservationContext = &AVFMe
name:AVPlayerItemTimeJumpedNotification name:AVPlayerItemTimeJumpedNotification
object:m_playerItem]; object:m_playerItem];
//Clean up old player if we have one
if (m_player) {
[m_player setRate:0.0];
[m_player removeObserver:self forKeyPath:AVF_CURRENT_ITEM_KEY];
[m_player removeObserver:self forKeyPath:AVF_RATE_KEY];
[m_player release];
m_player = 0;
if (m_playerLayer) {
[m_playerLayer release];
m_playerLayer = 0; //Will have been released
}
}
//Get a new AVPlayer initialized to play the specified player item. //Get a new AVPlayer initialized to play the specified player item.
m_player = [AVPlayer playerWithPlayerItem:m_playerItem]; m_player = [AVPlayer playerWithPlayerItem:m_playerItem];
[m_player retain]; [m_player retain];
@@ -354,18 +348,6 @@ static void *AVFMediaPlayerSessionObserverCurrentItemObservationContext = &AVFMe
#endif #endif
[self unloadMedia]; [self unloadMedia];
if (m_player) {
[m_player removeObserver:self forKeyPath:AVF_CURRENT_ITEM_KEY];
[m_player removeObserver:self forKeyPath:AVF_RATE_KEY];
[m_player release];
m_player = 0;
}
if (m_playerLayer) {
[m_playerLayer release];
m_playerLayer = 0;
}
if (m_URL) { if (m_URL) {
[m_URL release]; [m_URL release];
} }
@@ -464,6 +446,8 @@ void AVFMediaPlayerSession::setMedia(const QMediaContent &content, QIODevice *st
qDebug() << Q_FUNC_INFO << content.canonicalUrl(); qDebug() << Q_FUNC_INFO << content.canonicalUrl();
#endif #endif
[(AVFMediaPlayerSessionObserver*)m_observer unloadMedia];
m_resources = content; m_resources = content;
m_mediaStream = stream; m_mediaStream = stream;
@@ -475,7 +459,6 @@ void AVFMediaPlayerSession::setMedia(const QMediaContent &content, QIODevice *st
QMediaPlayer::MediaStatus oldMediaStatus = m_mediaStatus; QMediaPlayer::MediaStatus oldMediaStatus = m_mediaStatus;
if (content.isNull() || content.canonicalUrl().isEmpty()) { if (content.isNull() || content.canonicalUrl().isEmpty()) {
[(AVFMediaPlayerSessionObserver*)m_observer unloadMedia];
m_mediaStatus = QMediaPlayer::NoMedia; m_mediaStatus = QMediaPlayer::NoMedia;
if (m_state != QMediaPlayer::StoppedState) if (m_state != QMediaPlayer::StoppedState)
Q_EMIT stateChanged(m_state = QMediaPlayer::StoppedState); Q_EMIT stateChanged(m_state = QMediaPlayer::StoppedState);