Skip to content

Instantly share code, notes, and snippets.

@prucha
Last active August 29, 2015 14:22
Show Gist options
  • Save prucha/8b485c99fe87d63008f5 to your computer and use it in GitHub Desktop.
Save prucha/8b485c99fe87d63008f5 to your computer and use it in GitHub Desktop.
Play Video via Native Video Player (Swift vs Xamarin)
// This code snippet was taken from a very simple iPad App, that plays an embedded video file
// via the native IOS video player. The App was set up with a single 'View'.
// The code below is from the 'ViewController', which contains the only significant code
// to be added to the standard template.
// The full project was created in Xcode, using the Apple Swift language
import UIKit
import MediaPlayer
class Swift_VideoPlayerViewController: UIViewController {
var moviePlayer : MPMoviePlayerController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
playVideo()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playVideo()
{
let path = NSBundle.mainBundle().pathForResource("video", ofType: "mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer{
player.view.frame = self.view.bounds
player.prepareToPlay()
player.scalingMode = .AspectFit
self.view.addSubview(player.view)
}
}
}
// This code snippet was taken from a very simple iPad App, that plays an embedded video file
// via the native IOS video player. The App was set up with a single 'View'.
// The code below is from the 'ViewController', which contains the only significant code
// to be added to the standard template.
// The full project was created in Xamarin Studio, using the C# language
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MediaPlayer;
namespace Xamarin_VideoPlayer
{
public partial class Xamarin_VideoPlayerViewController : UIViewController
{
MPMoviePlayerController moviePlayer;
public Xamarin_VideoPlayerViewController (IntPtr handle) : base (handle)
{
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
}
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
// Release any cached data, images, etc that aren't in use.
moviePlayer = new MPMoviePlayerController (NSUrl.FromFilename ("video.mp4"));
RectangleF screenSize = UIScreen.MainScreen.Bounds;
//Console.WriteLine("screenSize - Width: " + screenSize.Width + ", Height: " + screenSize.Height );
//since we in landscape, the longest dimension needs to be applied to the width of this 'view'
moviePlayer.View.Frame = (screenSize.Width > screenSize.Height) ?
new RectangleF (0, 0, screenSize.Width, screenSize.Height) : new RectangleF (0, 0, screenSize.Height, screenSize.Width);
moviePlayer.RepeatMode = MPMovieRepeatMode.One;
moviePlayer.SetFullscreen (true, false);
moviePlayer.PrepareToPlay();
moviePlayer.ScalingMode = MPMovieScalingMode.AspectFit;
View.AddSubview (moviePlayer.View);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment