简体   繁体   中英

handling events when app goes into the background

What I want is that my app should stop playing sound when it enters the background (when home button is pressed). I did this in appDelegate.m but it says use of undeclared identifier 'soundID'

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    AudioServicesDisposeSystemSoundID(soundID)
}

I have imported my VC into the delegate and have also exposed the soundID as a property. Where am I going wrong? Please help.

   NSString *path = [[NSBundle mainBundle] pathForResource:@"something" ofType:@"wav"];
    NSURL *url = [NSURL fileURLWithPath:path]; 
    AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID);
    AudioServicesPlaySystemSound(soundID);

If you want that functionality you should go to your app main plist file (probably named [your-app-name]-Info.plist ) and there find a key:

Required background modes

and remove value

App plays audio

That should do the trick.

---------------- EDIT:

Try this:

NSString* path = [[NSBundle mainBundle] pathForResource:@"myWavFile" ofType:@"wav" inDirectory:@"DirectoryName"];
self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
[self.player play];

Where self player is:

@property (strong, nonatomic) AVPlayer *player;

You should also:

#import <AVFoundation/AVFoundation.h>

And add this framework to your project.

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