Source language: Translate to:

Playsound from Variable

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Playsound from Variable

Postby TinTin » Mon Jun 01, 2015 6:11 am

SetVar [AudioFile] "C:\1-A.mp3"
PlaySound "[AudioFile]" false

Doesn't seem to work.. any workaround? ;0)

Thanks
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby Gaev » Mon Jun 01, 2015 6:59 am

TinTin:

Try ...
Code: Select all
SetVar [AudioFile] "file:///C:\\1-A.mp3"
AlertBox "thisFile" "[thisFile]" ""
PlaySound "[AudioFile]" false

1) you need to specify the protocol for the url (file:/// when it is on a local disk)

2) in javascript, the backslash \ is an escape character (e.g. \n is the code for new line) ... so you need to replace each such character with two of them like this \\

Note: the AlertBox command is there just so you can see the contents of the variable.
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Playsound from Variable

Postby TinTin » Mon Jun 01, 2015 7:16 am

As ever you come with the solution - :-)

Working great now!

Cheers
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby TinTin » Mon Jun 01, 2015 10:55 am

Gaev,

Do you know if there's an easy way to ensure the files are added to the manifest, because if a file is directly assigned to playsound and not as variable
NAB seems to automatically add this file on compile..
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby Neosoft Support » Tue Jun 02, 2015 10:58 am

If you use a variable to store the name of the sound file, then the compiler won't be able to compile it into the finished project. Some type of embedded files option similar to NeoBook's would be useful here. However, it's a bit more complicated because files need to be moved and their paths modified in code so that everything works after the compile, and also works in test mode.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5603
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: Playsound from Variable

Postby TinTin » Wed Jun 03, 2015 8:22 am

ok, is there anyway to run a subroutine referenced from a variable?
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby Gaev » Wed Jun 03, 2015 10:09 am

Tin:

ok, is there anyway to run a subroutine referenced from a variable?

If you mean something like ...
Code: Select all
[doThisSubroutine] "[param1]" "[parm2]"
... the answer is No ... because NeoApp can not compile [doThisSubroutine] (it has no value at compile time).

You might be able to build a generic (javascript) subroutine that uses the (javascript) command called eval to invoke the contents of a variable ... but I would not recommend it because ...

- it is open to hacking (a user might be able to populate the variable with damaging commands)
- App stores might not allow it
- you would also need to pass a variable number of parameters required for each of the different subroutines
- how do you check against invalid subroutine names ?

You might be better off to have a routine with If/Then statements that compare the variable with a hardcoded value, and then invoke the hardcoded command if there was a match.

Can you provide a use case of where you would want such a facility ?
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Playsound from Variable

Postby TinTin » Thu Jun 04, 2015 2:45 am

Hi Gaev,

Yes all valid points regarding security and I would agree it would probably be rejected by app store for this... here's my scenario

I wan't to play 3 audio files in succession

Playsound audio file 1
.//
Playsound audio file 2
.//
Playsound audio file 3

however I may have a total of 30+ audio files I wan't to play in groups of 3 each time but randomly.

etc Each audio file is named

1st group of files (random play order of the 3 files)
audiofile-1-1.mp3
audiofile-1-2.mp3
audiofile-1-3.mp3

2nd group of files (random play order of the 3 files)
audiofile-2-1.mp3
audiofile-2-2.mp3
audiofile-2-3.mp3

3rd group of files (random play order of the 3 files)
audiofile-3-1.mp3
audiofile-3-2.mp3
audiofile-3-3.mp3


I was generating a random number each time to populate each audio file variable which works locally but struggle later on (and the audio file needs to be present
so that is an issue if audio files were missing so not the best solution)

example (do not have the app on this laptop so the code is just example)


.// So group 2 of audio files / played randomly

setvar [currentgroup] "2"
random no generated between 1-3

playsound "audiofile-[currentgroup]-[Random].mp3"
Delay "1000"
playsound "audiofile-[currentgroup]-[Random].mp3"
Delay "1000"
playsound "audiofile-[currentgroup]-[Random].mp3"


However if I was to hardcode the referenced audio file within playsound and use 'if' statements for each group of 3 - the play order of the 3 files each would be the same
everytime the app is run.. or maybe I am just looking at it the wrong way..

Even with a subroutine for each group of 3 audio files - that's a great deal of code for something which needs to be quite simple especially if there are 40+ audio files
which is generally doing the same thing each time - only with an audio file which is different.

I'm sure there are better ways to do it..
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby Gaev » Thu Jun 04, 2015 6:18 am

TinTin:

1) There is no problem calling a subroutine which has a command like ...
Code: Select all
PlaySound "audiofile-[currentgroup]-[Random].mp3" false


