Detecting if music is playing?

我只是一个虾纸丫 提交于 2019-11-28 21:52:55
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) ...

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.

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.

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");
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!