Ejemplo n.º 1
0
        /// <summary>
        /// Plays a sound.
        /// </summary>
        /// <remarks>
        /// You may need to stop or pause playing a sound when your app is paused.
        /// </remarks>
        /// <param name="isLooping">Indicates whether play sound repeatedly.</param>
        public async Task PlayAsync(bool isLooping = true)
        {
            if (_mediaSourceUpdated || _streamTypeUpdated)
            {
                if (_player.State != PlayerState.Idle)
                {
                    _player.Unprepare();
                }

                if (_mediaSourceUpdated)
                {
                    _player.SetSource(_mediaSource);
                    _mediaSourceUpdated = false;
                }

                if (_streamTypeUpdated)
                {
                    if (_streamPolicy != null)
                    {
                        _streamPolicy.FocusStateChanged -= OnFocusStateChanged;
                        _streamPolicy.Dispose();
                    }

                    _streamPolicy = new AudioStreamPolicy(_streamType);
                    _streamPolicy.FocusStateChanged += OnFocusStateChanged;

                    // TODO: If you want to reacquire the focus you've lost automatically, enable the focus reacquisition.
                    // In addition, you may need to implement the case of AudioStreamFocusState.Acquired in OnFocusStateChanged.
                    _streamPolicy.FocusReacquisitionEnabled = false;
                    _player.ApplyAudioStreamPolicy(_streamPolicy);
                    _streamTypeUpdated = false;
                }
            }

            if (_player.State == PlayerState.Playing)
            {
                _player.Stop();
            }
            else if (_player.State == PlayerState.Idle)
            {
                await _player.PrepareAsync();
            }

            if (_streamPolicy.PlaybackFocusState != AudioStreamFocusState.Acquired)
            {
                _streamPolicy.AcquireFocus(AudioStreamFocusOptions.Playback, AudioStreamBehaviors.Fading, null);
            }

            _player.IsLooping = isLooping;
            _player.Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronously plays ringing sound
        /// </summary>
        /// <param name="targetVolume">The volume level at playing</param>
        /// <param name="toneTypes">AlarmToneTypes to play</param>
        async public void PlaySound(float targetVolume, AlarmToneTypes toneTypes)
        {
            audioStreamPolicy = new AudioStreamPolicy(AudioStreamType.Alarm);
            audioStreamPolicy.AcquireFocus(AudioStreamFocusOptions.Playback, AudioStreamBehaviors.NoResume, null);

            player = new Player();
            MediaUriSource soudSource = new MediaUriSource(SystemSettings.IncomingCallRingtone);

            player.SetSource(soudSource);

            player.ApplyAudioStreamPolicy(audioStreamPolicy);
            await player.PrepareAsync();

            player.IsLooping = true;
            player.Volume    = targetVolume;
            if (player.State == PlayerState.Ready)
            {
                player.Start();
            }
        }
        /// <summary>
        /// Asynchronously plays ringing sound
        /// </summary>
        async public static void PlaySound()
        {
            if (player == null)
            {
                audioStreamPolicy = new AudioStreamPolicy(AudioStreamType.Alarm);
                audioStreamPolicy.AcquireFocus(AudioStreamFocusOptions.Playback, AudioStreamBehaviors.Fading, null);

                player = new Player();
                MediaUriSource soudSource = new MediaUriSource(SystemSettings.SoundNotification);
                player.SetSource(soudSource);

                player.ApplyAudioStreamPolicy(audioStreamPolicy);
                await player.PrepareAsync();

                player.IsLooping = true;
                player.Volume    = 1;
                if (player.State == PlayerState.Ready)
                {
                    player.Start();
                }
            }
        }