Attaching audio file to a myTextObject

How can I click on separate words of a sentence and hear back audio connected to each word.

For example, the sentence:

“Yo!  What’s up!”

Click on “Yo!” = play audio file of “Yo!”

Click on “What’s” = play audio file of “What’s”

I checked the audio area in Getting Started but I don’t see how to “attach” and audio file to a textObject.

Thanks for your help!

Hi, rshanlon, this is easily achieved by using event listeners. You would have to create 3 separate strings: “Yo”, “What’s”, and “up!” Then, for each string, you would do this:

For “yo”:(assuming the name of the string is yo)

local function yosound()

–play whatever sound here

end

yo:addEventListener(“tap”, yosound)

What this does is it adds an event listener to yo. It says to listen for a tap. Once it is tapped, it calls the function yosound, and when yosound is called, it plays whatever sound you want to be played.

Hopefully this makes sense and you are able to achieve what you wish to achieve.

Happy Coding!  :wink:

Thank you.  It does make sense- the logic of it.  Although i’ve never done this before.  So, I’ll get busy and see if I can make this work on the Simulator.

Thanks for also clarifying that this is possible with lua.  I was hoping it was but just wasn’t sure.

No Problem! Hopefully it works for you. :slight_smile:

This is really hard for me.  95% of what I’m reading doesn’t make sense to me but I’m still trying.  I changed the sentence to, “Hello.  My name is GG520.” and I recorded individual mp3 files for each word.

Using what you said about Event Listener and some other parts I read at Getting Started I came up with this non-working code.  Can you give me a sign where I’m going wrong?

The simulator keeps saying: Attempt to index global ‘helloSound’ (a nil value)

local myTextObject = display.newText( “Hello! My name is GG5-20.”, 160, 240, “Times”, 26 )

 

myTextObject:setFillColor( 1, 0, 1 )

 

local soundTable = {

 

    helloSound = audio.loadSound( “Hello.mp3” ),

    mySound = audio.loadSound( “my.mp3” ),

    nameSound = audio.loadSound( “name.mp3” ),

    isSound = audio.loadSound( “is.mp3” ),

    GG520Sound = audio.loadSound( “GG520_01.mp3” ),

}

 

 

audio.play( soundTable[“helloSound”] )

local function hellosound()

–play whatever sound here

end

helloSound:addEventListener(“tap”, helloSound)

But if I remove the last four lines, and then Save, I can hear the “Hello” mp3 auto-play.

Here, let me give you the code, and explain it step by step. OK, here’s the code that you want to put in( Copy and paste this into your text editor ) You can just use it or read on to see how it works:

          

          local group = display.newGroup();

local hello = display.newText( “Hello!”, 160, 240, “Times”, 26 )

local my = display.newText( “My”, 170, 240, “Times”, 26 )

local name = display.newText( “name”, 180, 240, “Times”, 26 )

local is = display.newText( “is”, 190, 240, “Times”, 26 )

local gg520 = display.newText( “GG520”, 200, 240, “Times”, 26 )

group:insert(hello)

group:insert(my)

group:insert(name)

group:insert(is)

group:insert(gg520)

group:setFillColor( 1, 0, 1 )

helloSound = audio.loadSound( “Hello.mp3” )

mySound = audio.loadSound( “my.mp3” )

nameSound = audio.loadSound( “name.mp3” )

isSound = audio.loadSound( “is.mp3” )

GG520Sound = audio.loadSound( “GG520_01.mp3”)

local function playhellosound()

audio.play(helloSound);

end

hello:addEventListener(“tap”, playhellosound)

Alright, so what this does:

Line 1 makes a group called group. Groups can store a bunch of objects in the same box, or group. Lines 2-6 create the sentence you wanted to make, each word a separate string. lines 7-11 store all of the strings in the group “group”. Line 12 makes the whole group the color you wanted it to be. Lines 13-17 load all of the sounds you wanted to load. Lastly, you want to add the event listener to the string itself, and not the sound So you would do what I did in the last line. Lines 18-20 play the sound helloSound every time you tap “hello”. You would have to add an event listener to every string, than make a function for it and play the appropriate sound.Did this make any sense AT ALL?? If it didn’t, just tell me what you didn’t get, and I’ll try to make it more understandable. If you don’t want to bother, just copy and paste the code, and you’ll be good to go. :slight_smile:

