Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do i get music to change to another song at certain point in story?>


Hey all, working on my story, I figured out how to use music as a script so it will play throughout the whole story..
I would like the music to change later in the story.. Any way to have the music make the music change?

Comments

  • You first need to tell us which story format (Harlowe; Snowman; SugarCube) you are using and which method (audio tag; one of the pre-made scripts; your own javascript code) you are using to play the background sound (music). *smile*

  • Hi,

    Yes. I'm using Sugarcube (can I switch story styles in mid-story without fear of erasing?)

    The Java script for music is:

    var audio = document.createElement('audio');
    audio.src = 'music/vivaldi.mp3';
    audio.loop = true;
    audio.play();

    Thanks!
  • Because you are using SugarCube I would suggest using this sound script which has been modified by the creator of the SugarCube story format.

    You then could add <<loopsound "music/vivaldi.mp3">> to a passage to play that piece of music, anytime you want to change to a different piece of music just add something like the following to your passage:

    <<stopsound "music/vivaldi.mp3">>
    <<loopsound "music/liszt.mp3">>
    note: A story format defines more than just how the page looks by default, it also defines the format of the macros, what macros are available by default, what features are available to the Author and the structure of the generated HTML file.

  • Thanks, where do I put the actual music MP3 file?
    I currently have in a folder called "Music" in my Twine 2.0.3 folder

    How do I get the code you mentioned to access the MP3?

    Thanks!
  • Where the script looks for the sound files is the same as your code example, and depends on two things:
    a. Is the story HTML file hosted on a web-server or on the local hard-disc.
    b. The format of the parameter being passed to the macro.

    eg.
    1. The story HTML file is stored on local hard-disc and parameter is "music/liszt.mp3"
    The sound file needs to be in a sub-folder named "music" directly below the folder containing the story HTML file.

    2. The story HTML file is stored on local hard-disc and parameter is "http://somehost.com/music/liszt.mp3";
    The sound file is will be retrieved from a web-server.

    3. The story HTML file is hosted on a web-server and parameter is "music/liszt.mp3"
    The sound file is will be retrieved from the web-server, it will need to be in a sub-folder named "music" directly below the folder containing the story HTML file.

    4. The story HTML file is hosted on a web-server and parameter is "http://somehost.com/music/liszt.mp3";
    The sound file is will be retrieved from the web-server.
  • My story opens on the Firefox Browser, but the Twine 2.0 folder is on desktop.. The music folder is in that...

    I add the script to my 1st opening page in my story & I still get this error: Error: macro <<loopsound>> does not exist
  • If you click on the name of your story in the bottom left, and then "Edit Story JavaScript", you should be able to put your macro code in there. After you've done that, the <<loopsound>> macro from greyelf's link to the Sugarcube site should work.

  • Yes, but if I add the music to the JavaScript, it will play throughout.. How would I get a different song to play at a different part? that's my dilemma..
  • If you add the script that is contained in the link that I posted earlier to the Story Javascript area of your story it will add a number of new macros to your story, one of them being <<loopsound>>

    You don't need to add references to your music files to the javascript, instead you can use the <<loopsound>> macro in your story's passages to play sound/music files.

    eg. <<loopsound "music/vivaldi.mp3">> will play the music file as long as it can find it and I explained the rules used to find the file in my previous post, these are the same rules that your original javascript code would of followed as well.

  • EUREKA!!! Success!! Thanks for the guidance!

  • With music I recommend using <fadeoutsound> and <<fadeinsound>> over <<stopsound>> and <<loopsound>>, as sudden stop/starts are jarring.

    I also adjusted the fade length from 2 seconds to 6 seconds to make this smoother. You can search out some of my posts in this forum for how to do that if you want to.
  • Ok for some reason I'm having a funny little problem, I set the music to start playing at the start of the story like so: <<loopsound "music/vivaldi987.mp3">>

    It plays on, but when I get to the part of the story I want to transition the music I put this in:

    <<stopsound "music/vivaldi11.mp3">>
    <<loopsound "music/vivaldi987.mp3">>

    My original music from the start is still playing over the new music, both are playing... how do I remedy this?

    Thanks!
  • I think there is an error in your example, you state the following:
    1. playing vivaldi987
    2. stopping vivaldi11  <--- you are stopping a different tune than the one in point 1?
    3. playing vivaldi987

  • Sorry for the confusion you are right, I meant to say that the story starts off with vivaldi987

    The I want vivaldi987 to STOP playing at a certain point & for vivaldi11 to START playing...
    Yes, vivaldi11 DOES indeed start to play at the point I set, my problem is that the original music from the start of the story: vivaldi987 continues to play & does not STOP as the command directs it too.. Instead, BOTH music tracks play..

    What do you think I might be doing wrong?


  • If you remove the <<loopsound "music/vivaldi11.mp3">> from that passage does the <<stopsound  "music/vivaldi987.mp3">> in that passage actually stop the music?

    Because the code in the script looks ok.
  • Azurean88 wrote:


    Sorry for the confusion you are right, I meant to say that the story starts off with vivaldi987

    The I want vivaldi987 to STOP playing at a certain point & for vivaldi11 to START playing...
    Yes, vivaldi11 DOES indeed start to play at the point I set, my problem is that the original music from the start of the story: vivaldi987 continues to play & does not STOP as the command directs it too.. Instead, BOTH music tracks play..

    What do you think I might be doing wrong?


    Check for spelling mistakes in the code you're actually using in your story. Try to avoid complex names like vivaldi987, since it's very easy to spell that wrong.

  • Thanks folks... Got it to work!! it was all a matter of bad spacing in the code.. what a difference an extra tap of the spacebar makes lol.. Simple fix, I like simple fixes :)
Sign In or Register to comment.