Multiple Videos - show all files in the directory - asp.net-mvc

How can I show in 1 view all the vidoes in a directory? What is the best approach for this? Here is the simple code I am using for 1 video. Also is there a way to disable right click download?
Thanks!
<video controls="controls" width="465" height="315">
<source src="~/Videos/Bunny2.mp4" type="video/mp4">
</video>

The Directory.EnumerateFiles method will provide a list of files in a directory. It returns just a list of full filepath strings. I'd use that in a class constructor, pulling info like Name and Type out of the string, and do something like this:
// Controller action
public ActionResult GetVideos()
{
var videos = Directory.EnumerateFiles("Videos/");
var videoList = new List<Video>();
foreach (var video in videos)
{
videoList.Add(new Video(video));
}
return View(videoList);
}
// Razor view
#model IEnumerable<Video>
#foreach (var video in Model)
{
<video controls="controls" width="465" height="315">
<source src="#video.Source" type="#video.Type">
</video>
}
To prevent download, disable the context menu. The MDN Element: contextmenu event has the following to disable this, though I would use a class you've applied to all the video elements.
noContext = document.getElementById('noContextMenu');
noContext.addEventListener('contextmenu', e => {
e.preventDefault();
});

Related

ASP NET Core 3.1 Streaming Video Doesn't Work on iOS and Safari

I've seen numerous questions with people having troubles with implementing video streaming for iOS and Safari. I have tried implementing all of the common pitfalls that I have read about in other questions, but I have not been successful.
I am trying to stream a video from an ASP NET Core 3.1 controller. This works completely correctly in basically every browser, except on iOS and Safari, for which the video just doesn't load (the player just shows a timebar with length 0:00).
I have shown the controller action and the view that I am using to stream the video below. As you can see, I have added EnableRangeProcessing, set the content length and added the playsinline attribute, which are all the main issues people talk about with iOS and Safari.
Can anyone see any other potential problem here?
public async Task<ActionResult> StreamVideo(int id)
{
var range = HttpContext.Request.GetTypedHeaders()?.Range?.Ranges.FirstOrDefault();
Stream data = await _service.DownloadVideo(id);
if (range is null) Response.ContentLength = data.Length;
else
{
Response.ContentLength = (range.To.HasValue ? range.To.Value + 1 : data.Length) - (range.From ?? 0);
}
return new FileStreamResult(data, "video/mp4") { EnableRangeProcessing = true };
}
<video style="max-height:100%; max-width: 100%;" controls="controls" preload="auto" playsinline>
<source src="#Url.Action("StreamVideo", "Attachment", new { Id = id })" type="video/mp4">
</video>

PhoneGap iOS can't play video using HTML 5

I'm building phonegap app and I need to play video in it. I know in order to play video on Android, I need to use one of the plugins available, but to play video on iOS, I can use HTML 5 video tag. I have these lines of code:
<div id="video">
<video id="video01" width="300" height="300" controls>
<source src="images/test2.mp4" type="video/mp4" />
</video>
</div>
I can see "video box", but I cannot play video for some reason. In console I don't see any errors.
Video I'm using is from this page: http://www.quirksmode.org/html5/tests/video.html
And in iPhone Safari I can load MP4 version from that page without any problems.
I know it has been a while, but I was looking at some of my old questions, and I've noticed that I completely forgot to post solution to my problem.
In the end I've managed to fix the issue using Cordova File api, and solution looks smth. like this:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
console.log("gotFS");
getFolder(fileSystem, "video", function(folder) {
filePath = folder.toURL() + "\/" + "videotest.mp4";
playVideo(filePath);
}, function() {
console.log("failed to get folder");
});
},
function() {
console.log("failed to get filesystem");
});
function playVideo(uri) {
var player = document.getElementById("videoPlayer");
var source = document.createElement("source");
source.src = uri;
source.type = "video/mp4";
player.appendChild(source);
player.load();
}

Unable to remove related videos in youtube

