/// <summary> /// Initialize the MLMusicService example and connect it to the example provider /// </summary> void Start() { if (!CheckReferences()) { return; } string[] playlist = new string[StreamingPlaylist.Length]; for (int i = 0; i < StreamingPlaylist.Length; ++i) { string streamingPath = Path.Combine(Path.Combine(Application.streamingAssetsPath, "BackgroundMusicExample"), StreamingPlaylist[i]); string persistentPath = Path.Combine(Application.persistentDataPath, StreamingPlaylist[i]); // The Example Music Provider will only search for songs correctly in /documents/C1 or /documents/C2 by their filename // This allows us to package the songs with the Unity application and deploy them from Streaming Assets. if (!File.Exists(persistentPath)) { File.Copy(streamingPath, persistentPath); } playlist[i] = "file://" + StreamingPlaylist[i]; } MLResult result = MLMusicService.Start(MusicServiceProvider); if (!result.IsOk) { if (result.Code == MLResultCode.PrivilegeDenied) { Instantiate(Resources.Load("PrivilegeDeniedError")); } Debug.LogErrorFormat("Error: MusicServiceExample failed starting MLMusicService, disabling script. Reason: {0}", result); enabled = false; return; } MLMusicService.OnPlaybackStateChange += HandlePlaybackStateChanged; MLMusicService.OnShuffleStateChange += HandleShuffleStateChanged; MLMusicService.OnRepeatStateChange += HandleRepeatStateChanged; MLMusicService.OnMetadataChange += HandleMetadataChanged; MLMusicService.OnPositionChange += HandlePositionChanged; MLMusicService.OnError += HandleError; MLMusicService.OnStatusChange += HandleServiceStatusChanged; _playbackBar.OnValueChanged += Seek; _volumeBar.OnValueChanged += SetVolume; _playButton.OnToggle += PlayPause; _prevButton.OnControllerTriggerDown += Previous; _nextButton.OnControllerTriggerDown += Next; _shuffleButton.OnToggle += ToggleShuffle; _repeatButton.OnControllerTriggerDown += ChangeRepeatState; MLMusicService.RepeatState = MLMusicServiceRepeatState.Off; MLMusicService.ShuffleState = MLMusicServiceShuffleState.Off; MLMusicService.SetPlayList(playlist); MLMusicService.StartPlayback(); }
#pragma warning restore 414 /// <summary> /// Locate tracks, connect provided music service provider, initialize MLMusicService API and setup callbacks. /// </summary> void Start() { string[] playlist = new string[StreamingPlaylist.Length]; for (int i = 0; i < StreamingPlaylist.Length; ++i) { string streamingPath = Path.Combine(Path.Combine(Application.streamingAssetsPath, StreamingAssetsLocationFolder), StreamingPlaylist[i]); string persistentPath = Path.Combine(Application.persistentDataPath, StreamingPlaylist[i]); // The Music Provider will only search for songs correctly in /documents/C1 or /documents/C2 by their filename // This allows us to package the songs with the Unity application and deploy them from Streaming Assets. if (!File.Exists(persistentPath)) { File.Copy(streamingPath, persistentPath); } playlist[i] = "file://" + StreamingPlaylist[i]; } #if PLATFORM_LUMIN MLResult result = MLMusicService.Start(MusicServiceProvider); if (!result.IsOk) { if (result.Result == MLResult.Code.PrivilegeDenied) { Instantiate(Resources.Load("PrivilegeDeniedError")); } Debug.LogErrorFormat("Error: MLMusicServiceBehavior failed starting MLMusicService, disabling script. Reason: {0}", result); enabled = false; return; } MLMusicService.OnPlaybackStateChange += HandlePlaybackStateChanged; MLMusicService.OnShuffleStateChange += HandleShuffleStateChanged; MLMusicService.OnRepeatStateChange += HandleRepeatStateChanged; MLMusicService.OnMetadataChange += HandleMetadataChanged; MLMusicService.OnPositionChange += HandlePositionChanged; MLMusicService.OnError += HandleError; MLMusicService.OnStatusChange += HandleServiceStatusChanged; MLMusicService.RepeatState = MLMusicService.RepeatStateType.Off; MLMusicService.ShuffleState = MLMusicService.ShuffleStateType.Off; MLMusicService.SetPlayList(playlist); MLMusicService.StartPlayback(); #endif }