您的位置:首页 > 其它

利用FMS3来播放H.264格式的mp4高清视频

2008-04-25 09:03 543 查看
利用FMS3来播放H.264格式的mp4高清视频
先安装Flash Media Server 3.0
安装后安装包中自带了个视频实例
具体客户端代码如下:
import mx.video.*;

msg_box.text = "Enter stream URL and click 'Go' to play.";
var so:SharedObject = SharedObject.getLocal("FMS_VOD_Client");
var counter = 0;
// Initial Stage setup
Stage.align = "T";
Stage.scaleMode = "noScale";
var resize_on_start_play;

// ******* Initializing URL combo-box data ******
if( so !=null){
// Default URLs.
my_cb.text = "rtmp://localhost/vod/sample.flv";
my_cb.addItem("rtmp://localhost/vod/sample.flv");
my_cb.addItem("rtmp://localhost/vod/AdobeBand_640.flv");
my_cb.addItem("rtmp://localhost/vod/mp4:AdobeBand_300K_H264.mp4");
my_cb.addItem("rtmp://localhost/vod/mp4:AdobeBand_800K_H264.mp4");
my_cb.addItem("rtmp://localhost/vod/mp4:AdobeBand_1500K_H264.mp4");
if (so.data.listURL == null){
so.data.listURL = new Array();
}
else{
// Pick up entries from local shared object.
for (var i =5; i< so.data.listURL.length; i++) {
my_cb.addItemAt(i,so.data.listURL[i]);
}
}
}

// Utility function to format time in two digits.
toTimeString = function (input){
if(input >9)
return (input.toString());
else
return ("0"+input.toString());
}

// Update duration and playback status on each update.
onStateChange = function(){
msg_box.text = my_FLVPlybk.state;
//Timing information printing
if(my_FLVPlybk.state == "playing" && my_FLVPlybk.totalTime ){
var current_hour = Math.floor(my_FLVPlybk.playheadTime/(60*60));
var current_minute = Math.floor((my_FLVPlybk.playheadTime/60)%60);
var current_second = Math.floor(my_FLVPlybk.playheadTime)%60;
var total_hour = Math.floor(my_FLVPlybk.totalTime/(60*60));
var total_minute = Math.floor((my_FLVPlybk.totalTime/60)%60);
var total_second = Math.floor(my_FLVPlybk.totalTime)%60;

play_head_text.text = toTimeString(current_hour)+":"+toTimeString(current_minute)+":"+toTimeString(current_second);
play_head_text.text += "/"+toTimeString(total_hour)+":"+toTimeString(total_minute)+":"+toTimeString(total_second);
}
// Sometimes size information is recieve when FLVPlayback component goes into playing state for
// the first time. Forcing a resize event as special handling for this case.
if (my_FLVPlybk.state == "playing" && resize_on_start_play == true){
my_FLVPlybk.autoSize = false;
my_FLVPlybk.autoSize = true;

}
}

//
onConnect = function(){
// remove all messages box content
msg_box_2.text = " ";
msg_box_2._height = 10;

// *************
// Updating the ComboBox items
// *************
var duplicate_found = false;
for(var i = 0; i < my_cb.length; i++)
{
if(my_cb.getItemAt(i).label == my_cb.text){
duplicate_found = true;
}
}
if(duplicate_found == false){
if(my_cb.length > 13){
// Remove the 6th URL, first 5 URLs will always stay intact
my_cb.removeItemAt(5);
}
my_cb.addItem(my_cb.text);
}

if(so !=null){
for (var i =0; i< my_cb.length; i++){
so.data.listURL[i]= my_cb.getItemAt(i).label;
}
so.flush();
}
//******** ComboBox update ends here *****
if(counter == 0){
attachMovie("FLVPlayback", "my_FLVPlybk", 10, {width:640, height:460, x:10, y:142});
my_FLVPlybk.playPauseButton = playpausebtn;
my_FLVPlybk.seekBar = seekbar;
my_FLVPlybk.muteButton = mutebtn;
}
else{
if(my_FLVPlybk.activeVideoPlayerIndex != 0){
trace ("closing the video player");
my_FLVPlybk.closeVideoPlayer(my_FLVPlybk.activeVideoPlayerIndex);
}
}
counter++;
my_FLVPlybk.activeVideoPlayerIndex = counter;
my_FLVPlybk.visibleVideoPlayerIndex = counter;
my_FLVPlybk.maintainAspectRatio = true;
my_FLVPlybk.autoSize = true;
resize_on_start_play = false;
play_head_text.text = "";
size_text.text = "";

//center the FLVPlayback component when FLV is ready to play
listenerObject.resize = function(eventObject:Object):Void {
if(my_FLVPlybk.preferredWidth == 0 && my_FLVPlybk.preferredHeight == 0){
resize_on_start_play = true;
return;
}
// resize or reposition FLVPlayback component
startx = 10;
starty = 142;
widthMax = 640;
heightMax = 450;
if(my_FLVPlybk.width > widthMax || my_FLVPlybk.height > heightMax){
my_FLVPlybk.autoSize = false;
my_FLVPlybk.setSize(widthMax,heightMax);
}
else
{
my_FLVPlybk._x = startx + (widthMax - my_FLVPlybk.width)/2;
my_FLVPlybk._y = starty + (heightMax - my_FLVPlybk.height)/2;

}

var size = (my_FLVPlybk.width/my_FLVPlybk.preferredWidth)*(my_FLVPlybk.height/my_FLVPlybk.preferredHeight)*100;
size_text.text = "size: "+Math.floor(size)+"%";
};

listenerObject.playheadUpdate = function(eventObject:Object):Void {
onStateChange();
};
my_FLVPlybk.addEventListener("resize", listenerObject);
my_FLVPlybk.addEventListener("stateChange", onStateChange);
my_FLVPlybk.addEventListener("playheadUpdate", listenerObject);

my_FLVPlybk.contentPath = my_cb.text;
my_FLVPlybk.autoPlay = true;
trace(my_FLVPlybk.contentPath);
}

