Reputation: 6465
I am developing an iPhone app that will play its own sounds at the request of the user.
My question is if the device is already playing music, how can I disable the sound within my app on launch?
I know I will have to detect if the device is already playing music, but how can I do that?
Upvotes: 0
Views: 1484
Reputation: 274
To check if you are already playing music, you could do something like :
(YES == ((AVAudioSession *)[AVAudioSession sharedInstance]).otherAudioPlaying)
Upvotes: 0
Reputation: 6039
You could either activate an audio session like so:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
or you could create a [MPMusicPlayerController iPodMusicPlayer]
instance and pause the music, but this wont pause music being played inside other apps.
Edit
If you want to check if the iPod is playing music then you could do something like this:
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
if ([musicPlayer playbackState] == == MPMusicPlaybackStatePlaying)
//iPod is playing music, do something here
}
You'll need to add the MediaPlayer
framework.
Upvotes: 3