From 3a6f2c4d5bb2d5fb04f892f2849cc75933f89e83 Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Wed, 26 Jan 2011 11:06:10 +0100 Subject: [PATCH] make CPSound handling error + More Cocoa compliant --- AppKit/CPSound.j | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/AppKit/CPSound.j b/AppKit/CPSound.j index 8fbf8980e..bba03b2f7 100644 --- a/AppKit/CPSound.j +++ b/AppKit/CPSound.j @@ -25,6 +25,7 @@ CPSoundLoadStateEmpty = 0; CPSoundLoadStateLoading = 1; CPSoundLoadStateCanBePlayed = 2; +CPSoundLoadStateError = 3; CPSoundPlayBackStatePlay = 0; CPSoundPlayBackStateStop = 1; @@ -68,6 +69,10 @@ CPSoundPlayBackStatePause = 2; { [self _soundDidEnd]; }, true); + _audioTag.addEventListener("error", function() + { + [self _soundError]; + }, true); } return self; @@ -137,31 +142,45 @@ CPSoundPlayBackStatePause = 2; [self stop]; } +/*! @ignore +*/ +- (void)_soundError +{ + _loadStatus = CPSoundLoadStateError; + CPLog.error("Cannot load sound. Maybe the format of your sound is not compatible with your browser."); +} + #pragma mark - #pragma mark Media controls /*! Play the sound. + + @return YES when the receiver is playing its audio data, NO otherwise. */ -- (void)play +- (BOOL)play { if ((_loadStatus !== CPSoundLoadStateCanBePlayed) || (_playBackStatus === CPSoundPlayBackStatePlay)) - return; + return NO; _audioTag.play(); _playBackStatus = CPSoundPlayBackStatePlay; + + return YES; } /*! Stop the sound. + + @return YES when the receiver is playing its audio data, NO otherwise. */ -- (void)stop +- (BOOL)stop { if ((_loadStatus !== CPSoundLoadStateCanBePlayed) || (_playBackStatus === CPSoundPlayBackStateStop)) - return; + return NO; _audioTag.pause(); _audioTag.currentTime = 0.0; @@ -169,32 +188,42 @@ CPSoundPlayBackStatePause = 2; if (_delegate && [_delegate respondsToSelector:@selector(sound:didFinishPlaying:)]) [_delegate sound:self didFinishPlaying:YES]; + + return YES; } /*! Pause the sound. + + @return YES when the receiver is playing its audio data, NO otherwise. */ -- (void)pause +- (BOOL)pause { if ((_loadStatus !== CPSoundLoadStateCanBePlayed) || (_playBackStatus === CPSoundPlayBackStatePause)) - return; + return NO; _audioTag.pause(); _playBackStatus = CPSoundPlayBackStatePause; + + return YES; } /*! Resume playback of a paused sound. + + @return YES when the receiver is playing its audio data, NO otherwise. */ -- (void)resume +- (BOOL)resume { if ((_loadStatus !== CPSoundLoadStateCanBePlayed) || (_playBackStatus !== CPSoundPlayBackStatePause)) - return; + return NO; _audioTag.play(); _playBackStatus = CPSoundPlayBackStatePlay; + + return YES; } /*! @@ -265,4 +294,4 @@ CPSoundPlayBackStatePause = 2; return (_playBackStatus === CPSoundPlayBackStatePlay); } -@end +@end \ No newline at end of file