My app involves music(iPodMusic), and there is a UISwitch toggling play/pause. My goal is to be able to detect if music is playing, so that therefore the play/pause switch can say 'play' when music is playing and 'pause' if it isn't.
4 Answers
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) ...

- 135,006
- 31
- 278
- 256
-
1This is also useful if you want your app to stop making its own noise if the user starts up the iPod app. For example if you're writing a game and your own in-game music clashes with the user trying to play their own. – Piku Aug 21 '11 at 19:19
-
1This has to be fixed due to iOS 8.0's deprecation of `iPodMusicPlayer` method of `MPMusicController`: swap it to `systemMusicPlayer`. – DDPWNAGE Jun 16 '15 at 01:22
If the music is from your own app, check AVAudioPlayer's playing
property.
If the music is from iPod, check MPMusicPlayerController's nowPlayingItem
or playbackState
property.

- 510,854
- 105
- 1,084
- 1,005
MPMusicPlayerController is only available in OS 3.0 or above. If you're running 2.0 you're out of luck. Here's a code snippet that checks if you're running 3.0 or above and only then attempts to create an MPMuiscPlayerController
bool playerDetectedAndPlaying = false;
NSString *reqSysVer = @"3.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
Class MusicPlayerController = NSClassFromString(@"MPMusicPlayerController");
if (MusicPlayerController){
id myMusicPlayerController = [[MusicPlayerController alloc]init];
id MusicPlayer = [[myMusicPlayerController class] iPodMusicPlayer ];
if ( [ MusicPlayer playbackState ] == MPMusicPlaybackStatePlaying ) {
playerDetectedAndPlaying = true;
}
}
}
You have to compile against a 3.0 SDK, but if you set the deployment target to 2.0, this code still runs on older devices.

- 529
- 4
- 13
it goes to yes if your iTunes sound is on else it goes to no if sound is off of iTunes music player ... So You can check easily default MusicPlayer Sound is On or Off You need to add media-player Framework Just Try It it will work Properly.... Thx...:)
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
{
NSLog(@"yes itune Player Sound is on");
}
else
{
NSLog(@"NO itune Player Sound is not on");
}

- 799
- 11
- 27