Media volume is mute in voice commands

115 views
Skip to first unread message

Mighty Apps Studio

unread,
Aug 29, 2018, 8:51:06 AM8/29/18
to DroidScript
Hi guys, in the voice commands sample, we can't increase the media volume as shown in the screenshot. Is it possible to increase it? It's permanantly locked
Screenshot_2018-08-29-16-47-09.png

BareK

unread,
Aug 29, 2018, 9:55:23 AM8/29/18
to DroidScript
I think this is because the text to speech in this demo uses the system stream (by default).
More informations here:
https://groups.google.com/d/msg/androidscript/cEl25alr2Hg/g5Rc3AJjCQAJ

Mighty Apps Studio

unread,
Aug 31, 2018, 10:14:06 AM8/31/18
to DroidScript
Thanks but how do i totally change it?

BareK

unread,
Aug 31, 2018, 10:47:06 AM8/31/18
to DroidScript
If you can share code you tried I will try to test/modify it :)

Mighty Apps Studio

unread,
Aug 31, 2018, 11:29:30 AM8/31/18
to DroidScript

//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

//Create an edit control.
edit = app.CreateTextEdit( "Hello", 1, 0.9, "" );
edit.SetBackColor( "#222222" );
edit.SetTextColor( "#ffffff" );
lay.AddChild( edit );

//Create a horizontal layout for icon buttons.
layBut = app.CreateLayout("Linear", "Horizontal");
lay.AddChild( layBut );

//Create an array of icon buttons.
var btns = ["[fa-undo]","[fa-search]","[fa-file]","[fa-save]","[fa-repeat]"];
for( var i=0; i<btns.length; i++ )
{
btn = app.CreateButton( btns[i], -1, 0.06, "FontAwesome" );
btn.icon = btns[i];
btn.SetTextSize( 22 );
btn.SetOnTouch( btns_OnTouch );
layBut.AddChild( btn );
}

//Add layout to app.
app.AddLayout( lay );
}

//Handle button presses.
function btns_OnTouch()
{
switch( this.icon )
{
case "[fa-undo]": edit.Undo(); break;
case "[fa-repeat]": edit.Redo(); break;
case "[fa-search]": alert("Todo!"); break;

case "[fa-file]":
edit.SetText( app.ReadFile( "/sdcard/testfile.txt" ) );
break;
case "[fa-save]":
app.WriteFile( "/sdcard/testfile.txt", edit.GetText() );
break;
}
}

This is the voice commands sample i was modifying it

BareK

unread,
Aug 31, 2018, 11:45:04 AM8/31/18
to DroidScript
There is no voice command or text to speech here.
It seems to be a text editor sample app.

Mighty Apps Studio

unread,
Aug 31, 2018, 11:58:34 AM8/31/18
to DroidScript
Ohh sorry wrong code...

//Note: Offline recognition engines can be downloaded in the
//Android Language and Input settings (recommended for speed).
//Higher quality voices may also be downloaded.

//Called when application is started.
function OnStart()
{

//Create a main layout with objects vertically centered.
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
lay.SetBackground( "/res/drawable/pattern_carbon", "repeat" );

var s = "<u>Commands</u><br><br>"
+ "\"Computer?\"<br>"
+ "\"What time is it?\"<br>"
+ "\"What day is it?\"<br>"
+ "\"How are you?\"<br>"
+ "\"Exit\"<br>";
txt = app.CreateText( s, 0.9, 0.8, "Multiline,Html" );
txt.SetTextSize( 32 );
lay.AddChild( txt );



//Add layout to app.
app.AddLayout( lay );

//Create recognition object and set callbacks.
speech = app.CreateSpeechRec("NoBeep,Parxtial");
speech.SetOnResult( speech_OnResult );
speech.SetOnError( speech_OnError );

//Say something at start (to get speech engine ready).
app.TextToSpeech( "Your wish is my command", 1, 1.5, Listen );
app.ShowProgress();
}

