public void InitAndPlayAudio (string trackSource)
		{
			var resourceId = GetRawResourceId(trackSource);
			Console.WriteLine("Resource id for " + trackSource + " = " 
				+ (resourceId.HasValue ? resourceId.Value.ToString() : "(null)"));


			if (player != null) {
				if (player.IsPlaying) {
					player.Stop ();
				}
				player.Reset ();
				player.Release ();
				player = null;
			}

			if (resourceId.HasValue) {
				player = MediaPlayer.Create (Application.Context, resourceId.Value);

				// do not use player.Prepare () -- MediaPlayer.Create takes care of this

				player.Completion += (sender, e) => {
					player.Reset ();
					player.Release ();
					player = null;
					AudioState = AudioState.Stopped;
				};

				AudioState = AudioState.Playing;
				player.Start ();
			}
		}
        public override void OnProgressChanged(WebView view, int newProgress)
        {
            base.OnProgressChanged(view, newProgress);

            _context.SetProgress(newProgress * 100);

            if (newProgress == 100)
            {
                _context.Title = view.Title;

                bool soundEnabled = PreferenceManager.GetDefaultSharedPreferences(_context.ApplicationContext).GetBoolean("sounds", false);

                if (soundEnabled)
                {
                    _mediaPlayer = MediaPlayer.Create(_context.ApplicationContext, Resource.Raw.inception_horn);
                    _mediaPlayer.Completion += delegate { _mediaPlayer.Release(); };
                    _mediaPlayer.Start();
                }

                // add this page to the history
                using (SQLiteDatabase db = _historyDataHelper.WritableDatabase)
                {
                    var values = new ContentValues();
                    values.Put("Title", view.Title);
                    values.Put("Url", view.Url);
                    values.Put("Timestamp", DateTime.Now.Ticks);

                    db.Insert("history", null, values);
                }
            }
            else
            {
                _context.Title = _context.ApplicationContext.Resources.GetString(Resource.String.title_loading);
            }
        }
        public bool Play()
        {
            _player = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw._01yes);
            _player.Start();
            _player.Completion += delegate
            {
                _player.Release();
            };

            return true;
        }
Esempio n. 4
0
 public int findDuration(string filename)
 {
     MediaPlayer wav = new MediaPlayer();
     FileInputStream fs = new FileInputStream(filename);
     FileDescriptor fd = fs.FD;
     wav.SetDataSource(fd);
     wav.Prepare();
     int length = wav.Duration;
     wav.Reset();
     wav.Release();
     return length;
 }