submitURL.addEventListener("click",onConnect);
var listenerObject:Object = new Object();
listenerObject.enter = function(eventObject:Object) {
onConnect();
};
my_cb.addEventListener("enter", listenerObject)

// Routine to validate the current flash player version
// useful when the swf is directly loaded in a standalone flash player.
versionCheck = function()
{
var retVal = false;
// The swf requires flashplayer version > = 9,0,60,xxx
contentMajorVersion = 9;
contentMinorVersion = 0;
contentMajorRevision = 60;
playerVersion = System.capabilities.version;
var onlyVersion = playerVersion.substr( playerVersion.indexOf(" ")+1);
var versionDetails:Array = onlyVersion.split(",");
if(versionDetails.length == 4)
{
var playerMajorVersion = versionDetails[0];
var playerMinorVersion = versionDetails[1];
var playerMajorRevision = versionDetails[2];
if(playerMajorVersion > contentMajorVersion){
retVal = true;
}
else if( (playerMajorVersion == contentMajorVersion) && (playerMinorVersion > contentMinorVersion)){
retVal= true;
}
else if( (playerMajorVersion == contentMajorVersion) && (playerMinorVersion == contentMinorVersion)
&& (playerMajorRevision >= contentMajorRevision)){
retVal = true;
}
}
return retVal;
}

// version number check
if( !versionCheck())
{
// Probably the flash player version is older, show warning to the user.
version_msg_box.text = "Warning: The version of Flash Player you have installed does not support H.264, RTMPE and SWF Verification. To upgrade Flash Player, please visit http://www.adobe.com/go/flashplayer/";
}

服务器端代码如下:

/*
* application.onAppStart:
* is called when application load. It contains (VOD) Video-on-Demand
* application specific initializations.
*/
application.onAppStart = function()
{

// Turning on the Authentication by default
this.HTMLDomainsAuth = true;
this.SWFDomainsAuth = true;

// Populating the list of domains which are allowed to host HTML file
// which in turn can embed a SWF to connect to this application
this.allowedHTMLDomains = this.readValidDomains("allowedHTMLdomains.txt","HTMLDomains");

// Populating the list of domains which are allowed to host a SWF file
// which may connect to this application
this.allowedSWFDomains = this.readValidDomains("allowedSWFdomains.txt", "SWFDomains");

}

/*
* application.validate:
* function to validate a given URL by matching through a list of
* allowed patterns.
*
* Parameters:
* url: contains the input url string.
* patterns: Array; an array of permmited url patterns.
*
* return value:
* true; when 'url domain" contains a listed domains as a suffix.
* false; otherwise.
*/

application.validate = function( url, patterns )
{
// Convert to lower case
url = url.toLowerCase();
var domainStartPos = 0; // domain start position in the URL
var domainEndPos = 0; // domain end position in the URL

switch (url.indexOf( "://" ))
{
case 4:
if(url.indexOf( "http://" ) ==0)
domainStartPos = 7;
break;
case 5:
if(url.indexOf( "https://" ) ==0)
domainStartPos = 8;
break;
}
if(domainStartPos == 0)
{
// URL must be HTTP or HTTPS protocol based
return false;
}
domainEndPos = url.indexOf("/", domainStartPos);
if(domainEndPos>0)
{
colonPos = url.indexOf(":", domainStartPos);
if( (colonPos>0) && (domainEndPos > colonPos))
{
// probably URL contains a port number
domainEndPos = colonPos; // truncate the port number in the URL
}
}
for ( var i = 0; i < patterns.length; i++ )
{
var pos = url.lastIndexOf( patterns[i]);
if ( (pos > 0) && (pos < domainEndPos) && (domainEndPos == (pos + patterns[i].length)) )
return true;
}
return false;
}