2) The bigger issue is "how to reference your associated Audio files" ...

- locally when designing/developing your App
- on a website when running as a WebApp
- on the device when running as a native App on a device

... the solution is to always have these files in the same folder (or same relative folder) as the App ... that way, you can have the same code work in all environments by using a relative file reference ... like "audiofile-1-2.mp3" or "myAudioFolder/audiofile-1-2.mp3".

I am sure Dave will come up with an easy way (for us) to do this ... perhaps similar to [Embedded] facility in NeoBook ... in the mean time, you might find out if it is possible to play audio files located on a website from ...

- your App at Design time
- your App running as a native App in a device (if this is the intended use)

... if it can, just place all your audio files in a folder on your website ... otherwise, consider a temporary workaround by have a global variable (e.g. [audioFilesPrefix]) that you change just before compile/publish to website (still does not provide a solution for native Apps) ... and use something like ...
Code: Select all
PlaySound "[audioFilesPrefix]audiofile-[currentgroup]-[Random].mp3" false
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Playsound from Variable

Postby TinTin » Sat Jun 06, 2015 1:56 am

Thanks Gaev,

Some great pointers there, yes the native app is the goal - problem with playback from web is that you need to rely on internet connection on initial launch of the app,
I may just have to look at reference every audio file in each block of 3 - that will ensure the files are there.. and then call a random sub routine from the group of 3 instead.
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby Gaev » Sat Jun 06, 2015 3:43 pm

TinTin:

yes the native app is the goal - problem with playback from web is that you need to rely on internet connection on initial launch of the app,

I did a Google search for "playing audio files in native App" ... just a couple of the returned links ...

http://forum.ionicframework.com/t/how-t ... les/7479/9

http://code.tutsplus.com/tutorials/buil ... bile-16347

... the second one gives you an idea about the scope (complexity) of such an undertaking (requires use of PhoneGap).

I may just have to look at reference every audio file in each block of 3 - that will ensure the files are there.. and then call a random sub routine from the group of 3 instead.

Are you saying that if you had a command like ...
Code: Select all
PlaySound "!file:///c:\\myAudioFiles\\cat.mp3" false
... then you are able to have it play in a native App ? ... I ask because I do not have the means to verify this.

If the above is possible, then all you need is a subroutine with one such command for each of your 40 audio files ...
Code: Select all
SetVar [fileIndex] [thisGroup].[thisFile]

If [fileIndex] = 1.1
   playsound "!file:///c:\\myAudioFiles\\audiofile-1-1.mp3"
Endif

If [fileIndex] = 1.2
   playsound "!file:///c:\\myAudioFiles\\audiofile-1-2.mp3"
Endif

If [fileIndex] = 1.3
   playsound "!file:///c:\\myAudioFiles\\audiofile-1-3.mp3"
Endif

...

If [fileIndex] = 13.3
   playsound "!file:///c:\\myAudioFiles\\audiofile-13-3.mp3"
Endif

Give the first parameter for the subroutine an alias of [thisGroup] and the second parameter an alias of [thisFile].

Now, once you compute values for [currentgroup] and [random], call this subroutine with the desired values ...
Code: Select all
SetVar [currentgroup] 2
RandomEx 1 3 [random]
playThisFile "[currentgroup]" "[random]"


Just need to add one If/Endif code block for each file.
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Playsound from Variable

Postby TinTin » Tue Jun 09, 2015 3:09 am

Hi Gaev,

Have had a little break from coding - I have found that referencing audio files directly like

PlaySound "c:\cat.mp3" false

Does work in Native app via Phonegap build (only on ios - Android seems to be different and I think this could well be because they need the !file://
format (I am yet to try this though)

PlaySound "!file:///c:\\myAudioFiles\\cat.mp3" false

I'm still struggling to find a way and even then when I reference the audio file directly it's pretty hit and miss.. :-(

Hoping Dave will release an updated BETA soon ;-)
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK

Re: Playsound from Variable

Postby TinTin » Mon Jun 15, 2015 11:53 am

just an update on this - after taking some time out and having a long rest... it turned out it was the filename which was clashing on the build process, so it would only allow 1.mp3 2.mp3 and 3.mp3 during compile from one directory, the good news is once I gave them a unique filename I can now get the result I wanted as I'm using a variable
for each set of 3 audio files.

For those who are interested audio is working well on a compiled iOS app through Phonegap build, I do however need to test on Android, has anyone had any issues with audio on this platform using Phonegap Build to the APK?

cheers
TinTin
 
Posts: 162
Joined: Sun Dec 06, 2009 4:03 am
Location: UK


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 1 guest