发布于2026-07-22 阅读(0)
扫一扫,手机访问
先来看一段老式播放器的代码——这是6.4版本的。而新式播放器呢,从MediaPlayer 9.0开始才出现,也就是说,只有安装了9.0或更高版本,这段代码才能正常工作。
接下来是新式播放器的代码,比老版本简洁不少:
每个参数都有自己的使命,下面逐一拆解:
以下列出了RealPlayer ActiveX控件库(Version 1.0)的所有函数与方法,有兴趣的可以深入研究:
function GetSource: WideString;
procedure SetSource(const lpszNewValue: WideString);
function GetConsole: WideString;
procedure SetConsole(const lpszNewValue: WideString);
function GetControls: WideString;
procedure SetControls(const lpszNewValue: WideString);
function GetNoLabels: WordBool;
procedure SetNoLabels(bNewValue: WordBool);
function GetAutoStart: WordBool;
procedure SetAutoStart(bNewValue: WordBool);
function GetAutoGotoURL: WordBool;
procedure SetAutoGotoURL(bNewValue: WordBool);
function GetVolume: Smallint;
procedure SetVolume(nVol: Smallint);
function GetMute: WordBool;
procedure SetMute(bMute: WordBool);
function GetLoop: WordBool;
procedure SetLoop(bVal: WordBool);
function GetImageStatus: WordBool;
procedure SetImageStatus(bEnable: WordBool);
function GetPacketsTotal: Integer;
function GetPacketsReceived: Integer;
function GetPacketsOutOfOrder: Integer;
function GetPacketsMissing: Integer;
function GetPacketsEarly: Integer;
function GetPacketsLate: Integer;
function GetBandwidthA verage: Integer;
function GetBandwidthCurrent: Integer;
procedure DoPlayPause;
procedure DoStop;
procedure DoNextItem;
procedure DoPrevItem;
function CanPlayPause: WordBool;
function CanStop: WordBool;
function HasNextItem: WordBool;
function HasPrevItem: WordBool;
function HasNextEntry: WordBool;
function HasPrevEntry: WordBool;
procedure DoNextEntry;
procedure DoPrevEntry;
procedure AboutBox;
procedure EditPreferences;
procedure HideShowStatistics;
function IsStatisticsVisible: WordBool;
procedure DoGotoURL(const url: WideString; const target: WideString);
procedure DoPlay;
procedure DoPause;
function GetPosition: Integer;
function GetPlayState: Integer;
function GetLength: Integer;
function GetTitle: WideString;
function GetAuthor: WideString;
function GetCopyright: WideString;
function GetClipWidth: Integer;
function GetClipHeight: Integer;
function CanPlay: WordBool;
function CanPause: WordBool;
procedure SetPosition(lPosition: Integer);
function GetNumLoop: Integer;
procedure SetNumLoop(lVal: Integer);
function GetCenter: WordBool;
procedure SetCenter(bVal: WordBool);
function GetNoLogo: WordBool;
procedure SetNoLogo(bVal: WordBool);
function GetMaintainAspect: WordBool;
procedure SetMaintainAspect(bVal: WordBool);
function GetBackgroundColor: WideString;
procedure SetBackgroundColor(const pVal: WideString);
function GetStereoState: WordBool;
function GetLiveState: WordBool;
function GetShowStatistics: WordBool;
procedure SetShowStatistics(bVal: WordBool);
function GetShowPreferences: WordBool;
procedure SetShowPreferences(bVal: WordBool);
function GetShowAbout: WordBool;
procedure SetShowAbout(bVal: WordBool);
function GetOriginalSize: WordBool;
procedure SetOriginalSize;
function GetDoubleSize: WordBool;
procedure SetDoubleSize;
function GetFullScreen: WordBool;
procedure SetFullScreen;
function GetEnableContextMenu: WordBool;
procedure SetEnableContextMenu(bVal: WordBool);
function GetEnableOriginalSize: WordBool;
procedure SetEnableOriginalSize(bVal: WordBool);
function GetEnableDoubleSize: WordBool;
procedure SetEnableDoubleSize(bVal: WordBool);
function GetEnableFullScreen: WordBool;
procedure SetEnableFullScreen(bVal: WordBool);
function GetEnableMessageBox: WordBool;
procedure SetEnableMessageBox(bVal: WordBool);
procedure SetTitle(const pVal: WideString);
procedure SetAuthor(const pVal: WideString);
procedure SetCopyright(const pVal: WideString);
function GetWantKeyboardEvents: WordBool;
procedure SetWantKeyboardEvents(bWantsEvents: WordBool);
function GetWantMouseEvents: WordBool;
procedure SetWantMouseEvents(bWantsEvents: WordBool);
function GetNumEntries: Smallint;
function GetCurrentEntry: Smallint;
function GetEntryTitle(uEntryIndex: Smallint): WideString;
function GetEntryAuthor(uEntryIndex: Smallint): WideString;
function GetEntryCopyright(uEntryIndex: Smallint): WideString;
function GetEntryAbstract(uEntryIndex: Smallint): WideString;
procedure SetCanSeek(bCanSeek: WordBool);
function GetCanSeek: WordBool;
function GetBufferingTimeElapsed: Integer;
function GetBufferingTimeRemaining: Integer;
function GetConnectionBandwidth: Integer;
function GetPreferedLanguageString: WideString;
function GetPreferedLanguageID: Integer;
function GetUserCountryID: Integer;
function GetNumSources: Smallint;
function GetSourceTransport(nSourceNum: Smallint): WideString;
function GetWantErrors: WordBool;
procedure SetWantErrors(bVal: WordBool);
function GetShuffle: WordBool;
procedure SetShuffle(bVal: WordBool);
function GetVersionInfo: WideString;
function GetLastMessage: WideString;
function GetLastErrorSeverity: Integer;
function GetLastErrorRMACode: Integer;
function GetLastErrorUserCode: Integer;
function GetLastErrorUserString: WideString;
function GetLastErrorMoreInfoURL: WideString;
procedure SetPreFetch(bVal: WordBool);
function GetPreFetch: WordBool;
procedure SetRegion(const pVal: WideString);
function GetRegion: WideString;
function GetIsPlus: WordBool;
function GetConsoleEvents: WordBool;
procedure SetConsoleEvents(bVal: WordBool);
function GetDRMInfo(const pVal: WideString): WideString;
property ControlInterface: IRealAudio read GetControlInterface;
property DefaultInterface: IRealAudio read GetControlInterface;
这里我们重点说说Windows Media Player控件在网页中的各种控制。
除了最基础的播放、暂停、停止,媒体播放器还提供了扫描(Scanning)和搜索(Seeking)功能。扫描类似于录像机的快进快倒,而搜索则可以直接跳到剪辑中标记的特定时间点。
指定要播放的媒体名称有两种方式:设置FileName属性,或调用Open方法。如果AutoStart为true,设置FileName后就会自动播放;否则需要调用Play方法。注意Open方法是异步的,不会等待其他进程结束。
媒体播放器还提供了类似录像机的控制方法:Play、Stop、Pause,以及PlayCount属性(设置播放次数)、AutoRewind属性(停止时是否返回开头)。
ShowAudioControls设为true,可在控制栏添加声音控件。CanScan和AllowScan都设为true。CurrentPosition时触发。CanSeek为true;要搜索到标记点,需CanSeekToMarkers为true。通过以下属性控制哪些元素显示:
PREVIEWDURATION指定每个剪辑的预览时间,默认10秒。使用GetMediaInfoString方法可返回剪辑或节目的相关信息:文件名、标题、描述、作者、版权、级别、URL(logo icon、watermark、banner的地址)。如果元文件中也指定了信息,则优先返回元文件中的内容。
在元文件中,可以用PARAM标签为每个剪辑添加自定义信息,通过GetMediaParameter方法访问。
以下属性返回大小和时间信息:
IsDurationValid判断是否有效(广播视频长度不可预测)。使用.smi文件添加字幕,相关属性:
流媒体文件中可嵌入脚本命令,与特定时间同步。命令由一对Unicode字符串组成:第一个标识命令类型,第二个指定要执行的内容。当流播放到对应时间时,控件会向网页发送ScriptCommand事件。
媒体播放器自动处理以下内嵌脚本命令:
InvokeURLs属性控制是否自动处理,设为false则只触发事件,可选择性处理。基地址由BaseURL指定。FileName设置为脚本命令提供的文件并播放。此类型无法禁止。EVENT元素,执行其中的条目。EVENT元素并打开匹配标题,但不播放,直到收到同名真实事件。SendMouseMoveEvents和SendMouseClickEvents设为true。支持的事件:MouseDown、MouseUp、MouseMove、Click、DbClick。SendKeyboardEvents设为true。支持的事件:KeyDown、KeyUp、KeyPress。流状态属性:PlayState(播放状态)、OpenState(打开状态)、Bandwidth(带宽)。
支持事件:OpenStateChange(打开状态改变,需SendOpenStateChangeEvents为true)、PlayStateChange(播放状态改变,需SendPlayStateChangeEvents为true)、EndOfStream(流结束时触发)、NewStream(打开新流时触发)。
网络接收属性:ReceptionQuality(接收质量)、ReceivedPackets(已收包数)、LostPackets(丢失包数)。
缓冲属性:BufferingTime(缓冲时间)、BufferingCount(缓冲次数)、BufferingProgress(缓冲进程)、Buffering(缓冲事件)。
媒体播放器内建错误处理功能,可通过对话框或状态栏显示错误信息。也可自定义错误处理:将SendErrorEvents设为true,则不显示错误框,而是发送错误事件;设为false则显示错误框。
支持的错误事件:Error(危险错误)、Warning(非危险错误)。
检测错误信息:HasError(是否有错误)、ErrorCode(错误代码)、ErrorDescription(错误描述)、ErrorCorrection(校正方式)。
媒体播放器将CD视为单一音频流,每个音轨开头有标记。要在网页中播放CD,将FileName设为CDAUDI(注意冒号):
CD Audio Playback Example
之后用Play方法播放。也可在Windows媒体元文件中指定播放顺序和音轨信息,示例如下:
CD Audio with the Media Player
Windows Media Technologies
(c) 1999, Microsoft, Inc.
Track 1: Title 1
Track 2: Title 2
Track 3: Title 3
Track 4: Title 4
使用以下代码:
注意:编辑文章时请使用HTML代码模式,否则代码会以文本形式显示。音乐文件不宜过大,否则影响浏览速度,甚至导致浏览器停止响应。
音乐文件的URL即音乐文件的网址。获取方法:在音乐链接上右键 -> 复制快捷方式,然后粘贴到代码中。
如果加了背景音乐后无法正常收听,可能有两个原因:一是音乐地址错误(误把HTML结尾的网址当成音乐地址),二是音乐文件太大,需要很长时间才能播放。
以WMP 9.0控件为例,在Delphi 7.0中常用的属性与方法如下(Ja vaScript中用法几乎完全相同)。假设控件名为wmp。
| 属性/方法名 | 说明 |
|---|---|
| [基本属性] | |
| URL:String | 指定媒体位置,本机或网络地址 |
| uiMode:String | 播放器界面模式,可选Full, Mini, None, Invisible |
| playState:integer | 播放状态:1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪 |
| enableContextMenu:Boolean | 启用/禁用右键菜单 |
| fullScreen:boolean | 是否全屏显示 |
| [controls] wmp.controls | |
| controls.play | 播放 |
| controls.pause | 暂停 |
| controls.stop | 停止 |
| controls.currentPosition:double | 当前进度 |
| controls.currentPositionString:string | 当前进度,字符串格式,如“00:23” |
| controls.fastForward | 快进 |
| controls.fastReverse | 快退 |
| controls.next | 下一曲 |
| controls.previous | 上一曲 |
| [settings] wmp.settings | |
| settings.volume:integer | 音量,0-100 |
| settings.autoStart:Boolean | 是否自动播放 |
| settings.mute:Boolean | 是否静音 |
| settings.playCount:integer | 播放次数 |
| [currentMedia] wmp.currentMedia | |
| currentMedia.duration:double | 媒体总长度 |
| currentMedia.durationString:string | 媒体总长度,字符串格式,如“03:24” |
| currentMedia.getItemInfo(const string) | 获取当前媒体信息:"Title"=标题,"Author"=艺术家,"Copyright"=版权,"Description"=描述,"Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址 |
| currentMedia.setItemInfo(const string) | 通过属性名设置媒体信息 |
| currentMedia.name:string | 同 currentMedia.getItemInfo("Title") |
| [currentPlaylist] wmp.currentPlaylist | |
| currentPlaylist.count:integer | 当前播放列表包含的媒体数 |
| currentPlaylist.Item[integer] | 获取或设置指定项目媒体信息,子属性同wmp.currentMedia |
常用的差不多就这些,更完整的资料请查阅Windows Media Player 9 SDK文档。另外,当前媒体在当前播放列表中的编号,这个属性在SDK中似乎没有直接暴露,如果遇到相关需求,可以结合实际项目自行探索。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8