2008-12-08 03:55:01 +00:00
/ * Copyright ( c ) 2008 Julian Mayer
Permission is hereby granted , free of charge , to any person obtaining a copy of this software and associated documentation files ( the "Software" ) , to deal in the Software without restriction , including without limitation the rights to use , copy , modify , merge , publish , distribute , sublicense , and / or sell copies of the Software , and to permit persons to whom the Software is furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY , FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE . * /
# import "NSSound_win32.h"
# import < Foundation / NSString . h >
# import < Foundation / NSNumber . h >
# import < Foundation / NSPathUtilities . h >
# import < Foundation / NSEnumerator . h >
# import < Foundation / NSArray . h >
2011-10-09 15:30:36 +02:00
# import < Foundation / NSURL . h >
2009-06-05 03:16:46 +00:00
# import < AppKit / NSRaise . h >
2009-02-17 19:12:02 +00:00
# import < windows . h >
2008-12-08 03:55:01 +00:00
2011-08-16 22:21:34 -04:00
// zero is null number
static unsigned int uniquenum = 1 ;
2008-12-08 03:55:01 +00:00
@ interface NSSound ( win32 )
@ end
@ implementation NSSound ( win32 )
+ allocWithZone : ( NSZone * ) zone {
2013-10-10 02:13:17 -04:00
Class directShow = NSClassFromString ( @ "NSSound_DirectShow" ) ;
2013-10-10 00:34:41 -04:00
if ( directShow ! = Nil )
return NSAllocateObject ( directShow , 0 , NULL ) ;
2008-12-08 03:55:01 +00:00
return NSAllocateObject ( [ NSSound_win32 class ] , 0 , NULL ) ;
}
+ ( NSArray * ) _soundUnfilteredFileTypes {
// FIXME : Instead of returned this predetermined set ( XPSP2 ) we should query it something * like * GetProfileString ( "mci extensions" , . . . . ) ;
return [ NSArray arrayWithObjects : @ "wav" , @ "aif" , @ "aifc" , @ "aiff" , @ "asf" , @ "asx" , @ "au" , @ "m1v" , @ "m3u" , @ "mp2" , @ "mp2v" , @ "mp3" , @ "mpa" , @ "mpe" , @ "mpeg" , @ "mpg" , @ "mpv2" , @ "snd" , @ "wax" , @ "wm" , @ "wma" , @ "wmv" , @ "wmx" , @ "wpl" , @ "wvx" , nil ] ;
}
@ end
@ implementation NSSound_win32
2011-10-09 15:07:17 +02:00
- ( id ) initWithContentsOfURL : ( NSURL * ) url byReference : ( BOOL ) byReference {
return [ self initWithContentsOfFile : [ url path ] byReference : byReference ] ;
}
- ( id ) initWithContentsOfFile : ( NSString * ) path byReference : ( BOOL ) byReference {
2008-12-08 03:55:01 +00:00
if ( ( self = [ super initWithContentsOfFile : path byReference : byReference ] ) )
{
_soundFilePath = [ path copy ] ;
2009-02-17 19:12:02 +00:00
_paused = NO ;
2011-08-16 22:21:34 -04:00
_handle = 0 ;
2008-12-08 03:55:01 +00:00
}
return self ;
}
- ( BOOL ) play {
2011-08-16 22:21:34 -04:00
if ( _handle = = 0 ) {
_handle = uniquenum + + ;
}
else {
/ * If the sound has already been played we need to close it before playing again or it wont play . * /
NSString * stopStr = [ NSString stringWithFormat : @ "close %i" , _handle ] ;
mciSendString ( [ stopStr UTF8String ] , NULL , 0 , 0 ) ;
}
NSString * loadStr = [ NSString stringWithFormat : @ "open \" % @ \ " type %@ alias %i" , _soundFilePath , [ [ _soundFilePath pathExtension ] isEqualToString : @ "wav" ] ? @ "waveaudio" : @ "MPEGVideo" , _handle ] ;
if ( mciSendString ( [ loadStr UTF8String ] , NULL , 0 , 0 ) )
return NO ;
2008-12-08 03:55:01 +00:00
NSString * playStr = [ NSString stringWithFormat : @ "play %i from 0" , _handle ] ;
if ( mciSendString ( [ playStr UTF8String ] , NULL , 0 , 0 ) )
return NO ;
return YES ;
}
- ( BOOL ) pause {
if ( _paused )
return NO ;
else
{
2011-08-16 22:21:34 -04:00
if ( _handle ! = 0 ) {
NSString * pauseStr = [ NSString stringWithFormat : @ "pause %i" , _handle ] ;
mciSendString ( [ pauseStr UTF8String ] , NULL , 0 , 0 ) ;
}
2009-02-17 19:12:02 +00:00
_paused = YES ;
2008-12-08 03:55:01 +00:00
}
return YES ;
}
- ( BOOL ) resume {
if ( ! _paused )
return NO ;
else
{
2011-08-16 22:21:34 -04:00
if ( _handle ! = 0 ) {
NSString * pauseStr = [ NSString stringWithFormat : @ "resume %i" , _handle ] ;
mciSendString ( [ pauseStr UTF8String ] , NULL , 0 , 0 ) ;
}
2009-02-17 19:12:02 +00:00
_paused = NO ;
2008-12-08 03:55:01 +00:00
}
return YES ;
}
- ( BOOL ) stop {
2011-08-16 22:21:34 -04:00
if ( _handle ! = 0 ) {
NSString * stopStr = [ NSString stringWithFormat : @ "stop %i" , _handle ] ;
mciSendString ( [ stopStr UTF8String ] , NULL , 0 , 0 ) ;
}
2008-12-08 03:55:01 +00:00
return YES ;
}
- ( void ) dealloc {
2011-08-16 22:21:34 -04:00
if ( _handle ! = 0 ) {
NSString * stopStr = [ NSString stringWithFormat : @ "close %i" , _handle ] ;
mciSendString ( [ stopStr UTF8String ] , NULL , 0 , 0 ) ;
}
2011-10-09 15:30:36 +02:00
[ _soundFilePath release ] ;
2008-12-08 03:55:01 +00:00
[ super dealloc ] ;
}
@ end