Skip to main content
Participant
April 16, 2019
Question

Accessing sound files on a page and assigning them to play

  • April 16, 2019
  • 2 replies
  • 179 views

I am trying to programmatically assign a large number of sound files (several hundred) to objects to play in an EPUB using InDesign 13.1.

I cannot understand how to access and assign the sound file after it has been placed on a page. I need to be able to assign it to my button with a soundBehaviors action, but I cannot find how to tell my button what sound file to use on the page.

=================================================================================

    var myDocument = app.documents.add();

    var myPage = myDocument.pages.item(0);

// build a loop and import 12 sound files on the page. They all appear in the Links panel. They will not be seen, just placed on the page to be accessed by different //buttons

  for(var i = 1; i < 13; i++){

        var fileString = "DISK:Users:PATH TO FILES:"Sound File " + i + ".mp3";

        myPage.place(File(fileString));     

   }

  // now create buttons and the actions to play the sound on click and link up to the sound files just placed on the page

 

  for(var i = 1; i < 13; i++){

           //make a button       

          var MyButton = myPage.buttons.add();

   

            MyButton.properties = {

                    geometricBounds: [8,8,12,12],

                    strokeColor: "Black",

                    strokeWeight: 1,

                    name: "Example " + i

        }

      

       //here is where the sound gets linked to the button to play on a mouse down.

         var mySoundStartBehavior = MyButton.soundBehaviors.add(

                                           {

                                             behaviorEvent: BehaviorEvents.MOUSE_DOWN,

                                             operation: PlayOperations.PLAY,

                                             abel: "Example!!",

                                             // THIS is the issue - I have tried every way I can think of to access the sounds on the page and assign it and no luck.

                                             soundItem: Page.sounds? sounds.items? a string to the sound file name? A link to the file on disk?

                                                 }

                                            );

=======================================================

How do you get access to the sounds that have been successfully placed at the beginning of the script?

The object model docs makes it seem like page.sounds[0] should work, but it doesn't.

This topic has been closed for replies.

2 replies

Community Expert
April 17, 2019

Oh. And since you are using method place() with object Page you could check what's returned by place().

Should be an array of one item in your case, that itself contains a sound item.

Regards,
Uwe

Community Expert
April 17, 2019

Hi musicpages ,

a sound file placed is a mediaItem.

Unfortunately there is no allMediaItems array in the scripting DOM.

Media items are contained by spline items as there are: graphicLines, ovals, polygons, rectangles .

To get an array of sounds you coud loop the allPageItems array of a page, a spread, a document or e.g. a group or a story.

Whatever. And then filter out the sounds.

var myPage = app.documents[0].pages[0];

var myPageSounds = [];

var allPageItems = myPage.allPageItems;

for( var n=0; n<allPageItems.length; n++ )

{

    var currentItem = allPageItems.getElements()[0];

    if( currentItem.constructor.name == "Sound" )

    {

        myPageSounds[myPageSounds.length++] = currentItem ;

    }

}

Or you could look into the Links collection.

The parent of every link can be tested if it is a sound.

var myLinksArray = app.documents[0].links.everyItem().getElements();

var myDocumentSounds = [];

for( var n=0; n<myLinksArray.length; n++ )

{

    var currentItem = myLinksArray;

    if( currentItem.parent.constructor.name == "Sound" )

    {

        myDocumentSounds[myDocumentSounds.length++] = currentItem.parent ;

    }

}

IMPORTANT: If you have MSOs ( multiStateObjects ) with sounds in different states, note that allPageItems from document, spread or page will not contain the elements of states that are not active!

Regards,
Uwe