在应用程序中将 Youtube 视频保存到 iPhone
在应用程序中播放 Youtube 视频非常简单,并且有详细的文档记录。
这样做有两个问题:
- 关闭 Youtube 播放器后,如果用户想再次播放,则必须再次等待在线流媒体
- 无法离线播放(在家加载视频以便在路上观看)
有人有代码可以:
- 下载YouTube 视频到文档文件夹并显示下载进度
- 通过从文档文件夹加载文件来播放下载的视频(意味着即使未连接到互联网)
Playing Youtube video in the app is easy and well documented around.
There are two problems with that:
- after closing Youtube player, if user wants to play it again it has to wait for online streaming again
- can't play offline (load video at home to watch on the road)
Does anyone have code to:
- download Youtube video to documents folder and show progress of download
- play downloaded video by loading file from documents folder (meaning even when not connected to the internet)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要从 YouTube 下载视频:
NSTemporaryDirectory()
中或文档目录中的临时命名文件中)打开。sendSynchronousRequest:returningResponse:error:
。connection:didReceiveResponse:
委托方法中,读出要下载的数据长度,以便正确更新进度条。connection:didReceiveData:
委托方法中,将数据写入输出流/文件句柄并根据需要更新进度条。connectionDidFinishLoading:
或connection:didFailWithError:
中,关闭输出流/文件句柄并根据需要重命名或删除临时文件。要播放它,只需使用 NSURL 的
fileURLWithPath:
创建一个指向 Documents 目录中本地文件的 URL,然后像播放任何远程视频一样播放它。To download the video from YouTube:
NSTemporaryDirectory()
or a temp-named file in your Documents directory).sendSynchronousRequest:returningResponse:error:
, of course.connection:didReceiveResponse:
delegate method, read out the length of data to be downloaded for proper updating of the progress bar.connection:didReceiveData:
delegate method, write the data to the output stream/file handle and update the progress bar as necessary.connectionDidFinishLoading:
orconnection:didFailWithError:
, close the output stream/file handle and rename or delete the temporary file as appropriate.To play it back, just use NSURL's
fileURLWithPath:
to create a URL pointing to the local file in the Documents directory and play it as you would any remote video.我使用过此项目中的类:https://github.com/larcus94/LBYouTubeView
它对我来说效果很好。
我可以下载 youtube 视频。
我使用了这段代码:
您可以使用上面帖子中描述的技术显示下载进度。
Ive used classes from this project: https://github.com/larcus94/LBYouTubeView
It works fine for me.
I can download youtube videos.
I used this code:
You can show progress of downloading using technique described in the posts above.
这是我的示例: https://github.com/comonitos/youtube_video
我使用 PSYouTubeExtractor.h 类Peter Steinberger 它可以获取 youtube mp4 视频 url,并且下载和观看不是问题
NSURLConnection
+
NS通知中心
+
PSYouTube提取器
+
NSMutableData
Here is my example: https://github.com/comonitos/youtube_video
I used PSYouTubeExtractor.h class by Peter Steinberger It can get youtube mp4 video url and than downloading and viewing is not a problem
NSURLConnection
+
NSNotificationCenter
+
PSYouTubeExtractor
+
NSMutableData
检查这些项目 -
https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube
这些绝对会对你有帮助!
check these projects -
https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube
these will definitely help you!!
我个人不知道如何下载 youtube 视频(代码太大,无法在此处给出答案)。
不过,此处提供了完整的 YouTube 下载示例。
这是一个名为 LoadTube 的开源 YouTube 下载器:这里是源代码的链接。
I don't personally know how to download youtube videos (and the code is too big to put in an answer here).
However, here's a complete youtube downloading example here.
It's an open source youtube downloader called LoadTube: here's the a link to the source code.
我会播放视频并找出临时文件的存储位置。如果您可以访问它,请将其复制到某个文档文件夹中以供离线查看。
I would play the video and figure out where the temp file is being stored. If you can get access to it, copy it into some document folder for offline viewing.