Page 1 of 1

How would I create a text "button" that will play a sound?

Posted: Fri Feb 22, 2019 8:08 pm
by ScotsScripts
I just updated to a new phone and one of my widgets is no longer available (sound button free). This allowed me to create a widget with a text label (in this case the old time smiley face :) and associate it with a sound file that would play without putting anything on the screen except "stop" (which had replaced the :) text/button.)

Is there a way to do this with LL scripting and if so, do you have any tips or coding or anything to share that will help? Thanks!

Re: How would I create a text "button" that will play a sound?

Posted: Sat Feb 23, 2019 12:07 am
by F43nd1r
Create a blank item and set this as launch action with the sound file path as data:

Code: Select all

bindClass("android.media.MediaPlayer");
var item = getEvent().getItem();

function stop() {
item.setLabel(item.getTag("label"));
item.setTag("label",null);
item.my.player.stop();
item.my.player.release();
}

if(item.getTag("label")==null){
item.my.player = MediaPlayer.create(getActiveScreen().getContext(), Uri.parse("file://"+getEvent().getData()));
item.my.player.start();
item.my.player.setOnCompletionListener(function(mp){stop();});
item.setTag("label",item.getLabel());
item.setLabel("Stop");
} else {
stop();
}

Re: How would I create a text "button" that will play a sound?

Posted: Sun Feb 24, 2019 6:49 pm
by Pierrot
I'm pleased to know that someone is using the "my" object!

Re: How would I create a text "button" that will play a sound?

Posted: Sun Feb 24, 2019 6:53 pm
by F43nd1r
It was introduced after I asked for a runtime object storage. So not surprising that I am using it :D

Re: How would I create a text "button" that will play a sound?

Posted: Sun Feb 24, 2019 6:54 pm
by Pierrot
:D

Re: How would I create a text "button" that will play a sound?

Posted: Thu Mar 21, 2019 7:34 am
by TBog
F43nd1r wrote:
Sun Feb 24, 2019 6:53 pm
It was introduced after I asked for a runtime object storage. So not surprising that I am using it :D
Can you please share a link to the documentation of the "my" object?

Re: How would I create a text "button" that will play a sound?

Posted: Thu Mar 21, 2019 9:58 am
by TrianguloY
https://www.lightninglauncher.com/scrip ... html#getMy()
(note that with this javascript almost any 'X.getY().Z' can be replaced with 'X.Y.Z').

A Scriptable object is a javascript object, like a '{}', but it is stored on the item itself (similar to a tag, but can store any other object not only a string, although it is not saved).