Man, your a generous person for taking the time.  Thanks so much.  Yes, it kind of makes sense considering I don’t speak code talk yet…but I do get the concepts.  As I get more familiar with coding, this language will make sense as I read it.  It’s like I’m reading English but it’s not quite making sense…yet.

Just one thing, I got an error message regarding the setFillColor saying "Attempt to call method ‘setFillColor’ (a nil value).  So I just took that part out and what showed up on the screen (both iPhone and iPad) is all the words bunched together.  So, I spaced the x values so that each word looked like it was part of the same sentence.  (I wonder if there’s a kind of spreadsheet style where I can “fill-in” the boxes with the necessary words so it won’t take so much time spacing out individual words.)

Next, I took the EventListener you wrote and copied and pasted it for the word “my” and everything worked great.  When I tapped Hello the hello audio played…when I tapped “my” the my audio played!

Also, I noticed with the EventListener part that says  hello:addEventListener(“tap”, playhellosound)  it’s not necessary to write it playhelloSound with a capital “S” since that’s how it’s written in Table part.  It worked just fine with the small “s”.  I didn’t know coding could change caps and small letters so freely.

Thanks again!  What a community!  I’m not used to people helping so freely.  I appreciate your help!

 local group = display.newGroup();

local hello = display.newText( “Hello!”, 40, 240, “Times”, 26 )

local my = display.newText( “My”, 100, 240, “Times”, 26 )

local name = display.newText( “name”, 150, 240, “Times”, 26 )

local is = display.newText( “is”, 190, 240, “Times”, 26 )

local gg520 = display.newText( “GG520”, 245, 240, “Times”, 26 )

group:insert(hello)

group:insert(my)

group:insert(name)

group:insert(is)

group:insert(gg520)

helloSound = audio.loadSound( “Hello.mp3” )

mySound = audio.loadSound( “my.mp3” )

nameSound = audio.loadSound( “name.mp3” )

isSound = audio.loadSound( “is.mp3” )

GG520Sound = audio.loadSound( “GG520_01.mp3”)

local function playhellosound()

audio.play(helloSound);

end

hello:addEventListener(“tap”, playhellosound)

local function playmysound()

audio.play(mySound);

end

my:addEventListener(“tap”, playmysound)

No Problem So glad it worked! Yeah, programming does kinda sound like English, but Yoda style! Hope you learn a lot!

Hi, rshanlon, this is easily achieved by using event listeners. You would have to create 3 separate strings: “Yo”, “What’s”, and “up!” Then, for each string, you would do this:

For “yo”:(assuming the name of the string is yo)

local function yosound()

–play whatever sound here

end

yo:addEventListener(“tap”, yosound)

What this does is it adds an event listener to yo. It says to listen for a tap. Once it is tapped, it calls the function yosound, and when yosound is called, it plays whatever sound you want to be played.

Hopefully this makes sense and you are able to achieve what you wish to achieve.

Happy Coding!  :wink:

Thank you.  It does make sense- the logic of it.  Although i’ve never done this before.  So, I’ll get busy and see if I can make this work on the Simulator.

Thanks for also clarifying that this is possible with lua.  I was hoping it was but just wasn’t sure.

No Problem! Hopefully it works for you. :slight_smile:

This is really hard for me.  95% of what I’m reading doesn’t make sense to me but I’m still trying.  I changed the sentence to, “Hello.  My name is GG520.” and I recorded individual mp3 files for each word.

Using what you said about Event Listener and some other parts I read at Getting Started I came up with this non-working code.  Can you give me a sign where I’m going wrong?

The simulator keeps saying: Attempt to index global ‘helloSound’ (a nil value)

local myTextObject = display.newText( “Hello! My name is GG5-20.”, 160, 240, “Times”, 26 )

 

myTextObject:setFillColor( 1, 0, 1 )

 

local soundTable = {

 

    helloSound = audio.loadSound( “Hello.mp3” ),

    mySound = audio.loadSound( “my.mp3” ),

    nameSound = audio.loadSound( “name.mp3” ),

    isSound = audio.loadSound( “is.mp3” ),

    GG520Sound = audio.loadSound( “GG520_01.mp3” ),

}

 

 

audio.play( soundTable[“helloSound”] )

local function hellosound()

–play whatever sound here

end

helloSound:addEventListener(“tap”, helloSound)

