2017-02-11 60 views
0

我有一個本地.mov文件添加到我的Xamarin項目。我想在Main.storyboard文件中添加的視圖中播放它。View中的MediaPlayer無法播放。 Xamarin iOS

我在做什麼錯?

using Foundation; 
using System; 
using UIKit; 
using MediaPlayer; 

namespace XamarinTest 
{ 
    public partial class heartbeatPage : UIViewController 
    { 
     MPMoviePlayerController moviePlayer; 
    public heartbeatPage (IntPtr handle) : base (handle) 
     { 
      moviePlayer = new MPMoviePlayerController(NSUrl.FromFilename("heartAnimation4.mov")); 
      moviePlayer.ScalingMode = MPMovieScalingMode.AspectFit; 

     heartView = moviePlayer.View; //HeartView is a view I added in the storyboard. 

     moviePlayer.PrepareToPlay(); 
     moviePlayer.Play(); 
    } 
} 

}

回答

0

試試我的方法:

AVPlayer _player; 
AVPlayerLayer _playerLayer; 
AVAsset _asset; 
AVPlayerItem _playerItem; 

_asset = AVAsset.FromUrl(NSUrl.FromFilename("YourClip.mp4")); 

_playerItem = new AVPlayerItem(_asset); 
_player = new AVPlayer(_playerItem); 

RegisterNotification(); 

_playerLayer = AVPlayerLayer.FromPlayer(_player); 
_playerLayer.Frame = heartView.Frame; 
_playerLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill; 

heartView.Layer.AddSublayer(_playerLayer); 

_player.Play(); 
+0

謝謝!我之前已經使用AVPlayer工作,但我只需要在屏幕的一部分上顯示視頻,並在其餘部分顯示文字。 –

相關問題