public void OnTapped() { switch (State) { case VideoPlayerState.Playing: VideoPlayer.Pause(); State = VideoPlayerState.Paused; break; case VideoPlayerState.Paused: case VideoPlayerState.Stopped: case VideoPlayerState.NotStarted: VideoPlayer.Play(); State = VideoPlayerState.Playing; break; case VideoPlayerState.Hidden: break; case VideoPlayerState.Unkown: break; default: break; } }
private void configureButtonForState(VideoPlayerState state) { // make all true first playButton.interactable = true; pauseButton.interactable = true; stopButton.interactable = true; skipFowardButton.interactable = true; skipBackwardButton.interactable = true; // now enable some based on state switch (state) { case VideoPlayerState.play: playButton.interactable = false; break; case VideoPlayerState.pause: pauseButton.interactable = false; break; default: // stop pauseButton.interactable = false; stopButton.interactable = false; skipFowardButton.interactable = false; skipBackwardButton.interactable = false; break; } }
public override void onCreate(Bundle savedInstanceState) { base.onCreate(savedInstanceState); ContentView = R.layout.video_player_layout; setupViewElements(); mSmcLib = new Smc(); try { mSmcLib.initialize(BaseContext); } catch (SsdkUnsupportedException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); //TODO Handle exceptions. } Log.d("media control lib version", mSmcLib.VersionName); // Prepares assets used by sample. extractAssets(); // Restores saved state, after e.g. rotating. if (savedInstanceState != null) { mState = new VideoPlayerState(savedInstanceState); } else { mState = new VideoPlayerState(); // Initializes state with built-in content. File storageDir = new File(Environment.ExternalStorageDirectory, ASSETS_SUBDIR); File video = new File(storageDir, VIDEO_FILE); File subtitles = new File(storageDir, SUBTITLES_FILE); // Gets media information (title, artist, cover) MediaScannerConnection.scanFile(ApplicationContext, new string[] { video.AbsolutePath }, null, this); Uri mediaUri = Uri.fromFile(video); Uri subtitlesUri = Uri.fromFile(subtitles); mState.mMediaUri = mediaUri; mState.mSubtitlesUri = subtitlesUri; mState.mTitle = "Sample album"; string path = mediaUri.Path; if (path != null && path.LastIndexOf(".", StringComparison.Ordinal) != -1) { string ext = path.Substring(path.LastIndexOf(".", StringComparison.Ordinal) + 1); mState.mMimeType = MimeTypeMap.Singleton.getMimeTypeFromExtension(ext); } } }
public void OnTapped() { switch (State) { case VideoPlayerState.Playing: InstructionManager.Instance.ActionLog.Add(new ActionLogEntry(InstructionManager.Instance.CurrentStepNumber, ActionType.VideoPaused, DateTime.Now)); VideoPlayer.Pause(); State = VideoPlayerState.Paused; break; case VideoPlayerState.Paused: case VideoPlayerState.Stopped: case VideoPlayerState.NotStarted: if (State == VideoPlayerState.Paused) { InstructionManager.Instance.ActionLog.Add(new ActionLogEntry(InstructionManager.Instance.CurrentStepNumber, ActionType.VideoUnpaused, DateTime.Now)); } if (State == VideoPlayerState.Stopped) { InstructionManager.Instance.ActionLog.Add(new ActionLogEntry(InstructionManager.Instance.CurrentStepNumber, ActionType.VideoRestarted, DateTime.Now)); } if (State == VideoPlayerState.NotStarted) { InstructionManager.Instance.ActionLog.Add(new ActionLogEntry(InstructionManager.Instance.CurrentStepNumber, ActionType.VideoStarted, DateTime.Now)); } VideoPlayer.Play(); State = VideoPlayerState.Playing; break; case VideoPlayerState.Hidden: break; case VideoPlayerState.Unkown: break; default: break; } }
// GST polling thread function void KeepPolling() { while (isrunning) { switch (playerState) { case VideoPlayerState.STOPPED: break; case VideoPlayerState.LOADING: //get video width/height int w = 0, h = 0; //Query video information Gst.Buffer buf = appSink.PullBuffer(); if (buf != null) { Console.WriteLine(buf.Caps.ToString()); //string format = buf.Caps[0].GetValue("format").Val.ToString(); //Console.WriteLine("format: " + format); int.TryParse(buf.Caps[0].GetValue("width").Val.ToString(), out w); int.TryParse(buf.Caps[0].GetValue("height").Val.ToString(), out h); if (w * h != 0) { //Create decoded buffer lock (lockFrameBuf) { width = w; height = h; bufferY = new byte[width * height]; bufferU = new byte[width * height / 4]; bufferV = new byte[width * height / 4]; IntPtr src = buf.Data; Marshal.Copy(src, bufferY, 0, width * height); src = new IntPtr(src.ToInt64() + width * height); Marshal.Copy(src, bufferU, 0, width * height / 4); src = new IntPtr(src.ToInt64() + width * height / 4); Marshal.Copy(src, bufferV, 0, width * height / 4); isFrameNew = true; //Dispose handle to avoid memory leak //gst_mini_object_unref(buf.Handle); buf.Dispose(); } Console.WriteLine("PLAYING"); playerState = VideoPlayerState.PLAYING; continue; } } break; case VideoPlayerState.PLAYING: Gst.Buffer buf2 = appSink.PullBuffer(); if (buf2 != null) { lock (lockFrameBuf){ //Update buffer IntPtr src = buf2.Data; Marshal.Copy(src, bufferY, 0, width * height); src = new IntPtr(src.ToInt64() + width * height); Marshal.Copy(src, bufferU, 0, width * height / 4); src = new IntPtr(src.ToInt64() + width * height / 4); Marshal.Copy(src, bufferV, 0, width * height / 4); isFrameNew = true; //gst_mini_object_unref(buf2.Handle); //buf2.Dispose(); } buf2.Dispose(); } else { lock (lockFrameBuf) { //Clear buffer bufferY = new byte[width * height]; bufferU = new byte[width * height / 4]; bufferV = new byte[width * height / 4]; } playerState = VideoPlayerState.STOPPED; Console.WriteLine("STOPPED"); } break; case VideoPlayerState.PAUSED: //Do nothing break; default: //Do nothing break; } Thread.Sleep(10); } //Clean up this.PlayerState = VideoPlayerState.STOPPED; playBin.SetState(State.Null); playBin.Dispose(); appSink.SetState(State.Null); appSink.Dispose(); playBin = null; appSink = null; }
public void LoadVideo(string uri) { if (gstThread != null) { isrunning = false; gstThread.Join(); gstThread = new Thread(new ThreadStart(KeepPolling)); } if (playBin != null) { playerState = VideoPlayerState.STOPPED; Console.WriteLine("STOPPED"); //Dispose playbin2 and appsink playBin.SetState(State.Null); playBin.Dispose(); appSink.SetState(State.Null); appSink.Dispose(); //Create playbin2 and appsink playBin = new PlayBin2(); appSink = ElementFactory.Make("appsink", "sink") as AppSink; appSink.Caps = new Caps("video/x-raw-yuv", new object[] {}); //appSink.Caps = new Caps("video/x-raw-rgb", new object[] { "bpp", 24 }); appSink.Drop = true; appSink.MaxBuffers = 8; playBin.VideoSink = appSink; } else { //Create playbin2 and appsink playBin = new PlayBin2(); appSink = ElementFactory.Make("appsink", "sink") as AppSink; appSink.Caps = new Caps("video/x-raw-yuv", new object[] {}); //appSink.Caps = new Caps("video/x-raw-rgb", new object[] { "bpp", 24 }); appSink.Drop = true; appSink.MaxBuffers = 8; playBin.VideoSink = appSink; } //init variables texturesOK = false; width = 0; height = 0; //Set file uri string validUri = uri; if (!validUri.StartsWith("file://")) { validUri = "file://" + uri; } playBin.Uri = validUri; StateChangeReturn sr = playBin.SetState(State.Playing); Console.WriteLine(sr.ToString()); playerState = VideoPlayerState.LOADING; Console.WriteLine("LOADING:" + validUri); if (gstThread == null) { gstThread = new Thread(new ThreadStart(KeepPolling)); } isrunning = true; //Start polling thread...future thought, using async queue? gstThread.Start(); return; }
private void VideoPlayer_loopPointReached(VideoPlayer source) { State = VideoPlayerState.Stopped; }
public override void onCreate(Bundle savedInstanceState) { base.onCreate(savedInstanceState); ContentView = R.layout.video_player_layout; setupViewElements(); mSmcLib = new Smc(); try { mSmcLib.initialize(BaseContext); } catch (SsdkUnsupportedException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); //TODO Handle exceptions. } Log.d("media control lib version", mSmcLib.VersionName); // Prepares assets used by sample. extractAssets(); // Restores saved state, after e.g. rotating. if (savedInstanceState != null) { mState = new VideoPlayerState(savedInstanceState); } else { mState = new VideoPlayerState(); // Initializes state with built-in content. File storageDir = new File(Environment.ExternalStorageDirectory, ASSETS_SUBDIR); File video = new File(storageDir, VIDEO_FILE); File subtitles = new File(storageDir, SUBTITLES_FILE); // Gets media information (title, artist, cover) MediaScannerConnection.scanFile(ApplicationContext, new string[] {video.AbsolutePath}, null, this); Uri mediaUri = Uri.fromFile(video); Uri subtitlesUri = Uri.fromFile(subtitles); mState.mMediaUri = mediaUri; mState.mSubtitlesUri = subtitlesUri; mState.mTitle = "Sample album"; string path = mediaUri.Path; if (path != null && path.LastIndexOf(".", StringComparison.Ordinal) != -1) { string ext = path.Substring(path.LastIndexOf(".", StringComparison.Ordinal) + 1); mState.mMimeType = MimeTypeMap.Singleton.getMimeTypeFromExtension(ext); } } }
public void Init(VideoPlayerState state) { _playerState.CopyFrom(state); }
/// <summary> /// Loads the video. Setup playbin2 and start polling thread /// </summary> /// <param name='uri'> /// video URI. /// </param> public void LoadVideo(string uri){ if (gstThread != null) { isrunning = false; gstThread.Join(); gstThread = new Thread(new ThreadStart(KeepPolling)); } if(playBin != null){ playerState = VideoPlayerState.STOPPED; Console.WriteLine("STOPPED"); //Dispose playbin2 and appsink playBin.SetState(State.Null); playBin.Dispose(); appSink.SetState(State.Null); appSink.Dispose(); //Create playbin2 and appsink playBin = new PlayBin2(); appSink = ElementFactory.Make("appsink", "sink") as AppSink; //appSink.Caps = new Caps("video/x-raw-yuv", new object[]{}); appSink.Caps = new Caps("video/x-raw-rgb", new object[] { "bpp", 24 }); appSink.Drop = true; appSink.MaxBuffers = 1; playBin.VideoSink = appSink; }else{ //Create playbin2 and appsink playBin = new PlayBin2(); appSink = ElementFactory.Make("appsink", "sink") as AppSink; //appSink.Caps = new Caps("video/x-raw-yuv", new object[]{}); appSink.Caps = new Caps("video/x-raw-rgb", new object[] { "bpp", 24 }); appSink.Drop = true; appSink.MaxBuffers = 1; playBin.VideoSink = appSink; } //init variables texturesOK = false; width = 0; height = 0; //Set file uri string validUri = uri; if (!validUri.StartsWith("file://")) { validUri = "file://" + uri; } playBin.Uri = validUri; StateChangeReturn sr = playBin.SetState(State.Playing); Console.WriteLine(sr.ToString()); playerState = VideoPlayerState.LOADING; Console.WriteLine("LOADING:" + validUri); if (gstThread == null) { gstThread = new Thread(new ThreadStart(KeepPolling)); } //Start polling thread...future thought, using async queue? isrunning = true; gstThread.Start(); return; }
/// <summary> /// Polling thread. Pull buffer data from GST thread. /// </summary> void KeepPolling(){ while(isrunning){ switch (playerState) { case VideoPlayerState.STOPPED: break; case VideoPlayerState.LOADING: //get video width/height int w = 0, h = 0; //Query video information Gst.Buffer buf = appSink.PullBuffer(); if(buf != null){ Console.WriteLine(buf.Caps.ToString()); //string format = buf.Caps[0].GetValue("format").Val.ToString(); //Console.WriteLine("format: " + format); int.TryParse(buf.Caps[0].GetValue("width").Val.ToString(), out w); int.TryParse(buf.Caps[0].GetValue("height").Val.ToString(), out h); if (w * h != 0) { //Create decoded buffer lock (lockFrameBuf) { width = w; height = h; buffer = new byte[width * height * 3]; Marshal.Copy(buf.Data, buffer, 0, width * height * 3); isFrameNew = true; //Dispose handle to avoid memory leak //gst_mini_object_unref(buf.Handle); buf.Dispose(); } Console.WriteLine("PLAYING"); playerState = VideoPlayerState.PLAYING; continue; } } break; case VideoPlayerState.PLAYING: Gst.Buffer buf2 = appSink.PullBuffer(); if(buf2 != null){ lock(lockFrameBuf){ //Update buffer Marshal.Copy(buf2.Data, buffer, 0, width * height * 3); isFrameNew = true; //gst_mini_object_unref(buf2.Handle); //buf2.Dispose(); } buf2.Dispose(); }else{ lock (lockFrameBuf) { //Clear buffer buffer = new byte[width * height * 3]; } playerState = VideoPlayerState.STOPPED; Console.WriteLine("STOPPED"); } break; case VideoPlayerState.PAUSED: //Do nothing break; default: //Do nothing break; } Thread.Sleep(10); } //Clean up this.PlayerState = VideoPlayerState.STOPPED; playBin.SetState(State.Null); playBin.Dispose(); appSink.SetState(State.Null); appSink.Dispose(); playBin = null; appSink = null; }