Record and Play sound at same time IOS

Stephen MacLean smaclean at madmansoft.com
Sat Apr 26 22:12:07 EDT 2014


Hi Nakia,

Ok, played around some, and found that while the file is created when you start to record, it doesn't write any data to it until you stop recording. That's not going to work, so I came up with a little something that does. It rotates the file that is being recorded and the file that is playing. Works pretty good, lol, but there sometimes a slight pop when switching files. Your milage may vary!

See what you think. It was an interesting exercise!

Put 2 buttons on a card. Name one Record and the other Stop

Out this into the script of the Record button:

global gFileNamePath1,gFileNamePath2,gFileNamePath3
global gRecFileName1,gRecFileName2,gRecFileName3
global gRecPointer1,gRecPointer2,gRecPointer3
global gCurrentFileNum
global gSendJammerID

on mouseUp
   #Setup mergMicrophone
   if the environment is "mobile" then
      mergMicrophoneSetNumberOfChannels "Stereo"
      mergMicrophoneSetSampleRate 44100
      mergMicrophoneSetAudioFormat "MP4-AAC"
      mergMicrophoneSetTargetBitRate 65536
      mergMicrophoneSetAudioQuality "High"
   end if
   
   # Setup up AV Session
   if the environment is "mobile" then
      mergAVAudioSessionSetCategory "play and record",false,false,true,true
   end if
   
   # Setup file names & current file number
   put UUID ("random") into tUUID1
   put UUID ("random") into tUUID2
   put UUID ("random") into tUUID3
   put 1 into gCurrentFileNum
   if the environment is "mobile" then 
      put specialFolderPath("cache") & slash & tUUID1 & ".m4a" into gFileNamePath1
      put specialFolderPath("cache") & slash & tUUID2 & ".m4a" into gFileNamePath2
      put specialFolderPath("cache") & slash & tUUID3 & ".m4a" into gFileNamePath3
      put tUUID1 & ".m4a" into gRecFileName1
      put tUUID2 & ".m4a" into gRecFileName2
      put tUUID3 & ".m4a" into gRecFileName3
      # Start Recording with file 1
      mergMicrophoneStartRecording gFileNamePath1
      send "playBackJammerStream" to me in 500 millisec
      put the result into gSendJammerID # To cancel sends when stop is pressed
   end if
end mouseUp

on playBackJammerStream
   #stop recording current file, start recording on next, start playing current file
   switch gCurrentFileNum
      case 1
         mergMicrophoneStopRecording # Stop recording to current file so data is written to disk
         mergMicrophoneStartRecording gFileNamePath2 # Start recording to new file so you don't lose anything in the stream
         put "file:" & gFileNamePath1 into tGetFile  # Set up file we just stopped recording for playback
         put mergAVPlayerCreateFromURL (tGetFile) into gRecPointer1 # Create the AV player for that file
         mergAVPlayerDo gRecPointer1,"play" # Start playing file
         put 2 into gCurrentFileNum # Advance counter for next callback
         send "playBackJammerStream" to me in 500 millisec # Call me back to do all over again
         put the result into gSendJammerID # To cancel sends when stop is pressed
         break
      case 2
         mergMicrophoneStopRecording
         mergMicrophoneStartRecording gFileNamePath3
         put "file:" & gFileNamePath2 into tGetFile
         put mergAVPlayerCreateFromURL (tGetFile) into gRecPointer2
         mergAVPlayerDo gRecPointer2,"play"
         put 3 into gCurrentFileNum
         send "playBackJammerStream" to me in 500 millisec
         put the result into gSendJammerID # To cancel sends when stop is pressed
         break
      case 3
         mergMicrophoneStopRecording
         mergMicrophoneStartRecording gFileNamePath1
         put "file:" & gFileNamePath3 into tGetFile
         put mergAVPlayerCreateFromURL (tGetFile) into gRecPointer3
         mergAVPlayerDo gRecPointer3,"play"
         put 1 into gCurrentFileNum
         send "playBackJammerStream" to me in 500 millisec
         put the result into gSendJammerID # To cancel sends when stop is pressed
         break
   end switch
end playBackJammerStream

and put this into the Stop button:

global gSendJammerID

on mouseUp
 # Stop recording and cancel the send timer
   if the environment is "mobile" then 
      mergMicrophoneStopRecording
      cancel gSendJammerID
   end if
end mouseUp

Best,

Steve

On Apr 26, 2014, at 7:29 PM, Nakia Brewer <Nakia.Brewer at westrac.com.au> wrote:

> Ah righto, that might explain why I cant get it to work.
> 
> I am pretty sure the file is being written real time from mergMicrophone as I can play the file using the other methods and it plays up to its current location when I called the play command (I am calling the play whilst I am still recording)..
> 
> It seems like LiveCode only evaluates the file on the initial play command and doesn't re evaluate the file whilst it playing hence only playing up to the duration that existed on the initial play command call...
> 
> As I am only playing audio do I need to even have a UI for the player? I have tried setting the visible of the player to false but still nothing played.
> 
> Thanks for taking a look at it, I appreciate it!
> 
> 
> 






More information about the use-livecode mailing list