You are on page 1of 4

using using using using using using using

System; System.Collections.Generic; System.Drawing; System.Linq; MonoTouch.Foundation; MonoTouch.MediaPlayer; MonoTouch.UIKit;

namespace VideoDemoForiPad { public partial class VideoViewController : UIViewController { #region Constructors // The IntPtr and initWithCoder constructors are required for controllers that need // to be able to be created from a xib rather than from managed code public VideoViewController (IntPtr handle) : base(handle) { Initialize (); } [Export("initWithCoder:")] public VideoViewController (NSCoder coder) : base(coder) { Initialize (); } public VideoViewController () : base("VideoViewController", null) { Initialize (); } void Initialize () { } #endregion #region Fields MPMoviePlayerController _player; NSObject _preloadObserver, _playbackObserver;

const string NOTIFICATION_PRELOAD_FINISH = "MPMoviePlayerContentPreloadDidFinishNotification"; const string NOTIFICATION_PLAYBACK_FINISH = "MPMoviePlayerPlaybackDidFinishNotification"; #endregion // Fields #region Implementation public override void ViewDidLoad () { base.ViewDidLoad (); this.PlayVideo("fractal.mp4"); } void PlayVideo(string videoFileName) { if (_player == null) { _player = new MPMoviePlayerController(); var center = NSNotificationCenter.DefaultCenter; _preloadObserver = center.AddObserver( NOTIFICATION_PRELOAD_FINISH, (notify) => { _player.Play(); notify.Dispose(); }); _playbackObserver = center.AddObserver( NOTIFICATION_PLAYBACK_FINISH, (notify) => { // Call Stop() to loop the video. //_player.Stop(); notify.Dispose(); }); // Add the movie player UI as a subview.

var f = this.View.Frame; _player.View.Frame = new RectangleF( 0, 0, // Swap the width and height. f.Height + 20, f.Width - 20); this.Add(_player.View); } // The video file must have its Build Action set // to 'Content' in MonoDevelop for this to work. var url = NSUrl.FromFilename(videoFileName); _player.ContentUrl = url; _player.Play(); } public override void ViewDidUnload () { base.ViewDidUnload (); if (_player != null) { var center = NSNotificationCenter.DefaultCenter; center.RemoveObserver(_preloadObserver); center.RemoveObserver(_playbackObserver); _preloadObserver = null; _playbackObserver = null; _player.Dispose (); _player = null; } } public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation orientation) { // Only allow the video to be displayed in landscape mode. return orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight; }

#endregion // Implementation } }

You might also like