简体   繁体   中英

Phonegap / Cordova Stop audio after time when in background IOS

I have an audio app that loops sounds for playback using Cordova 2.2 and its Audio API. At the moment I have setup a number of loops that will stop when finished based on predetermined time (calculated on n seconds per loop / 3 hours) . This method generally works.

           playMainAudio = new Media(url,
                                          // success callback
                                          function() {
                                          console.log("playAudio():Audio Success");
                                          },
                                          // error callback
                                          function(err) {
                                          console.log("playAudio():Audio Error: "+err);
                                          });

                // Play audio
                playMainAudio.play({ numberOfLoops: 123, playAudioWhenScreenIsLocked : true });

But I'd prefer a native code addition where I could just set all audio to stop after 3 hours rather then work it out based on time- but not sure were to look or even place the code. The catch is it has to work when locked or the app is in the background (currently I have the correct background mode set so the audio will play in the BG).

is there a native timer that is background compatible ?

If you are keen on editing app delegate.m in Objective-C (not your language of choice) inside

- (void)applicationDidEnterBackground:(UIApplication *)application stop audio but delay using

– performSelector:withObject:afterDelay:

See the documentation here:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

For examples on how to play audio in Objective-C:

How to play a sound in objective C iphone coding

Play Audio iOS Objective-C

How can I Add Audio Player in iphone App

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM