can't get audio clip to play in iOS

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rcmills
Posts: 42
Joined: Wed Nov 21, 2018 8:27 pm

can't get audio clip to play in iOS

Post by rcmills » Sun May 31, 2020 2:15 am

Hello,

I have read a lot of posts on playing audio clips on mobile in the forum, and have made many attempts to use the info, but cannot get it right :(

I have imported two clips as controls, and they work in the IDE. I have checked to see that the clips are present (see commented out script lines), and am sure my sound is not muted on my iPhone.

I am including the openCard script, and the playSounds script. The commented out variable names after some of the lines are to show how I've attempted various forms of the audio clip/files.

I'm hoping someone can tell me what I'm doing wrong...

Many thanks!

Robert Mills

Code: Select all

on openCard
   
   --if exists(audioClip "splash.aiff") then play "splash.aiff" 
   --if exists(audioClip "splash.aiff") then answer "theSplash exists!" with tFile
   --if exists(audioClip "hit.aiff") then answer "aHit exists!" with tFile
   
   
   if the environment is "mobile"
   then
      put "file:/" & specialfolderpath("engine") into sFolder
      
      get "/splash.aiff"
      put (sFolder & it) into tFile
      put tFile into theSplashFile
      put URL tFile into theSplash
      
      get "/hit.aiff"
      put (sFolder & it) into tFile
      put tFile into aHitFile
      put URL tFile into aHit
      
   end if
   
end openCard

on playSounds soundNum
   if not soundOn then exit playSounds
   
   if the environment is "mobile"
   then
      -- first option
      try
         if soundNum = 1 
         then play "splash.aiff" -- theSplash
         else play "hit.aiff" -- aHit
      catch e
         answer e
      end try
      
      -- second option 
      if soundNum = 1 
      then get theSplashFile -- theSplash
      else get aHitFile -- aHit
      
      try
         if the environment is "mobile"
         then mobilePlaySoundOnChannel it,"current", "now"
      catch e
         answer e
      end try
      
   else  -- environment not "mobile"
      
      if soundNum = 1 
      then play audioClip "splash.aiff"
      else play audioClip "hit.aiff"
   end if
   
end playSounds

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

Re: can't get audio clip to play in iOS

Post by Klaus » Sun May 31, 2020 1:45 pm

Hi Robert,

here some important general hints:
1. IMPORTED sounds (and videos, Menu: File: Import as control: Audio file...) are NOT supported on the mobile platform.
We have to use files here.

2. LC can NOT play any sounds/soundfiles placed in a variable!
What you try to do in your preopencard handler.

3. None of the variables (aHit, theSplash, soundOn) used in your handlers as declared as LOCAL or GLOBAL!

4. Everything you add to your runtime via the "Copy files..." tab in the "Standalone Application Settings"
will be found in -> specialfolderpath("resources") on ANY platform in the later runtime!

That specialfolderpath also works in the IDE, here it points to the folder with the current stack in it.
So we can use it in development and in the later runtime without any special scripting!

5. LC only supports uncompressed AIF and WAV files on the desktop with the PLAY command, so make sure your sounds are uncompressed.

Code: Select all

## In case this is meant to be a GLOBAL variable:
global soundON

on openCard
    ## No scripting neccessary regarding your sounds, see playSound handler!   
end openCard

on playSounds soundNum
   if not soundOn then 
      exit playSounds
   end if
   
   ## See above, we find everything here on ANY platform:
   put specialfolderpath("resources") & "/" into tSoundFile

  ## Check soundNum and create the correct appropriate path to that sound file:
   if soundNum = 1 then
      put "splash.aiff" after tSoundFile
   else
      put "hit.aiff" after tSoundFile
   end if
   ## tSoundFile now contains the correct absolute pathname to your sound file!
   
   ## Now this should work on all platforms:
   play tSoundFile
   ## DONE!   
end playSounds
Best

Klaus

rcmills
Posts: 42
Joined: Wed Nov 21, 2018 8:27 pm

Re: can't get audio clip to play in iOS

Post by rcmills » Sun May 31, 2020 6:39 pm

Many thanks, Klaus!

I had already declared the variables as GLOBALs, but didn't include that in my post. Sorry...

I had previously copied the sound files to my standalone settings, but they were copied from a folder on my Mac different from where the LC stack resides. I have removed them, placed them in the same folder as the LC stack, and re-copied them to the standalone settings.

After doing that, the code as you presented it worked perfectly!

I am most grateful!

Robert

Post Reply

Return to “iOS Deployment”