iOS 视频流传输并随后存储在设备上
到目前为止,我知道如何流式传输视频以及如何下载它然后流式传输它,但这里有一个棘手的地方:流式传输一次,将其存储在设备上,并在将来从设备上播放它。
这可能吗?
So far I know how to stream a video and how to download it and afterwards stream it, but here's the tricky bit: streaming it once, storing it on the device and in the future play it from the device.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不太确定如何获取流,但请查看 AVAssetWriter、AVAssetWriterInput 和 AVAssetWriterPixelBufferAdaptor,一旦收到数据,您应该能够使用以下方法将数据附加到像素缓冲区适配器:
不确定它是否适合您但通过一些摆弄,您应该能够调整您的输入以匹配此方法。有很多用于设置 writer 的示例代码
Not quite sure here how you get your stream but look in to the AVAssetWriter, AVAssetWriterInput and AVAssetWriterPixelBufferAdaptor and as soon as you receive data you should be able to append the data to the to the pixel buffer adaptor using:
not sure it will work for you but with some fiddling you should be able to adapt your input to match this method. There are lots of example code for setting up the writer
保存视频非常容易。执行与此类似的操作:
您真正需要更改的唯一内容是 * MovieObject *,在每个步骤中更改一次。
It's quite easy to save the video. Do something similar to this:
The only thing you really have to change there is * MovieObject *, once in each step.
我知道你想要实现什么,我只有一个解决方法。我必须实现相同的行为,最终从服务器流式传输视频并在流式传输旁边下载它。下次用户尝试流式传输视频时,请确定它是否已下载到磁盘,否则再次流式传输。正常情况下视频可以正常下载并可以离线查看。
I know what you want to achieve, I only got a workaround. I had to implement the same behavior and ended up with streaming the video from the server and downloading it next to streaming. Next time the user tries to stream the video determine whether it was downloaded to disk, otherwise stream it again. In a normal case the video was downloaded properly and could be reviewed offline.
fileURLWithPath
:isDirectory:
初始化并返回新创建的 NSURL 对象作为具有指定路径的文件 URL。
参数
path
NSURL 对象将表示的路径。路径应该是有效的系统路径。如果路径以波形符开头,则必须首先使用 stringByExpandingTildeInPath 对其进行扩展。如果路径是相对路径,则将其视为相对于当前工作目录。
对此参数传递 nil 会产生异常。
isDir
一个布尔值,指定在解析相对路径组件时是否将路径视为目录路径。如果路径指示目录,则传递 YES,否则传递 NO。
返回值
使用路径初始化的 NSURL 对象。
可用性
适用于 iOS 2.0 及更高版本。
您无法同时进行流式传输和保存,尤其是对于大型视频文件,因为 Apple 文档表示您必须使用传输流进行 HTTP 实时流式传输。
and
fileURLWithPath:isDirectory:
Initializes and returns a newly created NSURL object as a file URL with a specified path.
Parameters
path
The path that the NSURL object will represent. path should be a valid system path. If path begins with a tilde, it must first be expanded with stringByExpandingTildeInPath. If path is a relative path, it is treated as being relative to the current working directory.
Passing nil for this parameter produces an exception.
isDir
A Boolean value that specifies whether path is treated as a directory path when resolving against relative path components. Pass YES if the path indicates a directory, NO otherwise.
Return Value
An NSURL object initialized with path.
Availability
Available in iOS 2.0 and later.
You can't stream it and save it at the same time, especially with large video files as the Apple doc sais that you must use a transport stream for HTTP Live Streaming.
ASIHttpRequest 可能会让您的生活更轻松。
从您的委托中处理此问题:
在获取数据时使用
data
进行任何数据转码,并将其实时推送到您的AVAssetWriter
或电影播放器层,无论您使用什么。完成后,资产仍应保存,以便您稍后获取。ASIHttpRequest might make your life easier.
From your delegate, handle this:
Do whatever data transcoding with
data
as you get it and push it off to yourAVAssetWriter
or movie player layer in real time, whatever you are using. When you're done, the asset should still be saved so you can get it later.