Object MethodSystem Control

System.setVolume(stream, percent)

Sets an audio stream to the supported step nearest the requested percentage and verifies the actual volume index; returns false if it cannot be verified.

Documented for app version 1.0.64Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

System is a global table: call its functions with a dot (System.<method>(...)), not a colon. Requires Macro Handler 1.0.62 or later.

When To Use It

In system flow, design steps as observe first and act second when the API can affect device state.

Parameters and Return

stream: music, ring, notification, alarm, system. percent: finite Lua number from 0–100; decimals are accepted. The percentage maps to Android’s minimum/maximum index range. Numeric strings, NaN, infinity and out-of-range values return false without side effects.

Options and Limits

Root is not required; Android audio policies apply. Fixed-volume devices, Do Not Disturb access and OS restrictions may reject an operation. The API does not request or elevate permission. Use it on the macro runtime thread; it does not perform work on the main UI thread.

Best Combined With

Read the actual level with System.getVolume. Choose Volume level in the System block’s Android Actions mode or use Automation.action("set_volume", {stream="music", percent=30}); that higher-level path treats false as an error.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local changed = System.setVolume("music", 30)
print("Volume verified", changed)

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
local changed = System.setVolume("music", 30)
print("Volume verified", changed)

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local changed = System.setVolume("music", 30)
print("Volume verified", changed)
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  local changed = System.setVolume("music", 30)
  print("Volume verified", changed)
end)

if not ok then
  print("API step failed: System.setVolume: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- In system flow, design steps as observe first and act second when the API can affect device state.
local function run_setvolume_step()
  local changed = System.setVolume("music", 30)
  print("Volume verified", changed)
end

local ok, err = pcall(run_setvolume_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local state = System.getVolume("music")
if state ~= nil and not state.fixed then
  assert(System.setVolume("music", 30), "Volume could not be verified")
end