Solved

Playing of midi upon request

Posted on 1998-12-01
35
248 Views
Last Modified: 2008-02-01
Is there a script to start playing and stop midi files?

when the user wants to listen to a particular music, click on it and click play.
0
Comment
Question by:java_kevin
  • 17
  • 16
  • 2
35 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 150 total points
Comment Utility
Try this:
<HTML>
<HEAD>
<SCRIPT language="JavaScript"> <!-- // cloak
loaded=false;
nowplaying = '';
function playsound(theMidi)  {
   if (!document.embeds) return false; // browser does not support it
   if (nowplaying !='') stopsound(nowplaying);
   if (loaded) {
      if (navigator.appName=='Netscape') document.embeds[theMidi].play(false);
      else document.embeds[theMidi].Run();
      nowplayng = theMidi;
   }
}
function stopsound(theMidi)  {
   if (!document.embeds) return false; // browser does not support it
   if (loaded) document.embeds[theMidi].stop(); // not 100% sure of syntax here
}

function myVoid() { ; }
// uncloak --></SCRIPT>
<BODY onLoad="loaded=true;">
<FORM>
<SELECT onChange="playsound(this.options[this.selectedIndex].value)">
<OPTION VALUE="midi1">Jingle bells
<OPTION VALUE="midi2">Hark hark...
</SELECT>
</FORM>
<EMBED SRC="midi1.mid" NAME="midi1" VOLUME="60" AUTOSTART="false" HIDDEN="true" MASTERSOUND>
<EMBED SRC="midi2.mid" NAME="midi2" VOLUME="60" AUTOSTART="false" HIDDEN="true" MASTERSOUND>

</BODY>
</HTML>

This script is untested so le me know if it works for you...
Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
The select should be
<SELECT
onChange="if (this.options[this.selectedIndex].value !='') playsound(this.options[this.selectedIndex].value)">
    <OPTION VALUE="">Select a tune
    <OPTION VALUE="midi1">Jingle bells
    <OPTION VALUE="midi2">Hark hark...
    </SELECT>


Alternatively (and more compatible) have a frameset with a hidden frame and load a midifile (or a file containg an embedded midi/bgsound) into it and a blank file to stop it playing:

<A HREF="midi1.mid" TARGET="hidden">Play midi1</A>
<A HREF="midi2.mid" TARGET="hidden">Play midi2</A>
<A HREF="blank.html" TARGET="hidden">Stop playing</A>

Michel
0
 
LVL 10

Expert Comment

by:kollegov
Comment Utility
The methods depends on what plugin is associated with MIDI files
Nearly all support stop() but starting is quite different.
The script below will work for DEFAULT LiveAudio in NN
and default midi player in MIE4. It also SHOULD work if
Crescendo installed at both browsers. Sorry, I don't test it with Crescendo It requires reinstalling of plugins..
But according to Crescendo docs it should :)

<HTML>
<head>
<script>

var cresc=false;
for(i=0;i<navigator.mimeTypes.length;i++)
    {mime=navigator.mimeTypes[i];
     // check LiveAudio enabled for MIDI
     if(mime.suffixes.indexOf("mid")!=-1)
       {midiPlugin=navigator.plugins["LiveAudio"];
        if(midiPlugin.name.indexOf("Crescendo")!=-1)
         cresc=true;
       }
    }    


 function play(title)
  {p=document.player
   if(p)
     {if(!cresc)
      {if(navigator.appName.indexOf("Netscape")!=-1)
        {p.play(0,title);}
       if(navigator.appName.indexOf("Explorer")!=-1)
         {p.run();}
      }
      else
       {p.play(title);}
     }
  }
 function stop()
  {p=document.player
   if(p) {p.stop();}
  }
</script>
</head>
<body>
<EMBED
 src="passport.mid"
 NAME="player"

 AUTOSTART=false
 LOOP=FALSE
 MASTERSOUND
>
</body>
<A HREF="#" onClick="play('passport.mid'); return false">Play</a>
<A HREF="#" onClick="stop(); return false">Stop</a>
</body>
</HTML>

This script was tested with NN3,NN4 with LiveAudio
and MIE4 with default midi player.

But... There are a lot of other plugins which can cause
problems..

Last thing.. for proper plugin envoking you need to
have valid .mid file in EMBED specified.
Othervise plugin wouldn't be properly inited.