//Start recognizing.
function Listen()
{
app.HideProgress();
speech.Recognize();
}

//Called with the recognition result(s).
function speech_OnResult( results, partial )
{
//Get result.
var cmd = results[0].toLowerCase();

//Watch for key phrases.
if( cmd.indexOf("computer") > -1 )
{
//speech.Cancel();
app.TextToSpeech( "Yes Master?", 1,2, Listen );
}
else if( cmd.indexOf("what")>-1 && cmd.indexOf("time")>-1 )
{
//speech.Cancel();
app.TextToSpeech( GetTime(), 1,2, Listen );
}
else if( cmd.indexOf("what")>-1 && cmd.indexOf("day")>-1 )
{
// speech.Cancel();
app.TextToSpeech( GetDay(), 1,2, Listen );
}
else if( cmd.indexOf("how")>-1 && cmd.indexOf("you")>-1 )
{
//speech.Cancel();
app.TextToSpeech( "I'm feeling good", 1,2, Listen );
}
else if( cmd.indexOf("exit")>-1 )
{
app.Exit();
}

//Restart recognition.
else speech.Recognize();
}


//Called if recognition fails.
function speech_OnError( error )
{
console.log( "Error" + error );

//Restart recognition.
if( !speech.IsListening() ) speech.Recognize();
}

//Get the current time.
function GetTime()
{
var d = new Date();
return d.getHours() + " " + d.getMinutes();
}

//Get the current day.
function GetDay()
{
var d = new Date();
var weekday = new Array(7);
weekday[0]= "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
return weekday[d.getDay()];
}

This is theright one

BareK

unread,
Aug 31, 2018, 12:31:32 PM8/31/18
to DroidScript
Looking at this post from Steve Garman, you should be able to force your volume buttons in your app to control the volume stream you want:

Add this on top of your OnStart function:
app.DisableKeys( 'VOLUME_UP,VOLUME_DOWN' );
app
.SetOnKey( OnKeyPressed );

And this where you want:
function OnKeyPressed( type, name )
{
   
if( name == "VOLUME_UP" && type == "Up" )
   
{
       
var volume = app.GetVolume( 'music' );
        volume
+= 0.1;
        app
.SetVolume( 'music', volume );
       
       
var newVolume = app.GetVolume( 'music' );
        app
.ShowPopup( 'music volume: ' + newVolume, 'Short' );
   
}
   
else if( name == "VOLUME_DOWN" && type == "Up" )
   
{
       
var volume = app.GetVolume( 'music' );
        volume
-= 0.1;
        app
.SetVolume( 'music', volume );
       
       
var newVolume = app.GetVolume( 'music' );
        app
.ShowPopup( 'music volume: ' + newVolume, 'Short' );
   
}
   
else return;
}

It reacts on the 'UP' event (so you have to release the button if you want the volume to be increased/decreased by 0.1).

And to make the TextToSpeech use the 'music' stream:
app.TextToSpeech( "Your wish is my command", 1, 1.5, Loop, 'music' );

But strangely, on my device, after calling speech.Recognize, the volume of the music stream seems to be muted.
Anyway, using the volume buttons with the code provided above restore the volume.

Mighty Apps Studio

unread,
Aug 31, 2018, 12:59:10 PM8/31/18
to DroidScript
I tried this and the music stream is still muted

BareK

unread,
Aug 31, 2018, 1:01:05 PM8/31/18
to DroidScript
This might be something specific to your device / android version.
What are they?

Mighty Apps Studio

unread,
Aug 31, 2018, 1:18:35 PM8/31/18
to DroidScript
Android ver is 4.4 kitkat and my device is a samsung galaxy grand prime

Mighty Apps Studio

unread,
Aug 31, 2018, 4:05:25 PM8/31/18
to DroidScript
did it work for you

BareK

