/** * \brief stop the current movie being played * @return true on success */ public bool stop() { if (mFullscreen) { Log.Debug("Pikkart AR Video", "cannot stop this video since it is not on texture"); return(false); } if ((mVideoState == VIDEO_STATE.NOT_READY) || (mVideoState == VIDEO_STATE.ERROR)) { Log.Debug("Pikkart AR Video", "cannot stop this video if it is not ready"); return(false); } bool result = false; mMediaPlayerLock.Lock(); if (mMediaPlayer != null) { mVideoState = VIDEO_STATE.STOPPED; try { mMediaPlayer.Stop(); } catch (Exception e) { mMediaPlayerLock.Unlock(); Log.Error("Pikkart AR Video", "Could not stop playback"); } result = true; } mMediaPlayerLock.Unlock(); return(result); }
/** * \brief Callback for interface MediaPlayer.OnErrorListener * @param mediaPlayer * @param i error code * @param i1 error sub-code * @return true if the error is relative to this video player media player */ public bool OnError(MediaPlayer mp, [GeneratedEnum] MediaError what, int extra) { if (mp == mMediaPlayer) { string errorDescription; switch (what) { case MediaError.NotValidForProgressivePlayback: errorDescription = "The video is streamed and its container is not valid for progressive playback"; break; case MediaError.ServerDied: errorDescription = "Media server died"; break; case MediaError.Unknown: errorDescription = "Unspecified media player error"; break; default: errorDescription = "Unknown error " + what; break; } Log.Error("Pikkart AR Video", "Error while opening the file. Unloading the media player (" + errorDescription + ", " + extra + ")"); unload(); mVideoState = VIDEO_STATE.ERROR; return(true); } return(false); }
/** * \brief pauses the current movie being played * @return true on success */ public bool pause() { if (mFullscreen) { Log.Warn("Pikkart AR Video", "cannot pause this video since it is fullscreen"); return(false); } if ((mVideoState == VIDEO_STATE.NOT_READY) || (mVideoState == VIDEO_STATE.ERROR)) { Log.Warn("Pikkart AR Video", "cannot pause this video if it is not ready"); return(false); } bool result = false; mMediaPlayerLock.Lock(); if (mMediaPlayer != null) { if (mMediaPlayer.IsPlaying) { try { mMediaPlayer.Pause(); } catch (Exception e) { mMediaPlayerLock.Unlock(); Log.Error("Pikkart AR Video", "could not pause playback"); } mVideoState = VIDEO_STATE.PAUSED; result = true; } } mMediaPlayerLock.Unlock(); return(result); }
/** * \brief unload a media file, media player and related data * @return true on success */ public bool unload() { mMediaPlayerLock.Lock(); if (mMediaPlayer != null) { try { mMediaPlayer.Stop(); } catch (Exception e) { mMediaPlayerLock.Unlock(); Log.Error("Pikkart AR Video", "Could not start playback"); } mMediaPlayer.Release(); mMediaPlayer = null; } mMediaPlayerLock.Unlock(); mVideoState = VIDEO_STATE.NOT_READY; mFullscreen = false; mAutoStart = false; mSeekPosition = -1; mMovieUrl = ""; return(true); }
public void loadVideo(String video, VIDEO_STATE videoState, VideoEnded videoEnded = null) { currentPlayingVideo = video; Uri pathUri = new Uri(String.Format("ms-appx:///Assets/Videos/{0}.mp4", video)); MediaSource source = MediaSource.CreateFromUri(pathUri); getFocusedMediaPlayerElement().MediaPlayer.MediaEnded -= MediaPlayer_MediaEnded; switchFocusedMediaPlayers(); getFocusedMediaPlayerElement().MediaPlayer.MediaEnded += MediaPlayer_MediaEnded; if (videoState == VIDEO_STATE.LOOP) { getFocusedMediaPlayerElement().MediaPlayer.IsLoopingEnabled = true; } else { getFocusedMediaPlayerElement().MediaPlayer.IsLoopingEnabled = false; } this.videoState = videoState; if (videoEnded != null) { this.videoEnded = videoEnded; } else { this.videoEnded = null; } getFocusedMediaPlayerElement().Source = source; getFocusedMediaPlayerElement().MediaPlayer.Play(); animateMediaPlayers(); }
/** * \brief Callback for interface MediaPlayer.OnPreparedListener * @param mediaPlayer */ public void OnPrepared(MediaPlayer mp) { mVideoState = VIDEO_STATE.READY; // If requested an immediate play if (mAutoStart) { play(); } }
/** * \brief request the video to be played * @return true on success */ public bool play() { if (mFullscreen) { mPlayFullScreenIntent.PutExtra("autostart", true); if (mSeekPosition != -1) { mPlayFullScreenIntent.PutExtra("seekposition", mSeekPosition); } mPlayFullScreenIntent.PutExtra("requestedorientation", (int)ScreenOrientation.Landscape); mPlayFullScreenIntent.PutExtra("movieurl", mMovieUrl); mParentActivity.StartActivity(mPlayFullScreenIntent); return(true); } else { if ((mVideoState == VIDEO_STATE.NOT_READY) || (mVideoState == VIDEO_STATE.ERROR)) { Log.Warn("Pikkart AR Video", "cannot play this video if it is not ready"); return(false); } mMediaPlayerLock.Lock(); if (mSeekPosition != -1) { try { mMediaPlayer.SeekTo(mSeekPosition); } catch (Exception e) { } } else { try { mMediaPlayer.SeekTo(0); } catch (Exception e) { } } try { mMediaPlayer.Start(); } catch (Exception e) { Log.Error("Pikkart AR Video", "could not start playback"); } mVideoState = VIDEO_STATE.PLAYING; mMediaPlayerLock.Unlock(); return(true); } }
/** * \brief Callback for interface MediaPlayer.OnBufferingUpdateListener * @param mediaPlayer * @param i the buffering percentage (int from 0 to 100) */ public void OnBufferingUpdate(MediaPlayer mp, int percent) { mVideoState = VIDEO_STATE.BUFFERING; if (percent == 100) { mVideoState = VIDEO_STATE.READY; } mMediaPlayerLock.Lock(); if (mMediaPlayer != null) { if (mp == mMediaPlayer) { mCurrentBufferingPercent = percent; } } mMediaPlayerLock.Unlock(); }
/** * \brief load a media file, either from file or from web * @param url file path or url (String) * @param playFullscreen whatever should play in fullscreen or in AR * @param autoStart auto-start when ready * @param seekPosition start position (in milliseconds) * @return true on success */ public bool load(string url, bool playFullscreen, bool autoStart, int seekPosition) { mMediaPlayerLock.Lock(); mSurfaceTextureLock.Lock(); //if it's in a different state than NOT_READY don't load it. unload() must be called first! if ((mVideoState != VIDEO_STATE.NOT_READY) || (mMediaPlayer != null)) { Log.Warn("Pikkart AR Video", "Already loaded"); mSurfaceTextureLock.Unlock(); mMediaPlayerLock.Unlock(); return(false); } //if AR video (not fullscreen) was requested create and set the media player //we can play video in AR only if a SurfaceTexture has been created if (!playFullscreen && (Build.VERSION.SdkInt >= BuildVersionCodes.IceCreamSandwich) && mSurfaceTexture != null) { mMediaPlayer = new MediaPlayer(); //first search for the video locally, then check online AssetFileDescriptor afd = null; bool fileExist = true; try { afd = mParentActivity.Assets.OpenFd(url); } catch (IOException e) { fileExist = false; } if (afd == null) { fileExist = false; } try { if (fileExist) { mMovieUrl = url; mMediaPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); afd.Close(); } else { string URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)[a-z0-9-]+(\\.[a-z0-9-]+)+((:[0-9]+)|)+([/?].*)?$"; //should be ok Java.Util.Regex.Pattern p = Java.Util.Regex.Pattern.Compile(URL_REGEX); Matcher m = p.Matcher(url); //replace with string to compare if (m.Find()) { mMovieUrl = url; mMediaPlayer.SetDataSource(mMovieUrl); } } try { mMediaPlayer.SetOnPreparedListener(this); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.StackTrace); } mMediaPlayer.SetOnBufferingUpdateListener(this); mMediaPlayer.SetOnCompletionListener(this); mMediaPlayer.SetOnErrorListener(this); mMediaPlayer.SetAudioStreamType(Stream.Music); mMediaPlayer.SetSurface(new Surface(mSurfaceTexture)); mFullscreen = false; mAutoStart = autoStart; mMediaPlayer.PrepareAsync(); mSeekPosition = seekPosition; } catch (IOException e) { Log.Error("Pikkart AR Video", "Error while creating the MediaPlayer: " + e.ToString()); mMovieUrl = ""; mVideoState = VIDEO_STATE.ERROR; mMediaPlayerLock.Unlock(); mSurfaceTextureLock.Unlock(); return(false); } } else { //play full screen if requested or old android mPlayFullScreenIntent = new Intent(mParentActivity, typeof(FullscreenVideoPlayer)); mPlayFullScreenIntent.SetAction(Android.Content.Intent.ActionView); mFullscreen = true; mMovieUrl = url; mSeekPosition = seekPosition; mVideoState = VIDEO_STATE.READY; } mSurfaceTextureLock.Unlock(); mMediaPlayerLock.Unlock(); return(true); }
/** * \brief Callback for interface MediaPlayer.OnCompletionListener * @param mediaPlayer */ public void OnCompletion(MediaPlayer mp) { mVideoState = VIDEO_STATE.END; }