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

This is the place to ask "How do I ...?"
Post Reply
User avatar
ScotsScripts
Posts: 1
Joined: Fri Feb 22, 2019 7:58 pm

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

Post by ScotsScripts » Fri Feb 22, 2019 8:08 pm

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!

User avatar
F43nd1r
Posts: 26
Joined: Thu Jan 24, 2019 1:20 pm

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

Post by F43nd1r » Sat Feb 23, 2019 12:07 am

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();
}

User avatar
Pierrot
Site Admin
Posts: 155
Joined: Wed Jan 23, 2019 12:18 pm
Location: French Alps

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

Post by Pierrot » Sun Feb 24, 2019 6:49 pm

I'm pleased to know that someone is using the "my" object!

User avatar
F43nd1r
Posts: 26
Joined: Thu Jan 24, 2019 1:20 pm

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

Post by F43nd1r » 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

User avatar
Pierrot
Site Admin
Posts: 155
Joined: Wed Jan 23, 2019 12:18 pm
Location: French Alps

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

Post by Pierrot » Sun Feb 24, 2019 6:54 pm

:D

User avatar
TBog
Posts: 1
Joined: Sun Jan 27, 2019 8:30 pm
Location: România
Contact:

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

Post by TBog » Thu Mar 21, 2019 7:34 am

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?

User avatar
TrianguloY
Posts: 77
Joined: Thu Jan 24, 2019 9:46 am

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

Post by TrianguloY » Thu Mar 21, 2019 9:58 am

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).

Post Reply