JavaScript

5 отговора 326 прегледа
#1

JavaScript

здравейте на всички
имам малък проблем - как с Script да извикам системна функция или по- точто да дам на Windows Media Player параметър PLAY а не OPEN

Отговор
в отговор на petrov #2

Re: JavaScript

Ami nai-dobre da popitash v "Web Design i HTM".
S plugIn-i mnogo ne sym se zanimaval, no shte ti paste-na tova, koeto namerih v MSDN-a za MediaPlaer-a (sigurno ima i oshte ama ne mi se rovi sega :-))

MediaPlayer.Play()//t.e. ako sym te razbral tova iskash da pusnesh

Description
Goal: To catch and handle events sent by the Windows Media Player using both the Microsoft® Internet Explorer ActiveX model and the Netscape Navigator Plug-in model.
The Netscape Navigator plug-in model does not allow events to be handled as easily as Internet Explorer does. Creating code to work with both Navigator and Explorer is often a difficult task. This sample gives a good taste of the kinds of code necessary to handle events across both formats.

More Details
In Navigator, we use the appObs proxy applet to send callback methods to the Media Player plug-in. This Java applet resides on the page, and "listens" to all of the events that the Media Player sends out. The applet itself can be installed on a page with the following code:
<script language="JavaScript">
<!--
if ( navigator.appName == "Netscape" )
{
//-- This next line ensures that any plugins just installed are updated in
//-- the Browser without quitting the browser.
navigator.plugins.refresh();
// We don't need the APPLET within IE, and APPLET is treated as an implied
// end to the OBJECT tag by the IE HTML parser.
// ***Please note that if you do not need to script events, you can safely
// remove the next two lines***
document.write("\x3C" + "applet MAYSCRIPT Code=NPDS.npDSEvtObsProxy.class" )
document.writeln(" width=5 height=5 name=appObs\x3E \x3C/applet\x3E")
}
//-->
</script>

Before an event is fired, an event observer must be enabled to trap the event. The following code segment illustrates how to enable event observers:

appObs.setByProxyDSEventNameObserver(plugin, true);

Each registered event observer requires a corresponding event handler. In JavaScript for Netscape Navigator, an event handler is declared using the following syntax:

function OnDSEventNameEvt (paramter, ...)
{
// code goes from here..
}

In the above snippets, EventName is the Media Player event that is being handled. You simply need to replace this with the name of the Media Player event that you wish to listen to.
Once the appObs applet instantiates, we need to set it to listen to browser events. In this sample, we're only going to be using the PlayStateChange event, and we'll do this when the page loads. First, we must call the appropriate function when the browser sends the onload event:


<BODY BGCOLOR="#ffffff" onload="loader()">

The following bit of code, which goes inside the main <SCRIPT> block, actually sets the appObs applet to use appropriate callback events:

var bControl; // This boolean variable holds TRUE if we're using an ActiveX
// platform, and FALSE otherwise.

function loader(){
if ((navigator.userAgent.indexOf('IE') > -1) && (navigator.platform == "Win32")) {
bControl = true;
} else {
var plugIn = document.MediaPlayer1;
bControl = false;
// The following line of code registers the PlayStateChange event
// in the appObs applet, so that when a PlayStateChange event occurs,
// the appObs Applet sends the appropriate callback method.
document.appObs.setByProxyDSPlayStateChangeObserver(plugIn,true);
}
document.MediaPlayer1.Play();
}

The callback method, as illustrated below, also resides in the main <SCRIPT> block.

function OnDSPlayStateChangeEvt (oldState, newState){
doPlayStateChanges(oldState,newState);
}

Since we're handling events in both Netscape Navigator and Microsoft Internet Explorer, the OnDSPlayStateChangeEvt callback method passes its information on to a function called doPlayStateChanges. The Internet Explorer event handler will do the same. This way we can handle events in both browsers using the same piece of code.
<SCRIPT FOR="MediaPlayer1" EVENT="PlayStateChange(oldState,newState)" LANGUAGE="Jscript">
doPlayStateChanges(oldState, newState);
</SCRIPT>

The doPlayStateChanges function will be defined in the main <SCRIPT> block:
function doPlayStateChanges(oldState,newState){
timerID = setTimeout("document.image1.src = \"play_low.gif\";",0);
timerID = setTimeout("document.image2.src = \"pause_low.gif\";",0);
timerID = setTimeout("document.image3.src = \"stop_low.gif\";",0);

switch(newState) {
case 0:
timerID = setTimeout("document.image3.src = \"stop_hi.gif\";",0);
break;
case 1:
timerID = setTimeout("document.image2.src = \"pause_hi.gif\";",0);
break;
case 2:
timerID = setTimeout("document.image1.src = \"play_hi.gif\";",0);
break;
}
}

