/// <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();
        }
示例#2
0
        #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
        }
 /// <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)
     {
         if (MLMusicService.PlaybackState == MLMusicServicePlaybackState.Paused)
         {
             MLMusicService.ResumePlayback();
         }
         else if (MLMusicService.PlaybackState == MLMusicServicePlaybackState.Stopped)
         {
             MLMusicService.StartPlayback();
         }
     }
 }
示例#4
0
        /// <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;
            }
        }