private void OnApplicationQuit() { if (MLMusicService.IsStarted) { MLMusicService.Stop(); } }
/// <summary> /// Event handler for the metadata of the track being changed /// </summary> /// <param name="metaData">Metadata of the track</param> /// <param name="userData">Extra user provided data, if passed into MLMusicService.Start</param> void HandleMetadataChanged(MLMusicServiceMetadata metaData, IntPtr userData) { _metadataDisplay.text = String.Format("Track Title: {0}\nAlbum Name: {1}\nAlbum URL: {2}\nAlbum Cover URL: {3}\nArtist Name: {4}\nArtist URL: {5}\n", metaData.TrackTitle, metaData.AlbumInfoName, metaData.AlbumInfoUrl, metaData.AlbumInfoCoverUrl, metaData.ArtistInfoName, metaData.ArtistInfoUrl); _trackLengthMS = MLMusicService.TrackLength; Debug.LogFormat("Metadata Changed\n" + "Track Title: {0}\n" + "Album Name: {1}\n" + "Album URL: {2}\n" + "Album Cover URL: {3}\n" + "Artist Name: {4}\n" + "Artist URL: {5}\n" + "Length: {6}\n", metaData.TrackTitle, metaData.AlbumInfoName, metaData.AlbumInfoUrl, metaData.AlbumInfoCoverUrl, metaData.ArtistInfoName, metaData.ArtistInfoUrl, metaData.Length); MLMusicServiceMetadata trackMeta = new MLMusicServiceMetadata(); // Retrieve the meta information for the previous track. MLMusicService.GetMetadata(MLMusicServiceTrackType.Previous, ref trackMeta); _metadataPreviousTrack.text = string.Format("Previous: {0}", trackMeta.TrackTitle); // Retrieve the meta information for the next track. MLMusicService.GetMetadata(MLMusicServiceTrackType.Next, ref trackMeta); _metadataNextTrack.text = string.Format("Next: {0}", trackMeta.TrackTitle); }
/// <summary> /// Stops the MLMusicService API. /// </summary> private void OnApplicationQuit() { #if PLATFORM_LUMIN if (MLMusicService.IsStarted) { MLMusicService.Stop(); } #endif }
/// <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(); }
/// <summary> /// Handle the Play/Pause button being triggered /// </summary> /// <param name="shouldPlay">Should the service play or pause</param> private void PlayPause(bool shouldPlay) { if (!shouldPlay && MLMusicService.PlaybackState == MLMusicServicePlaybackState.Playing) { MLMusicService.PausePlayback(); } else if (shouldPlay && MLMusicService.PlaybackState != MLMusicServicePlaybackState.Playing) { MLMusicService.ResumePlayback(); } }
/// <summary> /// Seek to the specified position in the current track. /// </summary> /// <param name="targetMS">The target value to seek to.</param> public void Seek(uint targetMS) { #if PLATFORM_LUMIN MLResult result = MLMusicService.Seek(targetMS); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to seek in the current track, disabling script. Reason: {0}.", result); enabled = false; return; } #endif }
/// <summary> /// Handle the previous button being triggered /// </summary> public void Previous() { #if PLATFORM_LUMIN MLResult result = MLMusicService.Previous(); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to move to the previous track, disabling script. Reason: {0}.", result); enabled = false; return; } #endif }
/// <summary> /// Handle the seek bar being moved to a new position /// </summary> /// <param name="sliderValue">The new value of the seek bar in range [0, 1]</param> private void Seek(float sliderValue) { uint targetMS = (uint)(_trackLengthMS * sliderValue); float currentValue = _trackHeadPositionMS / (float)_trackLengthMS; if (Math.Abs(currentValue - sliderValue) < SEEK_EPSILON) { return; } MLMusicService.Seek(targetMS); }
private void OnApplicationPause(bool pause) { if (MLMusicService.IsStarted) { MLResult result = pause ? MLMusicService.PausePlayback() : MLMusicService.ResumePlayback(); if (!result.IsOk) { Debug.LogErrorFormat("MusicServiceExample failed to {0} the current track, disabling script. Reason: {1}.", pause ? "pause" : "resume", result); enabled = false; return; } } }
/// <summary> /// Handle the seek bar being moved to a new position /// </summary> /// <param name="sliderValue">The new value of the seek bar in range [0, 1]</param> private void Seek(float sliderValue) { uint lengthMS = MLMusicService.TrackLength * 1000; uint targetMS = (uint)(lengthMS * sliderValue); uint current = MLMusicService.CurrentPosition; float currentValue = (float)current / (float)lengthMS; if (Math.Abs(currentValue - sliderValue) < SEEK_EPSILON) { return; } MLMusicService.Seek(targetMS); }
#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 }
private void OnApplicationPause(bool pause) { if (MLMusicService.IsStarted) { MLResult result = pause ? MLMusicService.PausePlayback() : MLMusicService.ResumePlayback(); if (!result.IsOk) { if (result.Code == MLResultCode.PrivilegeDenied) { Instantiate(Resources.Load("PrivilegeDeniedError")); } Debug.LogErrorFormat("MusicServiceExample failed to {0} the current track, disabling script. Reason: {1}.", pause ? "pause" : "resume", result); enabled = false; return; } } }
/// <summary> /// Cleanup the MLMusicService and unregister callbacks. /// </summary> void OnDestroy() { #if PLATFORM_LUMIN if (MLMusicService.IsStarted) { MLMusicService.StopPlayback(); MLMusicService.OnPlaybackStateChange -= HandlePlaybackStateChanged; MLMusicService.OnShuffleStateChange -= HandleShuffleStateChanged; MLMusicService.OnRepeatStateChange -= HandleRepeatStateChanged; MLMusicService.OnMetadataChange -= HandleMetadataChanged; MLMusicService.OnPositionChange -= HandlePositionChanged; MLMusicService.OnError -= HandleError; MLMusicService.OnStatusChange -= HandleServiceStatusChanged; MLMusicService.Stop(); } #endif }
/// <summary> /// Pauses, resumes or starts playback based on playback state. /// </summary> public void PlayPause() { #if PLATFORM_LUMIN MLResult result; switch (MLMusicService.PlaybackState) { case MLMusicService.PlaybackStateType.Playing: result = MLMusicService.PausePlayback(); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to pause the current track, disabling script. Reason: {0}.", result); enabled = false; return; } break; case MLMusicService.PlaybackStateType.Paused: result = MLMusicService.ResumePlayback(); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to resume the current track, disabling script. Reason: {0}.", result); enabled = false; return; } break; case MLMusicService.PlaybackStateType.Stopped: result = MLMusicService.StartPlayback(); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to start the current track, disabling script. Reason: {0}.", result); enabled = false; return; } break; default: break; } #endif }
/// <summary> /// Handle the Play/Pause button being triggered /// </summary> private void PlayPause() { switch (MLMusicService.PlaybackState) { case MLMusicServicePlaybackState.Playing: MLMusicService.PausePlayback(); break; case MLMusicServicePlaybackState.Paused: MLMusicService.ResumePlayback(); break; case MLMusicServicePlaybackState.Stopped: MLMusicService.StartPlayback(); break; default: break; } }
/// <summary> /// Event handler for the metadata of the track being changed /// </summary> /// <param name="metaData">Metadata of the track</param> void HandleMetadataChanged(MLMusicService.Metadata metaData) { CurrentTrackMetadata = metaData; MLResult result = MLMusicService.GetMetadata(ref previousTrackMetadata, -1); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to get the metadata of the previous track, disabling script. Reason: {0}.", result); enabled = false; return; } result = MLMusicService.GetMetadata(ref nextTrackMetadata, 1); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to get the metadata of the next track, disabling script. Reason: {0}.", result); enabled = false; return; } OnMetadataChanged?.Invoke(metaData); }
/// <summary> /// Pauses or resumes playback based on if app is or was playing when paused. /// </summary> /// <param name="pause">Determines if it's a pause or a resume.</param> private void OnApplicationPause(bool pause) { #if PLATFORM_LUMIN if (MLMusicService.IsStarted) { if (pause) { playbackStateOnPause = PlaybackState; } if (playbackStateOnPause == MLMusicService.PlaybackStateType.Playing) { MLResult result = pause ? MLMusicService.PausePlayback() : MLMusicService.ResumePlayback(); if (!result.IsOk) { Debug.LogErrorFormat("MLMusicServiceBehavior failed to {0} the current track, disabling script. Reason: {1}.", pause ? "pause" : "resume", result); enabled = false; return; } } } #endif }
/// <summary> /// Cleanup the MLMusicService and unregister from the callbacks /// </summary> void OnDestroy() { if (MLMusicService.IsStarted) { MLMusicService.StopPlayback(); _playbackBar.OnValueChanged -= Seek; _volumeBar.OnValueChanged -= SetVolume; _playButton.OnToggle -= PlayPause; _prevButton.OnControllerTriggerDown -= Previous; _nextButton.OnControllerTriggerDown -= Next; _shuffleButton.OnToggle -= ToggleShuffle; _repeatButton.OnControllerTriggerDown -= ChangeRepeatState; MLMusicService.OnPlaybackStateChange -= HandlePlaybackStateChanged; MLMusicService.OnShuffleStateChange -= HandleShuffleStateChanged; MLMusicService.OnRepeatStateChange -= HandleRepeatStateChanged; MLMusicService.OnMetadataChange -= HandleMetadataChanged; MLMusicService.OnPositionChange -= HandlePositionChanged; MLMusicService.OnError -= HandleError; MLMusicService.OnStatusChange -= HandleServiceStatusChanged; MLMusicService.Stop(); } }
/// <summary> /// Handle the next button being triggered /// </summary> /// <param name="triggerReading">Unused parameter</param> private void Next(float triggerReading) { MLMusicService.Next(); }
/// <summary> /// Handle the previous button being triggered /// </summary> /// <param name="triggerReading">Unused parameter</param> private void Previous(float triggerReading) { MLMusicService.Previous(); }