Simple: Play audio file [SOLVED]

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Simple: Play audio file [SOLVED]

Post by William Jamieson » Sat Dec 13, 2014 9:09 pm

Hello. Simple question. How do you play an audio file wiith Livecode?

I tried this code:

Code: Select all

on PlaySound pSound
   if pSound is empty or pSound is "pSound" then put "wine_glasses_x2_clink_004.mp3" into pSound
      
   set the itemDel to "/"
   put item 1 to -2 of (the fileName of this stack) into tSoundFile
   put "/sound files/" & pSound after tSoundFile
   if the environment is "mobile" then
      mobilePlaySoundOnChannel tSoundFile, "current", "now"
   else
      play tSoundFile
   end if
end PlaySound
On desktop a static noise comes out of the speakers for 2 seconds (length of audio file)

On android nothing happens.

The folderpath is correct. The file suffix is correct. The folder "sound files" is included in the standalone application settings under "included files and folders".

Any ideas?? Thanks for your help guys!

-Will
Last edited by William Jamieson on Thu Dec 18, 2014 7:30 pm, edited 1 time in total.

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Simple: Play audio file (help)

Post by sefrojones » Sat Dec 13, 2014 9:54 pm

The play command doesn't recognize mp3, you will have to use a player object to play it as far as I know....


edit:

On android, I believe the path to your included sound files will be specialfolderpath("engine")

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: Simple: Play audio file (help)

Post by William Jamieson » Mon Dec 15, 2014 12:46 am

Ok. Cool. Thank you for letting me know. I will try that.

That is retarded that using the "play" command can play audio clips but cannot play MP3. I hope LC figures out how to change that.

-Will

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: Simple: Play audio file (help)

Post by William Jamieson » Mon Dec 15, 2014 1:32 am

Ok so it turns out the the two styles give the equivalent result

put specialFolderPath("engine") into tFile

and

set the itemdel to "/"
put item 1 to -2 of the filename of this stack into tFile

So I am back to where I started. There is something that I am missing but I still dont know what.

I also made sure that the folder "sound files" was attached and it is there when deployed. I hope I am clear as to what the issue is because I feel like this is really basic but I am just missing it by a hair. Please enlighten me.

-Will

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7267
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simple: Play audio file (help)

Post by jacque » Mon Dec 15, 2014 7:46 pm

You have a couple of choices. You can convert the mp3 to either an aif or wav file and import it as a control. Then the "play" command will work. You won't need to use a file path this way, just the short name of the sound. If this is just a short sound effect, this method works very well.

If the file is larger then you should use a player object. To load the player, you set its filename to the file path of the sound file. To start the playback, use the "start" command. Assuming your filepath is correct and named "tSoundFile" and you have a player named "sndPlayer" then something like this:

Code: Select all

set the filename of player "sndPlayer" to tSoundFile
if the result is not empty then answer the result -- just a check to see if it worked
start player "sndPlayer"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: Simple: Play audio file (help)

Post by William Jamieson » Thu Dec 18, 2014 5:20 am

Ok Great! Thanks Jacque! That definitely worked on Desktop.

I also found the solution for mobile as well. The difference is that Andoid cares whether the letters are capitalized or not. I am not used to checking capitalization but it seems to be a crucial step when working with Android.

What I did to make sure that the file I wanted would play without having to worry about capitalization was

Code: Select all

on PlaySound pSound
   if the environment is not "mobile" then exit PlaySound
   put specialfolderpath("engine") & "/Sound Files" into tFolder
   set the defaultfolder to tFolder
   put "/" & line pSound of the files after tFolder
   mobilePlaySoundOnChannel tFolder, "current", "now"
end PlaySound
where pSound is the line of the sound I wish to choose which I know beforehand**

Hope this helps to future readers

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7267
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simple: Play audio file (help)

Post by jacque » Thu Dec 18, 2014 6:18 am

William Jamieson wrote:I also found the solution for mobile as well. The difference is that Andoid cares whether the letters are capitalized or not. I am not used to checking capitalization but it seems to be a crucial step when working with Android
IOS and Linux too. For consistency I always match the case when creating file paths, just to be sure.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Simple: Play audio file (help)

Post by Klaus » Thu Dec 18, 2014 2:50 pm

The best advice is to ALWAYS create and use files and folders with all lowercase names on ANY platform!
At least that's what I do :D

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: Simple: Play audio file (help)

Post by William Jamieson » Thu Dec 18, 2014 7:29 pm

That is a good idea Klaus. Good idea.

And Jacque, what do you mean that will work on iOS and linux too?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7267
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simple: Play audio file [SOLVED]

Post by jacque » Fri Dec 19, 2014 12:10 am

I just meant that iOS and Linux are case-sensistive too when dealing with file names, it isn't just Android.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: Simple: Play audio file [SOLVED]

Post by William Jamieson » Sat Dec 20, 2014 5:22 am

Ahh got it. Thanks Jacque. I will take note of that. Especially as the app I am developing currently will be for bot Android and iOS. Will start working on the iOS version now.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”