I have embedded a video with magnificpopup script.
I am unable to remove related videos at the end of the reproduction of a youtbe embedded.
Here is the code I have tried:
<a class="popup-youtube" href="https://www.youtube.com/watch?rel=0&feature=player_detailpage&v=83UHRghhars">
but does not work.
The following code nor reproduce the video
<a class="popup-youtube" href="https://www.youtube.com/watch?feature=player_detailpage&v=83UHRghhars&rel=0">
if i put the embed code that youtube tell me:
//www.youtube.com/embed/83UHRghhars?rel=0
the video does not work. What I am doing wrong?
Here is a way to do it by adding additional javascript as demonstrated here.
<script>
jQuery(window).load(function(){
jQuery('a[href*="youtube.com/watch"]').magnificPopup({
type: 'iframe',
iframe: {
patterns: {
youtube: {
index: 'youtube.com',
id: 'v=',
src: '//www.youtube.com/embed/%id%?rel=0&autoplay=1'
}
}
}
});
});
</script>
You can remove the '&autoplay=1' if you do not need it.
There is an issue here. I did this workaround.
$('.js-home-video .js-popup-youtube').magnificPopup({
// your default config here
// All below settings are default except the rel attribute in youtube-src
// This is here to remove the related videos from showing up after the video ends
// Adding rel=0 from url in html template stopped autoplay, hence this hack
iframe: {
markup: '<div class="mfp-iframe-scaler">'+
'<div class="mfp-close"></div>'+
'<iframe class="mfp-iframe" frameborder="0" allowfullscreen></iframe>'+
'</div>', // HTML markup of popup, `mfp-close` will be replaced by the close button
patterns: {
youtube: {
index: 'youtube.com/', // String that detects type of video (in this case YouTube). Simply via url.indexOf(index).
id: 'v=', // String that splits URL in a two parts, second part should be %id%
// Or null - full URL will be returned
// Or a function that should return %id%, for example:
// id: function(url) { return 'parsed id'; }
src: '//www.youtube.com/embed/%id%?autoplay=1&rel=0' // URL that will be set as a source for iframe.
}
}
}
}
If you are using a mixed gallery (e.g. one of images and videos intermixed), then you might want to override how magnific popup builds the youtube embed.
The default for youtube are
youtube: {
index: 'youtube.com/', // String that detects type of video (in this case YouTube). Simply via url.indexOf(index).
id: 'v=', // String that splits URL in a two parts, second part should be %id%
// Or null - full URL will be returned
// Or a function that should return %id%, for example:
// id: function(url) { return 'parsed id'; }
src: '//www.youtube.com/embed/%id%?autoplay=1' // URL that will be set as a source for iframe.
}
This means that the ID is everything following v= and it simply takes that and sticks autoplay=1 at the end when creating the embed code.
To change this, you can pass in any special iframe youtube patterns to be used:
iframe: {
patterns: {
youtube: {
src: '//www.youtube.com/embed/%id%?autoplay=1&rel=0&feature=player_detailpage'
}
}
}
Here, we adjusted the source so that it will build the embed using the id as normal and then stick your rel and feature parameters afterwards (along with autoplay)
If you do this, then leave your parameters off of the urls in the html mark up OR set the v= attribute at the end so that no extra parameters get tacked in when building the embed url.
The final look might be something like:
$('.gallery-list').magnificPopup({
delegate: 'a',
type: 'image',
gallery: {
enabled: true
},
iframe: {
patterns: {
youtube: {
src: '//www.youtube.com/embed/%id%?autoplay=1&rel=0'
}
}
}
});
And your element might be:
<div class="gallery-list">
<a class="popup-youtube mfp-iframe" href="https://www.youtube.com/watch?v=83UHRghhars">Click here</a>
<img class="mfp-image" href="http://domain/image.jpg" width="100" height="200" />
</div>
The mfp-iframe class on the video link tells magnific to use iframe functionality.
The initialization code above told magnific to use image by default, but the mfp-iframe css class will override that for a video.
Now, when someone clicks the video, magnific popup should grab the video id via the v= parameter and then build the embed code by using the id and then tacking on the extra autoplay and rel url parameters.
1) Go to jquery.magnific-popup.min.js or jquery.magnific-popup.js, whatever you embed on your website.
2) Use your text editor to Search and Replace as follows:
Search: youtube.com/embed/%id%?autoplay=1
Replace: youtube.com/embed/%id%?rel=0&autoplay=1
3) Hit 'Save'.
VoilĂ 

Dailymotion iframe api

I am using dailymotion api to play videos. Example is straight from the docs:
http://www.dailymotion.com/doc/api/player.html#api-reference
And its giving me errors in console:
<!-- This <div> tag will be replaced the <iframe> video player -->
<div id="player"></div>
<script>
// This code loads the Dailymotion Javascript SDK asynchronously.
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//api.dmcdn.net/all.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s);
}());
// This function init the player once the SDK is loaded
window.dmAsyncInit = function()
{
// PARAMS is a javascript object containing parameters to pass to the player if any (eg: {autoplay: 1})
var player = DM.player("player", {video: "x1bpnpt", width: "640", height: "360", params: {autoplay: 1}});
// 4. We can attach some events on the player (using standard DOM events)
player.addEventListener("apiready", function(e)
{
e.target.play();
});
};
</script>
http://jsfiddle.net/WpbJH/
But if I embed it directly in iframe then it works (it also displays some errors but not the ones from above):
<iframe src="http://www.dailymotion.com/embed/video/x1bpnpt?api=true&autoplay=1" width="640" height="360" frameborder="0"></iframe>
http://jsfiddle.net/MWhdC/
What is wrong?
As i am looking on it , js file is not proper loaded , please look at
e.src = document.location.protocol + '//api.dmcdn.net/all.js'; line
document.location.protocol prefix domain infront of //api.dmcdn.net/all.js .
So please look at it and let me know .
After few research, I found what cause this issue.
The video they are using as example gets report from third party advertising group, and need to report to it every step of the video.
Upload a video of your own, and use the id you get from it, you will see that you will not have console errors.
Hope that was helpful.

Destroying/unloading video object HTML5 on ios

I'm loading up multiple videos into one video object inside a webview of an ios app. After viewing a couple of videos, the app would crash, assuming from memory leaking. I'm looking for a way to properly dump the stream or destroy the previous video when video is paused and closed, or before loading up a new one. Any help would be much appreciated:
<video class="videoPlayer" preload="auto" controls="controls">
<source src="" type="video/mp4" />
</video>
function LoadNewVideo() {
video = document.getElementsByTagName('video')[0];
video.src = "assets/vids/vid" + vidSelect + ".mp4";
video.load(); // need this for the new video to load
video.play();
}
function StopVideo() {
video.pause();
// kill current video here
// i've tried the following codes but didn't work
video.src = '';
video.load();
}

Resources