Play a Video Using AVPlayer
- PDF for offline use
- Sample Code:
- Related SDKs:
Let us know how you feel about this
This recipe shows how to play a video using an AVPlayer and AVPlayerLayer.
using AVFoundation;to the top of the class file.
Add the following class variable in a
AVPlayer player; AVPlayerLayer playerLayer; AVAsset asset; AVPlayerItem playerItem;
Add a movie file named sample.m4v to the project. There is a sample file included in the project download for this recipe.
- Ensure that the file's Build Action is set to Content. You can do this by right-clicking on the file and selecting Build Action from the context menu that appears.
In the ViewDidLoad method, create an
AVAssetand pass it to an
asset = AVAsset.FromUrl (NSUrl.FromFilename ("sample.m4v")); playerItem = new AVPlayerItem (asset);
Create an AVPlayer and pass it the AVPlayerItem created above.
player = new AVPlayer (playerItem);
AVPlayerinstance and add as a sublayer to the view’s layer.
playerLayer = AVPlayerLayer.FromPlayer (player); playerLayer.Frame = View.Frame; View.Layer.AddSublayer (playerLayer);
Playmethod of the
AVPlayerinstance to play the video.
AVPlayer is part of the
AVFoundation framework and is available in the