<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Support.V4.Media.Session;
using Plugin.MediaManager.Abstractions;
using Plugin.MediaManager.Abstractions.EventArguments;

namespace Plugin.MediaManager
{
    [Service]
    [IntentFilter(new[] { ActionPlay, ActionPause, ActionStop, ActionTogglePlayback, ActionNext, ActionPrevious })]
    public class MediaPlayerService : MediaServiceBase,
        MediaPlayer.IOnBufferingUpdateListener,
        MediaPlayer.IOnCompletionListener,
        MediaPlayer.IOnErrorListener,
        MediaPlayer.IOnPreparedListener,
        MediaPlayer.IOnSeekCompleteListener
    {
        private MediaPlayer _mediaPlayer;
        private TimeSpan _buffered = TimeSpan.Zero;

        public override TimeSpan Position
        {
            get
            {
                if (_mediaPlayer == null
                    || (MediaPlayerState != PlaybackStateCompat.StatePlaying
                        && MediaPlayerState != PlaybackStateCompat.StatePaused))
                    return TimeSpan.FromSeconds(-1);
                else
                    return TimeSpan.FromMilliseconds(_mediaPlayer.CurrentPosition);
            }
        }

        public override TimeSpan Duration
        {
            get
            {
                if (_mediaPlayer == null
                    || (MediaPlayerState != PlaybackStateCompat.StatePlaying
                        && MediaPlayerState != PlaybackStateCompat.StatePaused))
                    return TimeSpan.Zero;
                else
                    return TimeSpan.FromMilliseconds(_mediaPlayer.Duration);
            }
        }

        public override TimeSpan Buffered
        {
            get
            {
                if (_mediaPlayer == null)
                    return TimeSpan.Zero;
                else
                    return _buffered;
            }
        }

        public override void InitializePlayer()
        {
            DisposeMediaPlayer();
            _mediaPlayer = new MediaPlayer();
            SetMediaPlayerOptions();
        }

        public override void InitializePlayerWithUrl(string audioUrl)
        {
            Android.Net.Uri uri = Android.Net.Uri.Parse(Android.Net.Uri.Encode(audioUrl));
            _mediaPlayer = MediaPlayer.Create(ApplicationContext, uri);
            SetMediaPlayerOptions();
        }

        public override void SetMediaPlayerOptions()
        {
            //Tell our player to sream music
            _mediaPlayer.SetAudioStreamType(Stream.Music);

            //Wake mode will be partial to keep the CPU still running under lock screen
            _mediaPlayer.SetWakeMode(ApplicationContext, WakeLockFlags.Partial);
            _mediaPlayer.SetOnBufferingUpdateListener(this);
            _mediaPlayer.SetOnCompletionListener(this);
            _mediaPlayer.SetOnErrorListener(this);
            _mediaPlayer.SetOnPreparedListener(this);
        }

        [Obsolete("deprecated")]
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            HandleIntent(intent);
            return base.OnStartCommand(intent, flags, startId);
        }

        public override async Task Play(IMediaFile mediaFile = null)
        {
            var sameMediaFile = mediaFile == null || mediaFile == CurrentFile;

            await base.Play(mediaFile);

            if (!sameMediaFile) {
                try
                {
                    _mediaPlayer.PrepareAsync();
                }<commit_msg>Fix for droid service remaining after app kill<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Support.V4.Media.Session;
using Plugin.MediaManager.Abstractions;
using Plugin.MediaManager.Abstractions.EventArguments;

namespace Plugin.MediaManager
{
    [Service]
    [IntentFilter(new[] { ActionPlay, ActionPause, ActionStop, ActionTogglePlayback, ActionNext, ActionPrevious })]
    public class MediaPlayerService : MediaServiceBase,
        MediaPlayer.IOnBufferingUpdateListener,
        MediaPlayer.IOnCompletionListener,
        MediaPlayer.IOnErrorListener,
        MediaPlayer.IOnPreparedListener,
        MediaPlayer.IOnSeekCompleteListener
    {
        private MediaPlayer _mediaPlayer;
        private TimeSpan _buffered = TimeSpan.Zero;

        public override TimeSpan Position
        {
            get
            {
                if (_mediaPlayer == null
                    || (MediaPlayerState != PlaybackStateCompat.StatePlaying
                        && MediaPlayerState != PlaybackStateCompat.StatePaused))
                    return TimeSpan.FromSeconds(-1);
                else
                    return TimeSpan.FromMilliseconds(_mediaPlayer.CurrentPosition);
            }
        }

        public override TimeSpan Duration
        {
            get
            {
                if (_mediaPlayer == null
                    || (MediaPlayerState != PlaybackStateCompat.StatePlaying
                        && MediaPlayerState != PlaybackStateCompat.StatePaused))
                    return TimeSpan.Zero;
                else
                    return TimeSpan.FromMilliseconds(_mediaPlayer.Duration);
            }
        }

        public override TimeSpan Buffered
        {
            get
            {
                if (_mediaPlayer == null)
                    return TimeSpan.Zero;
                else
                    return _buffered;
            }
        }

        public override void InitializePlayer()
        {
            DisposeMediaPlayer();
            _mediaPlayer = new MediaPlayer();
            SetMediaPlayerOptions();
        }

        public override void InitializePlayerWithUrl(string audioUrl)
        {
            Android.Net.Uri uri = Android.Net.Uri.Parse(Android.Net.Uri.Encode(audioUrl));
            _mediaPlayer = MediaPlayer.Create(ApplicationContext, uri);
            SetMediaPlayerOptions();
        }

        public override void SetMediaPlayerOptions()
        {
            //Tell our player to sream music
            _mediaPlayer.SetAudioStreamType(Stream.Music);

            //Wake mode will be partial to keep the CPU still running under lock screen
            _mediaPlayer.SetWakeMode(ApplicationContext, WakeLockFlags.Partial);
            _mediaPlayer.SetOnBufferingUpdateListener(this);
            _mediaPlayer.SetOnCompletionListener(this);
            _mediaPlayer.SetOnErrorListener(this);
            _mediaPlayer.SetOnPreparedListener(this);
        }

        [Obsolete("deprecated")]
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            HandleIntent(intent);
            return StartCommandResult.NotSticky;// base.OnStartCommand(intent, flags, startId);
        }

        public override async Task Play(IMediaFile mediaFile = null)
        {
            var sameMediaFile = mediaFile == null || mediaFile == CurrentFile;

            await base.Play(mediaFile);

            if (!sameMediaFile) {
                try
                {
                    _mediaPlayer.PrepareAsync();
                }
{'fraction_non_alphanumeric': 0.06963974822552564, 'fraction_numerical': 0.0005356903709655819, 'mean_word_length': 1.9103663289166017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870481', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1708, 'n_words': 374}