unread,
Aug 31, 2018, 5:09:11 PM8/31/18
to DroidScript
Yes it work well on my device.
I will see if I can grab a kitkat one to test on it.

Mighty Apps Studio

unread,
Sep 1, 2018, 9:12:39 AM9/1/18
to DroidScript
When it's recognizing it mutes the music stream, if i say play some music using voice commands then i cant lsten to it because the music stream is muted

Steve Garman

unread,
Sep 1, 2018, 9:42:09 AM9/1/18
to DroidScript
It doesn't seem unreasonable hat it wants to hear what you're saying when you ask it to recognize it.

Have you tried resetting the volume straight after there may gave been a Recognize command?

Something like:

app.SetVolume( "music",1 );
}

//Called with the recognition result(s).
function speech_OnResult( results, partial )
{
//Get result.
var cmd = results[0].toLowerCase();

//Watch for key phrases.
if( cmd.indexOf("computer") > -1 )
{
//speech.Cancel();
app.TextToSpeech( "Yes Master?", 1,2, Listen );
}
else if( cmd.indexOf("what")>-1 && cmd.indexOf("time")>-1 )
{
//speech.Cancel();
app.TextToSpeech( GetTime(), 1,2, Listen );
}
else if( cmd.indexOf("what")>-1 && cmd.indexOf("day")>-1 )
{
// speech.Cancel();
app.TextToSpeech( GetDay(), 1,2, Listen );
}
else if( cmd.indexOf("how")>-1 && cmd.indexOf("you")>-1 )
{
//speech.Cancel();
app.TextToSpeech( "I'm feeling good", 1,2, Listen );
}
else if( cmd.indexOf("exit")>-1 )
{
app.Exit();
}

//Restart recognition.
else speech.Recognize();

app.SetVolume( "music",1 );
}


