Fix formatting and restart bug

In the current system you can’t have the same sound playing more than once at the same time - this is how it was implemented on Android, so we used the same restriction on iOS.

However in the previous version if you interrupted the sound to play it again, it continued from where it was instead of starting over. So it didn’t appear to do anything. Added resetting the current time to 0.0 on play to restart sounds.
This commit is contained in:
chrisgarrity 2017-03-14 12:11:53 -04:00
parent e9d0d0d7be
commit f752ff5760
5 changed files with 122 additions and 172 deletions

View file

@ -239,53 +239,18 @@ NSMutableDictionary *sounds;
else { else {
url = [self getResourcePath: [NSString stringWithFormat: @"%@%@", dir, name]]; url = [self getResourcePath: [NSString stringWithFormat: @"%@%@", dir, name]];
} }
NSLog (@"registering %@", url);
NSError *error; NSError *error;
AVAudioPlayer *snd = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:&error]; AVAudioPlayer *snd = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:&error];
if (error == NULL) { if (error == nil) {
[sounds setObject:snd forKey:name]; [sounds setObject:snd forKey:name];
[snd prepareToPlay];
return [NSString stringWithFormat: @"%@,%f", name, snd.duration]; return [NSString stringWithFormat: @"%@,%f", name, snd.duration];
} }
else {
NSLog (@"%@", error);
}
return @"error"; return @"error";
} }
// +(NSString *)playSound:(NSString*)name {
// // get the sound either from Documents (user defined sounds)
// // or from the HTML5 bundle.
// NSURL *url = ([dir isEqual: @"Documents"]) ? [self getDocumentPath: name] : [self getResou\
// rcePath: [NSString stringWithFormat: @"%@%@", dir, name]];
//
// // audio type: respect the "Mute" if there are audio sounds
// // ignore the Mute if it is from recording / playback and Runtime.
// NSString *audiotype = ([dir isEqual: @"Documents"] || [name isEqual:@"pop.mp3"]) ? AVAudio\
// SessionCategoryPlayAndRecord : AVAudioSessionCategoryAmbient;
// [[AVAudioSession sharedInstance] setCategory:audiotype error:nil];
//
// NSError *error;
// AVAudioPlayer *snd = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:&error];
//
// if (error == NULL) {
// snd.numberOfLoops = 0;
// [snd prepareToPlay];
// [snd play];
// NSString *id = [self setSoundTimeout: snd];
// NSString *result = [NSString stringWithFormat: @"%@,%f", id, [snd duration]];
// NSLog (@"%@", result);
// return result;
// }
// else {
// NSLog (@"%@", error);
// return @"error";
// }
// }
+ (NSString *)playSound :(NSString*)name { + (NSString *)playSound :(NSString*)name {
// TODO: make scratchJr pay attention to the mute // TODO: make scratchJr pay attention to the mute
// // audio type: respect the "Mute" if there are audio sounds // // audio type: respect the "Mute" if there are audio sounds
@ -294,12 +259,11 @@ NSMutableDictionary *sounds;
// SessionCategoryPlayAndRecord : AVAudioSessionCategoryAmbient; // SessionCategoryPlayAndRecord : AVAudioSessionCategoryAmbient;
// [[AVAudioSession sharedInstance] setCategory:audiotype error:nil]; // [[AVAudioSession sharedInstance] setCategory:audiotype error:nil];
AVAudioPlayer *snd = sounds[name]; AVAudioPlayer *snd = sounds[name];
NSLog (@"play %@", snd); if (snd == nil) {
if (snd == NULL) {
return [NSString stringWithFormat: @"%@ not found", name]; return [NSString stringWithFormat: @"%@ not found", name];
} }
else { else {
[snd prepareToPlay]; [snd setCurrentTime:0.0];
[snd play]; [snd play];
[NSTimer scheduledTimerWithTimeInterval: [snd duration] target: self selector: @selector(so\ [NSTimer scheduledTimerWithTimeInterval: [snd duration] target: self selector: @selector(so\
undEnded:) userInfo:@{@"soundName": name} repeats: NO]; undEnded:) userInfo:@{@"soundName": name} repeats: NO];
@ -309,26 +273,20 @@ undEnded:) userInfo:@{@"soundName": name} repeats: NO];
+ (void)soundEnded:(NSTimer*)timer { + (void)soundEnded:(NSTimer*)timer {
NSString *soundName = [[timer userInfo] objectForKey:@"soundName"]; NSString *soundName = [[timer userInfo] objectForKey:@"soundName"];
NSLog(@"%@", soundName); if (sounds[soundName] == nil) return;
if (sounds [soundName] == NULL) return;
NSString *callback = [NSString stringWithFormat:@"iOS.soundDone('%@');", soundName]; NSString *callback = [NSString stringWithFormat:@"iOS.soundDone('%@');", soundName];
UIWebView *webview = [ViewController webview]; UIWebView *webview = [ViewController webview];
[webview stringByEvaluatingJavaScriptFromString:callback]; [webview stringByEvaluatingJavaScriptFromString:callback];
} }
+ (NSString *)stopSound :(NSString*)name { + (NSString *)stopSound :(NSString*)name {
AVAudioPlayer *snd = sounds[name]; AVAudioPlayer *snd = sounds[name];
NSLog (@"stop %@", snd); if (snd == nil) {
if (snd == NULL) {
return [NSString stringWithFormat:@"%@ not found", name]; return [NSString stringWithFormat:@"%@ not found", name];
} }
else {
[snd stop]; [snd stop];
return [NSString stringWithFormat:@"%@ stopped", name]; return [NSString stringWithFormat:@"%@ stopped", name];
} }
}
//////////////////////////// ////////////////////////////
// File system // File system

View file

@ -120,7 +120,11 @@
- (NSString *)scratchjr_stopfeed; - (NSString *)scratchjr_stopfeed;
- (NSString *)scratchjr_choosecamera:(NSString *)body; - (NSString *)scratchjr_choosecamera:(NSString *)body;
- (NSString *)scratchjr_captureimage:(NSString *)onCameraCaptureComplete; - (NSString *)scratchjr_captureimage:(NSString *)onCameraCaptureComplete;
- (NSString*) sendSjrUsingShareDialog:(NSString*) fileName :(NSString*) emailSubject :(NSString*) emailBody :(int) shareType :(NSString*) b64data; - (NSString *)sendSjrUsingShareDialog:(NSString *)fileName
:(NSString *)emailSubject
:(NSString *)emailBody
:(int)shareType
:(NSString *)b64data;
- (NSString *) deviceName; - (NSString *) deviceName;
- (NSString *) analyticsEvent:(NSString *)category :(NSString *)action :(NSString *)label :(NSNumber*)value; - (NSString *) analyticsEvent:(NSString *)category :(NSString *)action :(NSString *)label :(NSNumber*)value;
@end @end
@ -135,7 +139,10 @@
@end @end
@interface ViewController (ViewFinderDelegate) <ViewFinderDelegate> @interface ViewController (ViewFinderDelegate) <ViewFinderDelegate>
- (void) showShareEmail:(NSURL *) projectURL withName: (NSString*) name withSubject:(NSString*) subject withBody:(NSString*)body; - (void)showShareEmail:(NSURL *)projectURL
withName:(NSString *)name
withSubject:(NSString *)subject
withBody:(NSString *)body;
- (void)showShareAirdrop:(NSURL *)projectURL; - (void)showShareAirdrop:(NSURL *)projectURL;
@end @end
@ -161,7 +168,11 @@
+ (NSString *)getmedialen:(NSString *)file :(NSString *)key; + (NSString *)getmedialen:(NSString *)file :(NSString *)key;
+ (NSString *)getmediadone:(NSString *)filename; + (NSString *)getmediadone:(NSString *)filename;
+ (NSString *)remove:(NSString *)filename; + (NSString *)remove:(NSString *)filename;
+ (NSString*) sendSjrUsingShareDialog:(NSString*) fileName :(NSString*) emailSubject :(NSString*) emailBody :(int) shareType :(NSString*) b64data; + (NSString *)sendSjrUsingShareDialog:(NSString *)fileName
:(NSString *)emailSubject
:(NSString *)emailBody
:(int)shareType
:(NSString *)b64data;
+ (NSString *)registerSound:(NSString *)dir :(NSString *)name; + (NSString *)registerSound:(NSString *)dir :(NSString *)name;
+ (NSString *)playSound:(NSString *)name; + (NSString *)playSound:(NSString *)name;
+ (NSString *)stopSound:(NSString *)name; + (NSString *)stopSound:(NSString *)name;

View file

@ -78,7 +78,6 @@ JSContext *js;
[defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key]; [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
} }
} }
// NSLog(@"defaultsToRegister %@", defaultsToRegister);
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
} }