/*
* application.onConnect:
* Implementation of the onConnect interface function (optional).
* it is invoked whenever a client connection request connection. VOD uses this
* function to authenticate the connection source domain and authorizes only
* for a subscriber request.
*/

application.onConnect = function( p_client, p_autoSenseBW )
{
//Add security here
p_client.writeAccess = ""; // prevents creating shared object or live streams.

// Authenticating HTML file's domain for the request :
// Don't call validate() when the request is from localhost
// or HTML Domains Authentication is off.
if ((p_client.ip != "127.0.0.1") && application.HTMLDomainsAuth && !this.validate( p_client.pageUrl, this.allowedHTMLDomains ) )
{
trace("unknown pageurl " + p_client.pageUrl + ", rejecting connection");
return false;
}

// Authenticating the SWF file's domain for the request :
// Don't call validate() when the request is from localhost
// or SWF Domains Authentication is off.
if ((p_client.ip != "127.0.0.1") && application.SWFDomainsAuth && !this.validate( p_client.referrer, this.allowedSWFDomains ) )
{
trace("unknown referrer " + p_client.referrer + ", rejecting connection");
return false;
}

// As default, all clients are disabled to access raw audio and video and data bytes in a stream
// through the use of BitmapData.draw() and SoundMixer.computeSpectrum()., Please refer
// Stream Data Access doccumentations to know flash player version requirement to support this restriction.
// Access permissions can be allowed for all by uncommenting the following statements

//p_client.audioSampleAccess = "/";
//p_client.videoSampleAccess = "/";

this.acceptConnection(p_client);
trace("Accepted the connection from IP:"+ p_client.ip);

// A connection from Flash 8 & 9 FLV Playback componant based client
// requires the following code.

if (p_autoSenseBW)
p_client.checkBandwidth();
else
p_client.call("onBWDone");

}

/*
* Client.prototype.getPageUrl
* Public API to return URL of the HTML page.
*
*/

Client.prototype.getPageUrl = function() {
return this.pageUrl;
}

/*
* Client.prototype.getReferrer
* Public API to return Domain URL of the client SWF file.
*
*/
Client.prototype.getReferrer = function() {
return this.referrer;
}

/*
* Client.prototype.getStreamLength
* Function to return the total length of the stream
*
*/

Client.prototype.getStreamLength = function(p_streamName) {
return Stream.length(p_streamName);
}

/*
* application.readValidDomains
* Function to read Allowed domain file
* Parameters:
* fileName:
* name of the file in the application directory
* which contains one valid domain name per line. This file can contain
* comments followed by a '#' as the very first charector in that line.
* a non-comment entry with a space is considered as an error case.
*
* returns
* an array in which each entry contains a domain name
* listed in the file.
*/

application.readValidDomains = function( fileName , domainsType )
{
var domainFile = new File(fileName);
var domainsArray = new Array();
var index = 0;
var lineCount = 0;
var tempLine;
domainFile.open("text", "read");

// Read the file line-by-line and fill the domainsArray
// with valid entries
while (domainFile.isOpen && ! domainFile.eof() )
{

tempLine = domainFile.readln();
lineCount++;
if( !tempLine || tempLine.indexOf("#") == 0)
{
continue;
}
tempLine = tempLine.trim();
if(tempLine.indexOf(" ")!=-1)
{
trace("undesired <space>, domain entry ignored. "+fileName+":"+(lineCount+1));
}
else
{
domainsArray[index] = tempLine.toLowerCase();
index++;

if(tempLine == "*")
{
switch (domainsType){

case "HTMLDomains":
trace ("Found wildcard (*) entry: disabling authentication for HTML file domains ") ;
application.HTMLDomainsAuth = false;
break;

case "SWFDomains":
trace ("Found wildcard (*) entry: disabling authentication for SWF file domains ") ;
this.SWFDomainsAuth = false;
break;

default:
// Do nothing
break;
}
}
}
} // End while

// Something is wrong! the domains file must be accessible.
if( !domainFile.isOpen){
trace("Error: could not open '"+fileName+"', rejecting all clients except localhost. ");

}
else
{
domainFile.close();
}

return domainsArray;
}

/**
* String.prototype.trim:
* Function to trim spaces in start an end of an input string.
* returns:
* a trimmed string without any leading & ending spaces.
*
*/
String.prototype.trim = function () {

return this.replace(/^/s*/, "").replace(//s*$/, "");
}

Flash Player要求9.0.115.0或更高。




fms3_6




fms3_7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: