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).

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.

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!