View file

@ -190,7 +190,6 @@ export default class iOS {
} }
} }
static playSound (name, fcn) { static playSound (name, fcn) {
var result = tabletInterface.io_playsound(name); var result = tabletInterface.io_playsound(name);
if (fcn) { if (fcn) {
@ -372,8 +371,6 @@ export default class iOS {
} }
} }
} }
} }
// Expose iOS methods for ScratchJr tablet sharing callbacks // Expose iOS methods for ScratchJr tablet sharing callbacks

View file

@ -48,18 +48,10 @@ export default class ScratchAudio {
ScratchAudio.addSound(prefix + 'sounds/', defaultSounds[i], uiSounds); ScratchAudio.addSound(prefix + 'sounds/', defaultSounds[i], uiSounds);
} }
ScratchAudio.addSound(prefix, 'pop.mp3', projectSounds); ScratchAudio.addSound(prefix, 'pop.mp3', projectSounds);
// } else {
// for (var j=0; j < defaultSounds.length; j++) {
// iOS.registerSound('HTML5/sounds/', defaultSounds[j], ScratchAudio.UIsoundLoaded );
// }
// iOS.registerSound('HTML5/', 'pop.mp3', ScratchAudio.UIsoundLoaded );
// }
} }
static addSound (url, snd, dict, fcn) { static addSound (url, snd, dict, fcn) {
var name = snd; var name = snd;
console.log(url+' '+snd);
if (!isAndroid) { if (!isAndroid) {
var whenDone = function (str) { var whenDone = function (str) {
if (str != 'error') { if (str != 'error') {
@ -71,10 +63,8 @@ export default class ScratchAudio {
if (fcn) { if (fcn) {
fcn(name); fcn(name);
} }
// dict [name].time = Number (result[1]);
}; };
iOS.registerSound(url, snd, whenDone); iOS.registerSound(url, snd, whenDone);
} else { } else {
// In Android, this is handled outside of JavaScript, so just place a stub here. // In Android, this is handled outside of JavaScript, so just place a stub here.
dict[snd] = new Sound(url + snd); dict[snd] = new Sound(url + snd);
@ -87,12 +77,9 @@ export default class ScratchAudio {
static soundDone (name) { static soundDone (name) {
if (!projectSounds[name]) return; if (!projectSounds[name]) return;
projectSounds[name].playing = false; projectSounds[name].playing = false;
console.log(name);
} }
static loadProjectSound (md5, fcn) { static loadProjectSound (md5, fcn) {
console.log(md5);
if (!md5) { if (!md5) {
return; return;
} }
@ -101,7 +88,6 @@ export default class ScratchAudio {
if (md5.indexOf('/') > -1) dir = 'HTML5/'; if (md5.indexOf('/') > -1) dir = 'HTML5/';
else if (md5.indexOf('wav') > -1) dir = 'Documents'; else if (md5.indexOf('wav') > -1) dir = 'Documents';
} }
console.log('loadProjectSound: ' + dir + ' ' + md5);
ScratchAudio.loadFromLocal(dir, md5, fcn); ScratchAudio.loadFromLocal(dir, md5, fcn);
} }
@ -111,7 +97,6 @@ export default class ScratchAudio {
} }
ScratchAudio.addSound(dir, md5, projectSounds, fcn); ScratchAudio.addSound(dir, md5, projectSounds, fcn);
} }
} }
window.ScratchAudio = ScratchAudio; window.ScratchAudio = ScratchAudio;