JavaScript appears to be disabled. We recommend you enable JavaScript while visiting this site.

(All original content on this site is licensed under the Creative Commons License Attribution-Noncommercial-No Derivative Works 3.0.)

Dynamically generating a list of videos for OSFLV Player with ASP.NET

I've had this code for a while, but here's some simple code to pull a listing of Flash videos (FLV) from a directory, display them in a drop down, and have a video player dynamically generated based on what's picked.

This uses OSFLV Player, version 3 specifically, but can be tweaked for the current (as of this post) version 4.0.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>FLV video player</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<a href="Default.aspx">Refresh listing</a><br/>
		<asp:DropDownList ID="DropDownList1" runat="server">
		</asp:DropDownList>
		<asp:Button ID="Button1" runat="server" Text="View" onclick="Button1_Click" />
    </div>
    <div id="videoPlayer" runat="server"></div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class _Default : System.Web.UI.Page {

	int videoWidth = 1000; // = 200/170 ratio
	int videoHeight = 680; // 400x340 = default

	string playerLocation = "osflv_player_v3/player.swf";

	protected void Page_Load(object sender, EventArgs e) {

		if (!IsPostBack) {
			DirectoryInfo directory = new DirectoryInfo(Server.MapPath("/videos"));

			FileInfo[] files = directory.GetFiles();

			foreach (FileInfo file in files) {
				if (file.Extension == ".flv") {
					DropDownList1.Items.Add(file.Name.Remove(file.Name.Length - 4));
				}
			}
		}

	}
	protected void Button1_Click(object sender, EventArgs e) {
		string video = "/videos/" + DropDownList1.SelectedItem.Text + ".flv";

		videoPlayer.InnerHtml = "<object width='" + videoWidth.ToString() + "' height='" + videoHeight.ToString() + "' id='flvPlayer2'>";
		videoPlayer.InnerHtml += "<param name='allowFullScreen' value='true'>";
		videoPlayer.InnerHtml += "<param name='movie' value='" + playerLocation + "?movie=" + video + "&fgcolor=0x0b7ba4&bgcolor=0x333333&autoload=on&autorewind=on&autoplay=on&volume=5'>";
		videoPlayer.InnerHtml += "";
		videoPlayer.InnerHtml += "<embed src='" + playerLocation + "?movie=" + video + "&fgcolor=0x0b7ba4&bgcolor=0x333333&autoload=on&autorewind=on&autoplay=on&volume=5' width='" + videoWidth.ToString() + "' height='" + videoHeight.ToString() + "' allowFullScreen='true' type='application/x-shockwave-flash'>";
		videoPlayer.InnerHtml += "</object>";
		videoPlayer.InnerHtml += "<br/><span style='color:#ccc;'>" + video + "</span>";
	}
}

A default Web.config file can be created and used, and a videos directory is checked for the listing of videos.

For ease, I recommend using Cassini to run the Web site on your own computer (creating a batch file similar to what is noted there).

Tags: ,

Categories: tutorials/guides

blog comments powered by Disqus