But if I remove the last four lines, and then Save, I can hear the “Hello” mp3 auto-play.

Here, let me give you the code, and explain it step by step. OK, here’s the code that you want to put in( Copy and paste this into your text editor ) You can just use it or read on to see how it works:

          

          local group = display.newGroup();

local hello = display.newText( “Hello!”, 160, 240, “Times”, 26 )

local my = display.newText( “My”, 170, 240, “Times”, 26 )

local name = display.newText( “name”, 180, 240, “Times”, 26 )

local is = display.newText( “is”, 190, 240, “Times”, 26 )

local gg520 = display.newText( “GG520”, 200, 240, “Times”, 26 )

group:insert(hello)

group:insert(my)

group:insert(name)

group:insert(is)

group:insert(gg520)

group:setFillColor( 1, 0, 1 )

helloSound = audio.loadSound( “Hello.mp3” )

mySound = audio.loadSound( “my.mp3” )

nameSound = audio.loadSound( “name.mp3” )

isSound = audio.loadSound( “is.mp3” )

GG520Sound = audio.loadSound( “GG520_01.mp3”)

local function playhellosound()

audio.play(helloSound);

end

hello:addEventListener(“tap”, playhellosound)

Alright, so what this does:

Line 1 makes a group called group. Groups can store a bunch of objects in the same box, or group. Lines 2-6 create the sentence you wanted to make, each word a separate string. lines 7-11 store all of the strings in the group “group”. Line 12 makes the whole group the color you wanted it to be. Lines 13-17 load all of the sounds you wanted to load. Lastly, you want to add the event listener to the string itself, and not the sound So you would do what I did in the last line. Lines 18-20 play the sound helloSound every time you tap “hello”. You would have to add an event listener to every string, than make a function for it and play the appropriate sound.Did this make any sense AT ALL?? If it didn’t, just tell me what you didn’t get, and I’ll try to make it more understandable. If you don’t want to bother, just copy and paste the code, and you’ll be good to go. :slight_smile:

Man, your a generous person for taking the time.  Thanks so much.  Yes, it kind of makes sense considering I don’t speak code talk yet…but I do get the concepts.  As I get more familiar with coding, this language will make sense as I read it.  It’s like I’m reading English but it’s not quite making sense…yet.

Just one thing, I got an error message regarding the setFillColor saying "Attempt to call method ‘setFillColor’ (a nil value).  So I just took that part out and what showed up on the screen (both iPhone and iPad) is all the words bunched together.  So, I spaced the x values so that each word looked like it was part of the same sentence.  (I wonder if there’s a kind of spreadsheet style where I can “fill-in” the boxes with the necessary words so it won’t take so much time spacing out individual words.)

Next, I took the EventListener you wrote and copied and pasted it for the word “my” and everything worked great.  When I tapped Hello the hello audio played…when I tapped “my” the my audio played!

Also, I noticed with the EventListener part that says  hello:addEventListener(“tap”, playhellosound)  it’s not necessary to write it playhelloSound with a capital “S” since that’s how it’s written in Table part.  It worked just fine with the small “s”.  I didn’t know coding could change caps and small letters so freely.

Thanks again!  What a community!  I’m not used to people helping so freely.  I appreciate your help!

 local group = display.newGroup();

local hello = display.newText( “Hello!”, 40, 240, “Times”, 26 )

local my = display.newText( “My”, 100, 240, “Times”, 26 )

local name = display.newText( “name”, 150, 240, “Times”, 26 )

local is = display.newText( “is”, 190, 240, “Times”, 26 )

local gg520 = display.newText( “GG520”, 245, 240, “Times”, 26 )

group:insert(hello)

group:insert(my)

group:insert(name)

group:insert(is)

group:insert(gg520)

helloSound = audio.loadSound( “Hello.mp3” )

mySound = audio.loadSound( “my.mp3” )

nameSound = audio.loadSound( “name.mp3” )

isSound = audio.loadSound( “is.mp3” )

GG520Sound = audio.loadSound( “GG520_01.mp3”)

local function playhellosound()

audio.play(helloSound);

end

hello:addEventListener(“tap”, playhellosound)

local function playmysound()

audio.play(mySound);

end

my:addEventListener(“tap”, playmysound)

No Problem So glad it worked! Yeah, programming does kinda sound like English, but Yoda style! Hope you learn a lot!