Esempio n. 5
0
 public int videoDuration(string filename)
 {
     MediaPlayer video = new MediaPlayer();
     FileInputStream fs = new FileInputStream(filename);
     FileDescriptor fd = fs.FD;
     video.SetDataSource(fd);
     video.Prepare();
     int length = video.Duration;
     video.Reset();
     video.Release();
     return length;
 }
 public void OnCompletion(Android.Media.MediaPlayer mp)
 {
     try
     {
         mp.Release();
         StoryVideoView?.StopPlayback();
         StoryVideoView = null;
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Esempio n. 7
0
 public void OnCompletion(MediaPlayer player)
 {
     try
     {
         player.Stop();
         player.Reset();
         player.Release();
     } catch (Exception ex)
     {
         #if DEBUG
         System.Diagnostics.Debug.WriteLine("Exception in audio {0}", ex.Message);
         #endif
     }
 }
Esempio n. 8
0
		public void Play ()
		{
			try {
				player = MediaPlayer.Create (context, Resource.Raw.droll);
				player.Completion += (object sender, EventArgs e) => {
					player.Stop ();
					player.Release ();
					player = null;
				};
				player.Start ();
			} catch (Exception ex) {
				Console.Out.WriteLine (ex.StackTrace);
			}
		}
Esempio n. 9
0
 private void TryDisposePlayer()
 {
     if (_player != null)
     {
         try
         {
             _isPlayRequested  = false;
             _isPlayerPrepared = false;
             _player.Release();
         }
         finally
         {
             _player?.Dispose();
             _player = null;
         }
     }
 }
Esempio n. 10
0
        private void TryDisposePlayer()
        {
            if (_player != null)
            {
                try
                {
                    _isPlayRequested  = false;
                    _isPlayerPrepared = false;
                    _player.Release();

                    var surfaceView = RenderSurface as SurfaceView;
                    surfaceView?.Holder?.RemoveCallback(this);
                }
                finally
                {
                    _player?.Dispose();
                    _player = null;
                }
            }
        }
        void DeletePlayer()
        {
            Stop();

            if (player != null)
            {
                player.Completion -= OnPlaybackEnded;
                player.Release();
                player.Dispose();
                player = null;
            }

            if (mNextPlayer != null)
            {
                mNextPlayer.Completion -= OnPlaybackEnded;
                mNextPlayer.Release();
                mNextPlayer.Dispose();
                mNextPlayer = null;
            }

            DeleteFile(path);
            path = string.Empty;
        }
Esempio n. 12
0
        private void TryDisposePlayer()
        {
            if (_player != null)
            {
                try
                {
                    _isPlayRequested  = false;
                    _isPlayerPrepared = false;
                    _player.Release();

                    // Clear the surface view so we don't see
                    // the previous video rendering.
                    if (RenderSurface is VideoSurface surfaceView && _hasValidHolder)
                    {
                        surfaceView.Clear();
                    }
                }
                finally
                {
                    _player?.Dispose();
                    _player = null;
                }
            }
        }
Esempio n. 13
0
 public void OnCompletion(MediaPlayer p)
 {
     p.Stop ();
     p.Release ();
 }
Esempio n. 14
0
        protected override void OnCreate(Bundle bundle)
        {
            _player = MediaPlayer.Create (this,Resource.Raw.police_alarm);
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.alarm);
            ImageButton start = FindViewById<ImageButton> (Resource.Id.Start_ALarm);
            start.Click += delegate {
                _player = MediaPlayer.Create (this,Resource.Raw.police_alarm);
                _player.Start ();

            };
            ImageButton stop = FindViewById<ImageButton> (Resource.Id.Stop_ALarm);
            stop.Click += delegate {
                _player.Stop ();
                _player.Release();
                _player=null;
            //				Intent i = new Intent (this, typeof(MainActivity));
            //				StartActivity (i);
            };
            //	FindViewById<TextView> (Resource.Id.Start_ALarm).Click+=Start_alarm_click;
            //FindViewById<TextView> (Resource.Id.Stop_ALarm).Click += Stop_alarm_click;
        }
        public void reproducir(string downloadurl, bool desdecache)
        {
            // musicaplayer.SetDataSource(downloadurl);
            if (playeroffline.gettearinstancia() != null)
            {
                try
                {
                    musicaplayer.Release();


                    musicaplayer = new MediaPlayer();

#pragma warning disable 414
                    if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.N)
                    {
                        musicaplayer.SetAudioAttributes(new AudioAttributes.Builder()
                                                        .SetUsage(AudioUsageKind.Media)
                                                        .SetContentType(AudioContentType.Music)
                                                        .Build());
                    }
                    else
                    {
#pragma warning disable CS0618 // El tipo o el miembro están obsoletos
                        musicaplayer.SetAudioStreamType(Android.Media.Stream.Music);
#pragma warning restore CS0618 // El tipo o el miembro están obsoletos
                    }

#pragma warning restore 414
                    musicaplayer.SetWakeMode(this, WakeLockFlags.Partial);
#pragma warning disable 414
#pragma warning disable CS0618 // El tipo o el miembro están obsoletos
                    var focusResult = audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
#pragma warning restore CS0618 // El tipo o el miembro están obsoletos
#pragma warning restore 414
                    if (focusResult != AudioFocusRequest.Granted)
                    {
                        //could not get audio focus
                        Console.WriteLine("Could not get audio focus");
                    }


                    musicaplayer.Prepared += delegate
                    {
                        musicaplayer.Start();
                        if (this.desdecache)
                        {
                            musicaplayer.Pause();
                        }
                        if (playeroffline.gettearinstancia() != null)
                        {
                            if (playeroffline.gettearinstancia().video.Visibility == ViewStates.Visible)
                            {
                                musicaplayer.SetDisplay(null);
                                musicaplayer.SetDisplay(playeroffline.gettearinstancia().holder);
                            }

                            if (SettingsHelper.HasKey("posactual") && this.desdecache)
                            {
                                var posicion = 0;
                                try
                                {
                                    posicion = int.Parse(SettingsHelper.GetSetting("posactual"));
                                    musicaplayer.SeekTo(posicion);
                                }
                                catch (Exception) { }
                            }
                        }
                    };
                    musicaplayer.Completion += delegate
                    {
                        playeroffline.gettearinstancia().RunOnUiThread(() =>
                        {
                            playeroffline.gettearinstancia().RunOnUiThread(() =>
                            {
                                playeroffline.gettearinstancia().siguiente.PerformClick();
                            });
                        });
                    };

                    musicaplayer.SetDataSource(this, Android.Net.Uri.Parse(downloadurl.Trim()));
                    mostrarnotificacion();
                    musicaplayer.PrepareAsync();
                }
                catch (Exception)
                {
                    //if()

                    playeroffline.gettearinstancia().RunOnUiThread(() =>
                    {
                        Toast.MakeText(playeroffline.gettearinstancia(), "Error al reproducir", ToastLength.Long).Show();
                    });
                }
            }
            else
            {
                musicaplayer.Reset();
                StopSelf();
            }
        }
        public void reproducir(string downloadurl)
        {
            // musicaplayer.SetDataSource(downloadurl);

            if (YoutubePlayerServerActivity.gettearinstancia() != null)
            {
                musicaplayer.Release();


                musicaplayer = new MediaPlayer();

                if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.N)
                {
                    musicaplayer.SetAudioAttributes(new AudioAttributes.Builder().SetUsage(AudioUsageKind.Media).SetContentType(AudioContentType.Music).Build());
                }
                else
                {
#pragma warning disable CS0618 // El tipo o el miembro están obsoletos
                    musicaplayer.SetAudioStreamType(Android.Media.Stream.Music);
#pragma warning restore CS0618 // El tipo o el miembro están obsoletos
                }
                musicaplayer.SetWakeMode(this, WakeLockFlags.Partial);

#pragma warning disable CS0618 // El tipo o el miembro están obsoletos
                var focusResult = audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
#pragma warning restore CS0618 // El tipo o el miembro están obsoletos
                //    musicaplayer.SetVideoScalingMode(VideoScalingMode.ScaleToFitWithCropping);

                if (focusResult != AudioFocusRequest.Granted)
                {
                    //could not get audio focus
                    Console.WriteLine("Could not get audio focus");
                }


                musicaplayer.Error += (aa, aaaa) =>
                {
                    Console.WriteLine("klk aw aw aw");
                };

                musicaplayer.Info += (aa, aaa) =>
                {
                    var instancia = YoutubePlayerServerActivity.gettearinstancia();
                    if (instancia != null)
                    {
                        switch (aaa.What)
                        {
                        case MediaInfo.BufferingStart:
                            if (instancia.prgBuffering.Visibility != ViewStates.Visible)
                            {
                                instancia.prgBuffering.Visibility = ViewStates.Visible;
                            }
                            break;

                        case MediaInfo.BufferingEnd:
                            if (instancia.prgBuffering.Visibility != ViewStates.Gone)
                            {
                                instancia.prgBuffering.Visibility = ViewStates.Gone;
                            }
                            break;

                        case MediaInfo.VideoRenderingStart:
                            if (instancia.prgBuffering.Visibility != ViewStates.Gone)
                            {
                                instancia.prgBuffering.Visibility = ViewStates.Gone;
                            }
                            break;
                        }
                        ;
                    }
                };



                musicaplayer.Prepared += delegate
                {
                    if (YoutubePlayerServerActivity.gettearinstancia().videoon)
                    {
                        YoutubePlayerServerActivity.gettearinstancia().RunOnUiThread(() =>
                        {
                            try
                            {
                                musicaplayer.SetDisplay(null);
                                musicaplayer.SetDisplay(YoutubePlayerServerActivity.gettearinstancia().videoSurfaceHolder);
                            }
                            catch (Exception) {
                            }


                            YoutubePlayerServerActivity.gettearinstancia().SetVideoSize();
                        });
                    }
                    musicaplayer.Start();
                    if (YoutubePlayerServerActivity.gettearinstancia().qualitychanged)
                    {
                        try
                        {
                            YoutubePlayerServerActivity.gettearinstancia().qualitychanged = false;
                            musicaplayer.SeekTo(YoutubePlayerServerActivity.gettearinstancia().previousprogress);
                        }
                        catch (Exception) { }
                    }
                };
                musicaplayer.Completion += delegate
                {
                    if ((musicaplayer.Duration > 5 && musicaplayer.CurrentPosition > 5))
                    {
                        new Thread(() =>
                        {
                            YoutubePlayerServerActivity.gettearinstancia().NextVideo();
                        }).Start();
                    }
                };


                mostrarnotificacion();
                musicaplayer.SetDataSource(this, Android.Net.Uri.Parse(downloadurl));
                musicaplayer.PrepareAsync();
            }
            else
            {
                musicaplayer.Reset();
                StopSelf();
            }
        }