I'm trying to use the following code to retrieve video files to play to a user:
public class VideoController : Controller
{
public VideoResult GetMP4Video(string videoID)
{
if (User.Identity.IsAuthenticated)
{
string clipLocation = string.Format("{0}\\Completed\\{1}.mp4", ConfigurationManager.AppSettings["VideoLocation"].ToString(), videoID);
using (FileStream stream = new FileStream(clipLocation, FileMode.Open))
{
FileStreamResult fsResult = new FileStreamResult(stream, "video/mp4");
VideoResult result = new VideoResult(ReadFully(fsResult.FileStream), "video/mp4");
return result;
}
}
else
{
return null;
}
}
private static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[32 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
For displaying to the client I'm using Media Element:
<!-- Video Player Here -->
<video width="640" height="360" poster="@Url.Content(string.Format("~/Videos/{0}_2.jpg", Model.VideoID))" controls="controls" preload="none">
<!-- MP4 for Safari, IE9, iPhone, iPad, Android, and Windows Phone 7 -->
<source type="video/mp4" src="@Url.Action("GetMP4Video", "Video", new { videoID = Model.VideoID })" />
<!-- Flash fallback for non-HTML5 browsers without JavaScript -->
<object width="320" height="240" type="application/x-shockwave-flash" data="@Url.Content("~/Scripts/ME/flashmediaelement.swf")">
<param name="movie" value="@Url.Content("~/Scripts/ME/flashmediaelement.swf")" />
<param name="flashvars" value="controls=true&file=@Url.Action("GetMP4Video", "Video", new { videoID = Model.VideoID })" />
<!-- Image as a last resort -->
<img src="myvideo.jpg" width="320" height="240" title="No video playback capabilities" />
</object>
</video>
The problem is though that the file doesn't seem to play or at least not consistently. Also seeking in the video doesn not appear to work properly either. I guess my question is is this a acceptable way to serve a video to a user? If so what have I got wrong? I think its important that I'm very new to video and I'm very much learning as I go. Any help would be appreciated.