//Called if recognition fails.
function speech_OnError( error )
{
console.log( "Error" + error );

//Restart recognition.
if( !speech.IsListening() ) speech.Recognize();

app.SetVolume( "music",1 );

Mighty Apps Studio

unread,
Sep 1, 2018, 10:14:55 AM9/1/18
to DroidScript
I tried it but it doesnt work

BareK

unread,
Sep 1, 2018, 12:50:29 PM9/1/18
to DroidScript
This works well on android 4.0.4 so it should be fine on 4.4 I guess:

//Note: Offline recognition engines can be downloaded in the
//Android Language and Input settings (recommended for speed).
//Higher quality voices may also be downloaded.

//Called when application is started.
function OnStart()
{

    app
.SetOrientation( 'Portrait' );

   
    app
.DisableKeys( 'VOLUME_UP,VOLUME_DOWN' );
    app
.SetOnKey( OnKeyPressed );

   
   
//Create a main layout with objects vertically centered.

    lay
= app.CreateLayout( "Linear", "VCenter,FillXY" );  
    lay
.SetBackground( "/res/drawable/pattern_carbon", "repeat" );

   
var s = "<u>Commands</u><br><br>"

       
+ "\"What time is it?\"<br>"
       
+ "\"What day is it?\"<br>"

       
+ "\"Play\"<br>"
       
+ "<br><br><br>"
       
+ "\"Volume\"<br>"
       
+ "\"Up\"<br>"
       
+ "\"Down\"<br>"

       
+ "\"Exit\"<br>";
    txt
= app.CreateText( s, 0.9, 0.8, "Multiline,Html" );
    txt
.SetTextSize( 32 );
    lay
.AddChild( txt );
 
   
//Add layout to app.    
    app
.AddLayout( lay );
   
   
//Create recognition object and set callbacks.
    speech
= app.CreateSpeechRec("NoBeep,Parxtial");
    speech
.SetOnResult( speech_OnResult );
    speech
.SetOnError( speech_OnError );
 
   
//Say something at start (to get speech engine ready).

    app
.TextToSpeech( "Your wish is my command", 1, 1.5, Listen, "music" );
    app
.ShowProgress( 'Init' );
   
    player
= app.CreateMediaPlayer();
    player
.SetFile( '/Sys/Snd/Poing.ogg' );
    player
.SetOnReady( function() { console.log( 'player is ready' ); } );

}

function OnKeyPressed( type, name )
{
   
if( name == "VOLUME_UP" && type == "Up" )
   
{
       
var volume = app.GetVolume( 'music' );
        volume
+= 0.1;
        app
.SetVolume( 'music', volume );
       
       
var newVolume = app.GetVolume( 'music' );
        app
.ShowPopup( 'music volume: ' + newVolume, 'Short' );
   
}
   
else if( name == "VOLUME_DOWN" && type == "Up" )
   
{
       
var volume = app.GetVolume( 'music' );
        volume
-= 0.1;
        app
.SetVolume( 'music', volume );
       
       
var newVolume = app.GetVolume( 'music' );
        app
.ShowPopup( 'music volume: ' + newVolume, 'Short' );
   
}
   
else return;
}

//Start recognizing.
function Listen()
{
    app
.ShowProgress( 'Lintening', 'NoDim' );
    speech
.Recognize();

}

//Called with the recognition result(s).
function speech_OnResult( results, partial )
{

    app
.HideProgress();

   
   
//Get result.
   
var cmd = results[0].toLowerCase();

   
    console
.log( 'Understood: ' + cmd );
   
   
//Watch for key phrases.

   
if( cmd.indexOf("what")>-1 && cmd.indexOf("time")>-1 )
   
{

        app
.TextToSpeech( GetTime(), 1,2, Listen, "music"  );

   
}
   
else if( cmd.indexOf("what")>-1 && cmd.indexOf("day")>-1 )
   
{

        app
.TextToSpeech( GetDay(), 1,2, Listen, "music" );
   
}
   
else if( cmd.indexOf("play")>-1 )
   
{
        player
.SeekTo( 0 );
        player
.Play();
       
Listen();
   
}
   
else if( cmd.indexOf("volume")>-1 )
   
{
        app
.TextToSpeech( app.GetVolume( 'music' ), 1, 2, Listen, 'music' );
   
}
   
else if( cmd.indexOf("up")>-1 )
   
{
        app
.SetVolume( 'music', app.GetVolume( 'music' ) + 0.1 );
        app
.TextToSpeech( app.GetVolume( 'music' ), 1, 2, Listen, 'music' );
   
}
   
else if( cmd.indexOf("down")>-1 )
   
{
        app
.SetVolume( 'music', app.GetVolume( 'music' ) - 0.1 );
        app
.TextToSpeech( app.GetVolume( 'music' ), 1, 2, Listen, 'music' );

   
}
   
else if( cmd.indexOf("exit")>-1 )
   
{
        app
.Exit();
   
}
   
   
//Restart recognition.
   
else

   
{
        app
.Alert( 'cmd: ' + cmd, 'Unknown' );
       
Listen();

   
}
}

//Called if recognition fails.
function speech_OnError( error )
{
    console
.log( "Error" + error );
   
   
//Restart recognition.
   
if( !speech.IsListening() ) speech.Recognize();
}

Mighty Apps Studio

unread,
Sep 1, 2018, 1:42:50 PM9/1/18
to DroidScript
Thanks bro but this is not working there definitely a problem with my device

Chris Larcombe

unread,
Dec 29, 2018, 7:43:49 AM12/29/18
to DroidScript
If media is playing it appears to pause it, but when using DS Audio player behavior is different. I had the same problem with media volume muting and not being able to alter volume programatically. I was able to resolve this by removing "NoBeep" option when creating speech rec object.

Try removing NoBeep from CreateSpeechRec options:

So it looks like this:

 speech = app.CreateSpeechRec("Parxtial");

Or this:

 speech = app.CreateSpeechRec();
Reply all
Reply to author
Forward
0 new messages