스트리밍 비디오에 문제가 있습니다./IOS 클라이언트 용 스트리밍 비디오 문제 (ASP.NET WEB API 2에서 개발 된 서버)
개인 비동기 무효 WriteContentToStream (스트림의 OutputStream, HttpContent 내용, TransportContext transportContext)를 스트리밍
1) The first method: if (Request.Headers.Range != null) { try { var httpResponce = Request.CreateResponse(); httpResponce.Content = new PushStreamContent((Action) WriteContentToStream);는 웹 클라이언트와 안드로이드 클라이언트에 근무하지만, 아이폰 OS 클라이언트가 '아무튼 쇼 비디오. 그 문제는 비디오의 코덱과 관련이있을 수 있습니다 (하지만 Apple을 권장하는 코덱이나 http 헤더를 사용했습니다).return httpResponce; } catch (Exception ex) { return new HttpResponseMessage(HttpStatusCode.InternalServerError); } } else { return new HttpResponseMessage(HttpStatusCode.RequestedRangeNotSatisfiable); }
/방법 : 나는이 방법을 ASP.NET 웹 API 2 서버를 개발 및 구현 { string relativeFilePath = "~/App_Data/Videos/4.mp4"; 시도하십시오. { var filePath = System.Web.Hosting.HostingEnvironment.MapPath (relativeFilePath);
int bufferSize = 1000; byte[] buffer = new byte[bufferSize]; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { int totalSize = (int)fileStream.Length; while (totalSize > 0) { int count = totalSize > bufferSize ? bufferSize : totalSize; int sizeOfReadedBuffer = fileStream.Read(buffer, 0, count); await outputStream.WriteAsync(buffer, 0, sizeOfReadedBuffer); totalSize -= sizeOfReadedBuffer; } } } catch (HttpException ex) { if (ex.ErrorCode == -2147023667) { return; } } finally { outputStream.Close(); }
}
2) 두 번째 방법 :
두 방법 모두
public HttpResponseMessage Test() { if (Request.Headers.Range != null) { try { string relativeFilePath = "~/App_Data/Videos/4.mp4"; var filePath = System.Web.Hosting.HostingEnvironment.MapPath(relativeFilePath); HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent); partialResponse.Headers.AcceptRanges.Add("bytes"); var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, new MediaTypeHeaderValue("video/mp4")); return partialResponse; } catch (Exception) { return new HttpResponseMessage(HttpStatusCode.InternalServerError); } } else { return new HttpResponseMessage(HttpStatusCode.RequestedRangeNotSatisfiable); } }
답변 해 주셔서 감사합니다.