我們使用構建應用程序在Xamarin。在應用程序中,我們需要播放視頻,所以我們爲此編寫了一些代碼。但是,該視頻並未播放,Android上的應用程序崩潰,同時拋出一個通用錯誤。與Xamarin播放視頻格式
這是代碼:
VideoContainer.cs
using System;
using Xamarin.Forms;
using System.Collections.Generic;
namespace MyApp
{
public class VideoView : View
{
public Action StopAction;
public VideoView()
{
Console.WriteLine("VideoView loaded");
}
public static readonly BindableProperty FileSourceProperty =
BindableProperty.Create<VideoView,string>(
p => p.FileSource,string.Empty);
public string FileSource {
get { return (string)GetValue (FileSourceProperty); }
set { SetValue (FileSourceProperty, value); }
}
public void Stop(){
if(StopAction != null)
StopAction();
}
}
}
VideoViewRender.cs
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Media;
using Android.Content.Res;
using Java.Lang;
using MyApp.Droid;
[assembly: ExportRenderer(typeof(VideoView), typeof(VideoViewRenderer))]
//
namespace MyApp.Droid
{
public class VideoViewRenderer : ViewRenderer
{
VideoView videoview;
MediaPlayer player;
MediaController mediaController;
Handler handler = new Handler();
public VideoViewRenderer()
{
Console.WriteLine("VideoViewRenderer loaded");
}
public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
{
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
}
void play(string fullPath)
{
AssetFileDescriptor afd = Forms.Context.Assets.OpenFd(fullPath);
if (afd != null)
{
player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
player.Prepare();
player.Start();
Control.Layout(0, 200, 400, 600);
player.Pause();
}
}
public void SurfaceCreated(ISurfaceHolder holder)
{
player.SetDisplay(holder);
}
public override bool OnTouchEvent(MotionEvent e)
{
mediaController.Show();
return false;
}
//--MediaPlayerControl methods----------------------------------------------------
public void Start()
{
player.Start();
}
public void Pause()
{
player.Pause();
}
public int Duration
{
get
{
return player.Duration;
}
}
public int CurrentPosition
{
get
{
return player.CurrentPosition;
}
}
public void SeekTo(int i)
{
player.SeekTo(i);
}
public bool IsPlaying
{
get
{
return player.IsPlaying;
}
}
public int BufferPercentage
{
get
{
return 0;
}
}
public int AudioSessionId
{
get
{
return 0;
}
}
public bool CanPause()
{
return true;
}
public bool CanSeekBackward()
{
return true;
}
public bool CanSeekForward()
{
return true;
}
//--------------------------------------------------------------------------------
}
}
什麼情況是,VideoView loaded
登錄到控制檯,但VideoViewRenderer loaded
不是。我們從the Xamarin forum獲得了此代碼,但無法成功實施。我們做錯了什麼?
請注意,張貼鏈接到您自己的GitHub的項目,但沒有附帶代碼講解如何使用它可以被看作是[自我推銷(http://meta.stackexchange.com/q/57497/284827 )由一些。 –
對不起 - 我分叉它一會兒回來,不是想邀功。 –