Stop Background audio while starting

前端 未结 1 1683
抹茶落季
抹茶落季 2021-01-24 07:00

I could make the application, to play the audio when it moves to the background, by adding the setting in info.plist file.

But, when some other iOS application plays bac

相关标签:
1条回答
  • 2021-01-24 07:07

    Have a look at AVAudioSession class and AVAudioSession programming guide by Apple. This will hopefully steer you in the right direction.

    To stop background audio when your application starts set your audio session to a non-mixable category and mode then set active.

    If you don't configure an audio session your application will get the default session which in your case is not what you're after.

    Try this in you applicationDelegates method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // set audio session category AVAudioSessionCategoryPlayAndRecord with no options
    success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
    if (!success) {
        NSLog(@"setCategoryError");
    }
    
    // set audio session mode to default
    success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
    if (!success) {
        NSLog(@"setModeError");
    }
    
    // activate audio session
    success = [[AVAudioSession sharedInstance] setActive:YES error:nil];
    if (!success) {
        NSLog(@"activationError");
    }
    
    0 讨论(0)
提交回复
热议问题