protected virtual void InitializeSource() { PlaybackSession.NaturalDuration = TimeSpan.Zero; PlaybackSession.PositionFromPlayer = TimeSpan.Zero; if (Source == null) { return; } try { // Reset player TryDisposePlayer(); InitializePlayer(); PlaybackSession.PlaybackState = MediaPlaybackState.Opening; SetVideoSource(((MediaSource)Source).Uri); _player.PrepareAsync(); MediaOpened?.Invoke(this, null); } catch (global::System.Exception ex) { OnMediaFailed(ex); } }
private void PlayVideo(SurfaceTexture surfaceTexture) { DoCleanUp(); try { path = "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"; if (path == "") { // Tell the user to provide a media file URL. Toast.MakeText(this, "Please edit MediaPlayerDemo_setSurface Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", ToastLength.Long).Show(); return; } // Create a new media player and set the listeners mMediaPlayer = new MediaPlayer(this, true); mMediaPlayer.SetDataSource(path); if (surf == null) { surf = new Surface (surfaceTexture); } mMediaPlayer.SetSurface(surf); mMediaPlayer.PrepareAsync(); mMediaPlayer.SetOnBufferingUpdateListener(this); mMediaPlayer.SetOnCompletionListener(this); mMediaPlayer.SetOnPreparedListener(this); VolumeControlStream = Stream.Music; } catch (Exception e) { Log.Error(TAG, "error: " + e.Message, e); } }
protected virtual void InitializeSource() { PlaybackSession.NaturalDuration = TimeSpan.Zero; PlaybackSession.PositionFromPlayer = TimeSpan.Zero; // Reset player TryDisposePlayer(); if (Source == null) { return; } try { InitializePlayer(); PlaybackSession.PlaybackState = MediaPlaybackState.Opening; Uri uri; switch (Source) { case MediaPlaybackList playlist when playlist.Items.Count > 0: SetPlaylistItems(playlist); uri = _playlistItems[0]; break; case MediaPlaybackItem item: uri = item.Source.Uri; break; case MediaSource source: uri = source.Uri; break; default: throw new InvalidOperationException("Unsupported media source type"); } SetVideoSource(uri); _player.PrepareAsync(); MediaOpened?.Invoke(this, null); } catch (global::System.Exception ex) { OnMediaFailed(ex); } }
protected virtual void InitializeSource() { PlaybackSession.NaturalDuration = TimeSpan.Zero; PlaybackSession.PositionFromPlayer = TimeSpan.Zero; // Reset player TryDisposePlayer(); if (Source == null) { return; } try { InitializePlayer(); PlaybackSession.PlaybackState = MediaPlaybackState.Opening; if (Source is MediaPlaybackList) { _playlistItems = new List <Uri>(); var playlist = Source as MediaPlaybackList; foreach (var mediaItem in playlist.Items) { _playlistItems.Add(mediaItem.Source.Uri); } } var uri = _playlistItems?.Count > 0 ? _playlistItems[0] : ((MediaSource)Source).Uri; SetVideoSource(uri); _player.PrepareAsync(); MediaOpened?.Invoke(this, null); } catch (global::System.Exception ex) { OnMediaFailed(ex); } }
public Sound(string filename, float volume, bool looping) { this._player = new MediaPlayer(); // get the Asset Descriptor and Release it when the SetDataSource returns // otherwise you cant play the file using (AssetFileDescriptor fd = Game.contextInstance.Assets.OpenFd(filename)) { _player.SetDataSource(fd.FileDescriptor); } _player.Prepared += this.OnPrepared; this.Looping = looping; this.Volume = volume; // prepare on the background thread try { _player.PrepareAsync(); } catch (Exception ex) { Log.Debug("MonoGameInfo", ex.ToString()); } }
protected async override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.PodcastDetail); var showNumber = Intent.GetIntExtra("show_number", 0); episode = Activity1.ViewModel.GetPodcast(showNumber); var description = FindViewById<TextView>(Resource.Id.descriptionView); description.Text = episode.Description; var play = FindViewById<Button>(Resource.Id.playButton); var pause = FindViewById<Button>(Resource.Id.pauseButton); var stop = FindViewById<Button>(Resource.Id.stopButton); seekBar = FindViewById<SeekBar>(Resource.Id.seekBar1); status = FindViewById<TextView>(Resource.Id.statusText); updateHandler = new Handler(); player = new MediaPlayer(); player.SetDataSource(this, Android.Net.Uri.Parse(episode.AudioUrl)); player.PrepareAsync(); player.Prepared += (sender, e) => { initialized = true; player.SeekTo(timeToSet * 1000); UpdateStatus(); }; play.Click += (sender, e) => { player.Start(); updateHandler.PostDelayed(UpdateStatus, 1000); }; pause.Click += (sender, e) => player.Pause(); stop.Click += (sender, e) => { player.Stop(); player.Reset(); player.SetDataSource(this, Android.Net.Uri.Parse(episode.AudioUrl)); player.Prepare(); }; seekBar.ProgressChanged += (sender, e) => { if (!e.FromUser) return; player.SeekTo((int)(player.Duration * ((float)seekBar.Progress / 100.0))); }; var updated = await episode.GetTimeAsync(); if (updated == null || updated.ShowNumber != episode.ShowNumber) return; if (initialized && player != null) { player.SeekTo(updated.CurrentTime * 1000); UpdateStatus(); } else { timeToSet = updated.CurrentTime; } }
private void PlayVideo(int Media) { DoCleanUp(); try { switch (Media) { case LOCAL_VIDEO: // // * TODO: Set the path variable to a local media file path. // path = "http://172.16.101.100:81/video/123.mp4"; if (path == "") { // Tell the user to provide a media file URL. Toast.MakeText(this, "Please edit MediaPlayerDemo_Video Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", ToastLength.Long).Show(); return; } break; case STREAM_VIDEO: // // * TODO: Set path variable to progressive streamable mp4 or // * 3gpp format URL. Http protocol should be used. // * Mediaplayer can only play "progressive streamable // * contents" which basically means: 1. the movie atom has to // * precede all the media data atoms. 2. The clip has to be // * reasonably interleaved. // * // path = "http://172.16.101.100:81/video/123.mp4"; if (path == "") { // Tell the user to provide a media file URL. Toast.MakeText(this, "Please edit MediaPlayerDemo_Video Activity," + " and set the path variable to your media file URL.", ToastLength.Long).Show(); return; } break; } // Create a new media player and set the listeners mMediaPlayer = new MediaPlayer(this); mMediaPlayer.SetDataSource(path); mMediaPlayer.SetDisplay(holder); mMediaPlayer.PrepareAsync(); mMediaPlayer.SetOnBufferingUpdateListener(this); mMediaPlayer.SetOnCompletionListener(this); mMediaPlayer.SetOnPreparedListener(this); mMediaPlayer.SetOnVideoSizeChangedListener(this); VolumeControlStream = Stream.Music; } catch (Exception e) { Log.Error(TAG, "error: " + e.Message, e); } }
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(); } }