Ejemplo n.º 1
0
        /// <summary>
        /// Releases all resources used by the current instance.
        /// </summary>
        /// <param name="disposing">Indicates whether the method call comes from a Dispose method or from a finalizer.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _player.PlaybackCompleted   -= OnCompleted;
                _player.PlaybackInterrupted -= OnInterrupted;
                _player.ErrorOccurred       -= OnErrorOccurred;
                _player.Dispose();

                _streamPolicy.FocusStateChanged -= OnFocusStateChanged;
                _streamPolicy.Dispose();
            }

            _disposed = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronously stops ringing
        /// </summary>
        public void StopSound()
        {
            if (player != null)
            {
                player.Stop();
                player.Unprepare();
                audioStreamPolicy.ReleaseFocus(AudioStreamFocusOptions.Playback, AudioStreamBehaviors.NoResume, null);
                player.Dispose();
                player = null;
            }

            if (audioStreamPolicy != null)
            {
                audioStreamPolicy.Dispose();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class
        /// </summary>
        /// <param name="navigation">
        /// Navigation instance
        /// </param>
        public MainViewModel(INavigation navigation)
        {
            Navigation = navigation;

            PlayCommand = new Command(async() =>
            {
                _isPlaying         = true;
                _cts               = new CancellationTokenSource();
                _audioStreamPolicy = new AudioStreamPolicy(AudioStreamType.Media);

                PlayCommand.ChangeCanExecute();
                CancelCommand.ChangeCanExecute();

                try
                {
                    await TonePlayer.StartAsync(ToneType.Default, _audioStreamPolicy, Duration * _secUnit, _cts.Token);
                }
                catch (TaskCanceledException)
                {
                    Tizen.Log.Info("TonePlayer", "A task for playing media was canceled.");
                }
                finally
                {
                    _isPlaying = false;
                    _cts?.Dispose();
                    _audioStreamPolicy?.Dispose();
                }

                PlayCommand.ChangeCanExecute();
                CancelCommand.ChangeCanExecute();
            }, CanPlay);

            CancelCommand = new Command(() =>
            {
                if (_isPlaying)
                {
                    _cts?.Cancel();
                    _isPlaying = false;
                }

                PlayCommand.ChangeCanExecute();
                CancelCommand.ChangeCanExecute();
            }, CanCancel);
        }