While I was writing my answer, Michele already locked it :)

0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Sorry, Kollegov! I hate it when that happens. I will share any points

Michel  <--- no e at the end ;-)  


0
 
LVL 10

Expert Comment

by:kollegov
Comment Utility
Sorry, Michel for extra 'e' :)
Actually points doesn't matter for me anything, as well as rating
Just looking for interesting puzzles as stimulus to learn something new..
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Ok. Fair enough. I also use EE as a source of ideas to implement. I however do like the points and the tshirts ;-)

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
wat i mean is the user has to click something b4 playing the music.

like button... not that jus click at a name and wil play...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Change the select in my script to

<INPUT TYPE="button" onClick="playsound(midi1)" VALUE="Jingle Bells">
<INPUT TYPE="button" onClick="playsound(midi2)" VALUE="Bla bla">

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
i don't mean this.

i mean a select box (pull down list or watever). i select a tune, click a button to play it. and click a button to stop it. however clicking the play button twice (ie did not click the stop first) will stop the former midi and play the selected midi.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Ok, remove the onchange from the select and put the code in a button:

<SELECT NAME="MFILES">
<OPTION VALUE="">Select a tune
<OPTION VALUE="midi1">Jingle bells
<OPTION VALUE="midi2">Hark hark...
</SELECT>
<INPUT TYPE="button"
onClick="var mf = this.form.MFILES;
if (mf.options[mf.selectedIndex].value !='') playsound(mf.options[mf.selectedIndex].value)">

Now you select and click to play.

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
ok... trying the script... but can ans mi something else?

must i embed the midis even when i'm using the button?

and wat's the diff between document.location and location.href? (this is optional)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
The embedding of the midis will allow the start and stop to be run.
An alternative would be to have a hidden frame and load a piece of html per midifile and an empty html for the stop. The html would each have one embedded midifile.

JavaScript The Definitive Guide says:

In JS1.0, document.location is a read-only  sring property that contains the complete URL of the current document. Normally the same string as in window.location.href except when a redirection has taken place then window.location.href is the requested URL and document.location is the actual URL

In JS 1.1 document.location is no longer a string but a reference to the same Location object as window.location. It is deprecated (you are not supposed to use it anymore) and document.URL takes it's place.

window.location.href is the one I read when I want to know URL (although I really should use document.location in JS1.0 and document.URL in js1.1+)
and window.location is the one I set to a new page especially if I tack anything on like page1.html?some_argument
since that will set the location.search and not just the href.

Hope this clarifies your question

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
i tried it.

it says the function of playsound does not always return a value. but everything is still there.

