Hi! 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.
+1
A:
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) ...
Ole Begemann
2010-02-16 16:35:53
It worked Flawlessly!
Flafla2
2010-02-16 16:53:48
+1
A:
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.
KennyTM
2010-02-16 16:37:36
A:
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.
Curmudgeonlybumbly
2010-03-21 19:05:15