Object MethodSystem Control

System.getVolume(stream)

Reads a stream’s actual volume index, permitted range, mute and fixed-volume state. Returns nil when the read is unavailable.

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. Result fields: stream, index, min, max, percent, fixed, muted. Percent is calculated from the actual index; it is undefined and nil when min==max. Invalid input or a failed read returns nil for the whole table.

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

This read-only call neither plays audio nor changes volume. Do not interpret nil as a zero volume level.

Example Usage

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

local state = System.getVolume("music")
if state ~= nil then
  print(state.index, state.min, state.max, state.percent, state.fixed, state.muted)
end

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 state = System.getVolume("music")
if state ~= nil then
  print(state.index, state.min, state.max, state.percent, state.fixed, state.muted)
end

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local state = System.getVolume("music")
if state ~= nil then
  print(state.index, state.min, state.max, state.percent, state.fixed, state.muted)
end
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 state = System.getVolume("music")
  if state ~= nil then
    print(state.index, state.min, state.max, state.percent, state.fixed, state.muted)
  end
end)

if not ok then
  print("API step failed: System.getVolume: " .. 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_getvolume_step()
  local state = System.getVolume("music")
  if state ~= nil then
    print(state.index, state.min, state.max, state.percent, state.fixed, state.muted)
  end
end

local ok, err = pcall(run_getvolume_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("alarm")
if state == nil then
  print("Volume unavailable")
else
  print(state.index, state.percent)
end