コード例 #1
0
        /// <summary>
        /// initializes the mediaplayer object on tab of your choice.
        /// if the mediaplayer is already instantiated then it gets reset for new playback
        /// </summary>
        /// <param name="mp"></param>
        /// <returns></returns>
        public static async Task <MediaPlayer> InitializePlayer(int id, Android.Net.Uri uri, Context ctx, string mediaPath = null)
        {
            if (ctx == null)
            {
                ctx = Android.App.Application.Context;
            }
            var cookieHeader = ExtWebInterface.GetCookieDictionary();

            if (!MainPlaybackSticky.MediaPlayerDictionary.ContainsKey(id))
            {
                MainPlaybackSticky.MediaPlayerDictionary.Add(id, new MediaPlayer());
            }
            else if (MediaPlayerDictionary[id] == null)
            {
                MediaPlayerDictionary[id] = new MediaPlayer();
            }
            else
            {
                MediaPlayerDictionary[id].Reset();
                MediaPlayerDictionary[id].Release();

                MediaPlayerDictionary[id] = null;
                MediaPlayerDictionary[id] = new MediaPlayer();
            }
            if (mediaPath != null)
            {
                await MediaPlayerDictionary[id].SetDataSourceAsync(mediaPath);
            }
            else
            {
                //await MediaPlayerDictionary[id].SetDataSourceAsync(ctx, uri);
                await MediaPlayerDictionary[id].SetDataSourceAsync(Android.App.Application.Context, uri, cookieHeader);
            }
            MediaPlayerDictionary[id].PrepareAsync();

            PlaystateChanged.Invoke(new PlaystateEventArgs(-1, false, false, false, id, false, false, true, id));
            MediaPlayerDictionary[id].SetOnSeekCompleteListener(ExtStickyServ);

            //Wake mode will be partial to keep the CPU still running under lock screen
            MediaPlayerDictionary[id].SetWakeMode(Android.App.Application.Context, WakeLockFlags.Partial);

            //When we have prepared the song start playback
            MediaPlayerDictionary[id].Prepared += (sender, args) => ExtStickyServ.Play(id, PlayerType.NativeMediaPlayer);

            //When we have reached the end of the song stop ourselves, however you could signal next track here.
            MediaPlayerDictionary[id].Completion += (sender, args) => OnVideoFinished(false, id);

            MediaPlayerDictionary[id].Error += (sender, args) =>
            {
                //playback error
                Console.WriteLine("Error in playback resetting: " + args.What);
                Stop();//this will clean up and reset properly.
            };

            return(MediaPlayerDictionary[id]);
        }
        public static void ReRouteToAppPlaystate(string url, int playerNumber = -1)
        {
            switch (url)
            {
            case @"https://_%26app_play_invoked/":
                PlaystateChanged.Invoke(new PlaystateEventArgs(playerNumber, true));
                break;

            case @"https://_%26app_pause_invoked/":
                PlaystateChanged.Invoke(new PlaystateEventArgs(playerNumber, false, true));
                break;

            case @"https://_%26app_play_isplaying/":
                PlaystateChanged.Invoke(new PlaystateEventArgs(playerNumber, false, false, true));
                break;

            case @"https://_%26app_vidend_invoked_autoplay":
                PlaystateChanged.Invoke(new PlaystateEventArgs(true, playerNumber, true, false));
                break;

            case @"https://_%26app_vidend_invoked_noautoplay":
                PlaystateChanged.Invoke(new PlaystateEventArgs(true, playerNumber, false, false));
                break;

            case @"https://_%26app_isplaying_onminimized/":
                PlaystateChanged.Invoke(new PlaystateEventArgs(true, playerNumber, true));
                break;

            case @"https://_%26app_isnotplaying_onminimized/":
                PlaystateChanged.Invoke(new PlaystateEventArgs(false, playerNumber, false));
                break;

            case @"https://_&app_isplaying_and_loading_onminimized/":
                PlaystateChanged.Invoke(new PlaystateEventArgs(true, playerNumber, true));
                break;
            }
        }
コード例 #3
0
 public void Play(int id = -1, PlayerType playerType = PlayerType.None)
 {
     if ((PlaystateManagement.MediaPlayerIsStreaming && PlaystateManagement.MediaPlayerIsQueued) ||
         playerType == PlayerType.NativeMediaPlayer)
     {
         Task.Factory.StartNew(() =>
         {
             if (MediaPlayerDictionary[id] != null)
             {
                 MediaPlayerDictionary[id].Start();
                 return;
             }
             if (MediaPlayerDictionary[id].IsPlaying)
             {
                 PlaystateChanged.Invoke(new PlaystateEventArgs(-1, false, false, false, id, false, false, false, id));
             }
         });
     }
     else if (PlaystateManagement.PlayerTypeQueued() == PlayerType.WebViewPlayer)
     {
         Task.Factory.StartNew(() =>
         {
             if (PlaystateManagement.WebViewPlayerNumberIsStreaming != -1)
             {
                 StartVideoInBkgrd(PlaystateManagement.WebViewPlayerNumberIsStreaming);
             }
             else if (PlaystateManagement.MediaPlayerNumberIsQueued != -1)
             {
                 StartVideoInBkgrd(PlaystateManagement.MediaPlayerNumberIsQueued);
             }
             else
             {
                 StartVideoInBkgrd();
             }
         });
     }
 }