Here, we're swapping the player images based on the current PlayState of the Media Player. You'll notice that we're using a setTimeout function for our DHTML. This is necessary because Netscape Navigator requires a new thread for DHTML called from the appObs proxy applet.
As usual, we'll add the code that instantiates the Media Player ActiveX control and plug-in.


<OBJECT ID="MediaPlayer1" width=160 height=120
classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"
codebase=
"[линк]"
standby="Loading Microsoft® Windows® Media Player components..."
type="application/x-oleobject">
<PARAM NAME="AutoStart" VALUE="False">
<PARAM NAME="FileName" VALUE="[линк]">
<PARAM NAME="ShowControls" VALUE="False">
<PARAM NAME="ShowStatusBar" VALUE="False">
<EMBED type="application/x-mplayer2"
pluginspage="[линк]"
SRC="[линк]"
name="MediaPlayer1"
width=160
height=120
autostart=0
showcontrols=0>
</EMBED>
</OBJECT>

Next, we'll actually define our buttons. This code will reside in the <BODY> of the HTML document, after the Media Player code.

<TABLE>
<TR>
<TD><A HREF="#" onmouseover="swap(image1,'play.gif')"
onmouseout="swap(image1,'play_low.gif')"
OnClick="document.MediaPlayer1.Play()"
><IMG border=0 name="image1" SRC="play_low.gif"></A></TD>
<TD><A HREF="#" onmouseover="swap(image2,'pause.gif')"
onmouseout="swap(image2,'pause_low.gif')"
OnClick="PauseClick()"
><IMG border=0 name="image2" SRC="pause_low.gif"></A></TD>
<TD><A HREF="#" onmouseover="swap(image3,'stop.gif')"
onmouseout="swap(image3,'stop_low.gif')"
OnClick="StopClick()"
><IMG border=0 name="image3" id="image3" SRC="stop_low.gif"></A></TD>
</TR>
</TABLE>

You'll notice that we're using an onclick event, and a hash HREF (#). Why? Internet Explorer will treat an inline JavaScript reference (HREF="javascript:functionName()) like a normal HREF. When the user clicks on such an HREF, the browser immediately sends a "stop" command. Since the Media Player ActiveX control is tied directly to the browser, the "stop" command will also stop the Media Player. This can cause a plethora of problems if you're trying to make the Media Player play or pause a piece of content. As such, the click event works much better for this.
Netscape Navigator, on the other hand, will not recognize an onclick event without an HREF attached to it (or some other tinkering).

This is where the hash reference comes into play. The hash does not cause Explorer to fire the stop command, so the stack issues revolving around making the Media Player stop and play simultaneously are no longer an issue. And since Netscape Navigator has an HREF attribute attached to the anchor tag, it will process the onclick code.

We've defined a couple of new functions in the image button code. These functions live in the main <SCRIPT> block.

function swap(whichImage, whatURL){
if (whichImage.src.indexOf('_hi') == -1) {
whichImage.src = whatURL;
}
}

function StopClick(){
document.MediaPlayer1.Stop();
if (bControl == true){
document.MediaPlayer1.CurrentPosition=0;
} else {
document.MediaPlayer1.SetCurrentPosition(0);
}
}

function PauseClick(){
if (bControl == true) {
if (document.MediaPlayer1.PlayState == 2){
document.MediaPlayer1.Pause();
}
} else {
if (document.MediaPlayer1.GetPlayState() == 2){
document.MediaPlayer1.Pause();
}
}
}

These three functions are fairly straightforward. The swap function is a fairly generic image swapping routine.

The StopClick function sets the CurrentPosition property of the Media Player to false, since a regular Stop method won't necessarily return the content to the beginning.

The PauseClick function simply checks to see if the Media Player is currently playing content. If so, the Media Player is pauseable, and will be paused.

Отговор
в отговор на petrov #3

Re: JavaScript

primer za event handling na media player s javascript

[линк]

Отговор
в отговор на edno momiche #4

Re: Thanks

blagodaria mnogo i na dvamata
chte se radvam da vi varna uslugata

Отговор
в отговор на petrov #5

Nqma zashto :-)

E, shte imash tazi vyzmojnost da vyrnesh uslugata:-)
Vse pak tuk sme za da si pomagame s kakvoto mojem :-)

Отговор
в отговор на edno momiche #6

Re: JavaScript

Някой може ли да помогне?! ...сложих си MediaPlayer в сайт но незнам от каде да го настроя за да се play-не url-то на едно online радио.

Отговор

Списъкът не се зареди. Опитайте отново.

Зареждане…

Няма клуб с това име