2012-08-09 22 views
5

Estoy intentando utilizar el siguiente código para recuperar archivos de vídeo para jugar a un usuario:problemas de reproducción de archivos de vídeo utilizando MVC FileStream

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(); 
     } 
    } 
} 

Para mostrar al cliente que estoy usando Elementos Mediático:

<!-- 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&[email protected]("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> 

El problema es que el archivo no parece reproducirse o al menos no consistentemente. También parece que buscar en el video tampoco funciona bien. Supongo que mi pregunta es, ¿es esta una manera aceptable de mostrar un video a un usuario? Si es así, ¿qué tengo mal? Creo que es importante que soy muy nuevo en el video y estoy aprendiendo mucho sobre la marcha. Cualquier ayuda sería apreciada.

+0

Ahh whata buena pregunta el hombre que estoy haciendo lo mismo para! un proyecto propio ... no podemos especificar el archivo directo, entonces, ¿cómo podemos server streams mp4? – ppumkin

Respuesta

0

Sí, está tratando de poner un archivo mp4 servido por un servidor web en un reproductor que está incrustado en su archivo HTML, y no se reproducirá correctamente (a menos que el archivo sea comparativamente muy pequeño o esté teniendo un extremadamente rápido conexión a internet, para que el archivo se descargue rápidamente a la carpeta temporal del navegador).

Para transmitir correctamente archivos de video, siga cualquiera de los pasos a continuación.

  1. use Windows media Servidor/Flash servidor. Empuje su cámara web hacia el servidor mediante Windows Media Encoder o el codificador de medios flash, y utilice el enlace en vivo del servidor para vincularlo con su sitio web a través de cualquier reproductor adecuado (como jwplayer).

  2. Utilice Windows Media Encoder para transmitir su cámara web a cualquier persona que no tenga un servidor involucrado. cuando se inicia el codificador, obtendrá una URL para ver su transmisión, que puede usar para publicar en su sitio.

  3. usan servicios de transmisión de terceros, donde te dan un punto de publicación para publicar la transmisión de tu cámara web, y usan el enlace provisto por ellos para mostrarlo en tu sitio web. (Consulte con brighcove o Mogulus por LiveStream

Esperanza esto ayuda

1

Esto funciona para mí Adaptado de here:..

internal static void StreamVideo(string fullpath, HttpContextBase context) 
    { 
     long size, start, end, length, fp = 0; 
     using (StreamReader reader = new StreamReader(fullpath)) 
     { 

      size = reader.BaseStream.Length; 
      start = 0; 
      end = size - 1; 
      length = size; 
      // Now that we've gotten so far without errors we send the accept range header 
      /* At the moment we only support single ranges. 
      * Multiple ranges requires some more work to ensure it works correctly 
      * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 
      * 
      * Multirange support annouces itself with: 
      * header('Accept-Ranges: bytes'); 
      * 
      * Multirange content must be sent with multipart/byteranges mediatype, 
      * (mediatype = mimetype) 
      * as well as a boundry header to indicate the various chunks of data. 
      */ 
      context.Response.AddHeader("Accept-Ranges", "0-" + size); 
      // header('Accept-Ranges: bytes'); 
      // multipart/byteranges 
      // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 

      if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"])) 
      { 
       long anotherStart = start; 
       long anotherEnd = end; 
       string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") }); 
       string range = arr_split[1]; 

       // Make sure the client hasn't sent us a multibyte range 
       if (range.IndexOf(",") > -1) 
       { 
        // (?) Shoud this be issued here, or should the first 
        // range be used? Or should the header be ignored and 
        // we output the whole content? 
        context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size); 
        throw new HttpException(416, "Requested Range Not Satisfiable"); 

       } 

       // If the range starts with an '-' we start from the beginning 
       // If not, we forward the file pointer 
       // And make sure to get the end byte if spesified 
       if (range.StartsWith("-")) 
       { 
        // The n-number of the last bytes is requested 
        anotherStart = size - Convert.ToInt64(range.Substring(1)); 
       } 
       else 
       { 
        arr_split = range.Split(new char[] { Convert.ToChar("-") }); 
        anotherStart = Convert.ToInt64(arr_split[0]); 
        long temp = 0; 
        anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size; 
       } 
       /* Check the range and make sure it's treated according to the specs. 
       * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 
       */ 
       // End bytes can not be larger than $end. 
       anotherEnd = (anotherEnd > end) ? end : anotherEnd; 
       // Validate the requested range and return an error if it's not correct. 
       if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size) 
       { 
        context.Response.ContentType = MimeMapping.GetMimeMapping(fullpath); 
        context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size); 
        throw new HttpException(416, "Requested Range Not Satisfiable"); 
       } 
       start = anotherStart; 
       end = anotherEnd; 

       length = end - start + 1; // Calculate new content length 
       fp = reader.BaseStream.Seek(start, SeekOrigin.Begin); 
       context.Response.StatusCode = 206; 
      } 
     } 
     // Notify the client the byte range we'll be outputting 
     context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size); 
     context.Response.AddHeader("Content-Length", length.ToString()); 
     // Start buffered download 
     context.Response.WriteFile(fullpath, fp, length); 
     context.Response.End(); 

    } 
Cuestiones relacionadas