however, when I click play, it says playsound is not defined...
http://home.pacific.net.sg/~kevinlsm/card.html where the midis are not included.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Sorry:
Just remove the false from the return
function playsound(theMidi)  {
       if (!document.embeds) return false; // browser does not support it

should be

       if (!document.embeds) return; // browser does not support it

Please try it again

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
but i think a total of 31 midis to be loaded is a bit too much, right?
or do u think so?

if u think so too, can i open a new small window  to play tyhr midi?
var popup=window.open(url, name,"...")
popup.embeds(same as the code as in doc.embed).

is this possible?
0
 

Author Comment

by:java_kevin
Comment Utility
i changed. same url. but it says the embed has no properties...

as a comparison, koll's is much diffcult to understand... unlike urs... :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
If you use the popup, you need to change the html in the popup to
autostart true:
<HTML>
Now playing:
Jingle bells
<EMBED SRC="midi1.mid" NAME="midi1" VOLUME="60" AUTOSTART="true" HIDDEN="true" MASTERSOUND>
</HTML>

and just close the popup or load a
<HTML>
Music off
</HTML>
into the popup.

All the embed.play/stop scripting can be removed then:

<INPUT TYPE="button" onClick="window.open('midi1.html','midi')" VALUE="Jingle Bells">
<INPUT TYPE="button" onClick="window.open('midi2.html','midi')" VALUE="bla bla">
<INPUT TYPE="button" onClick="window.open('empty.html','midi')" VALUE="music off">

or

<INPUT TYPE="button"
onClick="mwin=window.open('','midi');mwin.close()" VALUE="music off and close">


Michel



0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:java_kevin
Comment Utility
but do i need the name in the embed

and is it possible to get the name of the file that is supposed to be played and use this to open the html?

then the user only needs to close the popped win and all resources are returned right?
0
 

Author Comment

by:java_kevin
Comment Utility
how do i state the size of the window to be opened?

i've got the others done... but not sure if it works...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
<SCRIPT>
function openplay(what2play) {
   FileName = what2play+'.html';
   window.open(FileName,'midi','width=400,height=200');
}
</SCRIPT>

<INPUT TYPE="button" onClick="openplay('midi1')" VALUE="Jingle Bells">
<INPUT TYPE="button" onClick="openplay('midi2')" VALUE="bla bla">
<INPUT TYPE="button" onClick="openplay('close')" VALUE="music off">

have a file called close.html containing
<SCRIPT>
self.close();
</SCRIPT>
<B>Music Off</B>

Michel

0
 

Author Comment

by:java_kevin
Comment Utility
sorry to kep ur pts in suspense...

writing htmls is too troublesome. i will just have all on the same pg.

but I still have a prob wif the embed. it says doc.embeds has no properties.
0
 

Author Comment

by:java_kevin
Comment Utility
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Add a name to the embed and play that instead:

<option value="anything">So You Don't Want Anything


<embed NAME="anything" hidden=true autostart=false mastersound volume="60" src="01.mid">

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
so... the name IS needed... (not tried yet :P )

the name is same as the value of the midi?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Nope, just any name in the <EMBED NAME="whatever"

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
i think it still doesn't work... http://www.angelfire.com/ny/kevinlee/card.html
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
I am sorry, I could not access this url when I tried. I will try again later

Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
You need to give each embed a different name and use that name in the select:

<OPTION VALUE="music1">bla bla
<OPTION VALUE="music2">bla bla
<OPTION VALUE="music3">bla bla


<EMBED NAME="music1" ........>
<EMBED NAME="music2" ........>
<EMBED NAME="music3" ........>

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
I think I finally managed to get them to load but the midis load bit too slow, causing my page to be ready in a long period of time. Thus, I did the multiple html instead.

I tried a button to close the popup window bu can't work. Think will depend on user to close the window. thankx.

think this is my toughest and longest history question ever asked...
0
 

Author Comment

by:java_kevin
Comment Utility
u are always welcome to send postcards from my site :) the angelire place. :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
I tried to see what was wrong with the close, but you have not published the page yet, I see.

if you use
<SCRIPT>
WinId=nul;
function openWin(file) {
   WinId = window.open(file,'thefile')
}
function closeWin() {
   if(WinId) WinId.close();
}
<SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="open" onCLick="openWin('myfile.html')">
<INPUT TYPE="button" VALUE="close" onCLick="closeWin">
</FORM>

or if you want a close button in the file you open, just have
<FORM>
<INPUT TYPE="button" VALUE="Close Me" onCLick="self.close()">
</FORM>

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
wow...
i didn't know about the (winld) way... :)

u're right. The file at angelfire is the old version. if u using IE, u'll notice that it trys to finish loading the midis b4 the full pg (3.0).

anyway, will this pg be gone after it's been ans'ed for some time already? i think i want to keep the scripts but if this qn/ans still exists here, then i can just delete this script form my card.html

and i think the script may still have some problems. it keeps saying about the file:/a... can u check it out?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
This page stays a Previously Answered Question for as long as EE will keep it.
So far they haven't deleted answered questions, but I cannot promise they will not do that in the future.

I cannot check it out if you do not upload or show me the new page...

Michel
0
 

Author Comment

by:java_kevin
Comment Utility
i mean, it's already uploaded... now the error is mf not defined...

and b4 this, i tried ur winld (my popup) and set to nul. but ti says repeatedly (although i saved it umpteen times) that nul is not defined. then i tried null. same thing.

so i made it to ' ' instead... (no spaced between)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
that is because mf is a local variable in the onClick.
Change the onClick :
      onClick="var mf = this.form.sound;
      if (mf.options[mf.selectedIndex].value !='')
        playmidi();
      else alert('Please select a tune')">
to

      onClick="var mf = this.form.sound;
      if (mf.options[mf.selectedIndex].value !='')
        playmidi(mf);
      else alert('Please select a tune')">

and function playmidi()
to function playmidi(mf)

null is a valid thing to set a variable to. In the newest browser that might actually delete the variable, so '' is ok except you need to ask when you use it:
popup='';
if (popup && popup !='') popup.close();

Michel

Michel
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now