- Home /
iOS Requirement (A10.16) Ensure that when iTunes Radio is playing, Game Music must be set to “Off” automatically
I'm getting this bug report back on my game and I have no idea how to solve it. Does anyone know how to detect when iTunes Radio starts or stops using c# or how to go about solving this issue? (To clarify, I'm not asking how to start/stop my music, but rather how do I knew when to start/stop my music).
Did you solve for this? We are hitting the same issue, any guidance you're willing to share?
Answer by JDAUL · Jun 16, 2016 at 05:38 AM
Hey there @ChimeraSW, here is a simple plugin we used to accomplish what you want:
Save it as DetectMusicPlayer.mm or something similar, and place it in your Plugins/iOS folder.
#import <MediaPlayer/MediaPlayer.h>
extern "C" {
bool _DetectMusicPlayer() {
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;
}
}
}
return playerDetectedAndPlaying;
}
void _ResumeMusicPlayer()
{
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 ) {
[myMusicPlayerController play];
}
}
}
}
}
Then, in another script, say DetectMusicPlayer.cs, give a callback to the native plugin like so:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class DetectMusicPlayer
{
#if UNITY_IOS
[DllImport("__Internal")]
static private extern bool _DetectMusicPlayer();
[DllImport("__Internal")]
static private extern void _ResumeMusicPlayer();
#endif
static public bool Detect()
{
#if UNITY_IOS
return _DetectMusicPlayer();
#else
return false;
#endif
}
static public void ResumePlayer()
{
#if UNITY_IOS
Debug.Log("Resuming music player");
_ResumeMusicPlayer();
#endif
}
}
//Usage example
/*
//Some global Listener class...
void OnApplicationFocus(bool isFocused)
{
bool musicPlaying = DetectMusicPlayer.Detect();
if (musicPlaying)
{
// Mute game music here...
}
else
{
//Unmute game music here...
}
}
*/
Hope this helps!
Answer by samonte.rey · May 16, 2016 at 05:16 PM
I personally did not solve it but a member in our team did. The solution was to write a native plugin that detects when iTunes radio was playing.
I don't suppose this is a plugin you guys would be willing to share? :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612080858im_/https://answers.unity.com/themes/thub/images/avi.jpg)