Skip to main content

Upload and play audio files

Important:
  • Audio Injection feature is currently in Private Beta phase and is supported only on Android 11 and above devices. Reach out to our Support team to enable the Audio Injection feature for your profile.
  • Please note that setting this feature will lead to an increase of ~20 seconds in session start-time. Thus, users are expected to use this feature based on their needs.

Introduction

Browserstack App Automate enables you to upload audio files on remote Android devices and test various use cases, such as testing apps that require access to microphone and audio output, linking the microphone to your app, testing the microphone recording feature, etc.

In this guide, you will learn how to:

Supported OS and devices

Supported Devices Android Version
Samsung Galaxy S22 12
Samsung Galaxy S22 Ultra 12
Samsung Galaxy S21 11
Google Pixel 6 12

Upload your audio files

Prior to starting the test execution, upload the audio files using media REST APIs.

curl -u "username:accesskey" \
-X POST "https://api-cloud.browserstack.com/app-automate/upload-media" \
-F "file=@/path/to/audio/file/<filename>.mp3" \
-F "custom_id=SampleMedia"
Note:
  • Allowed file formats for audio - .mp3, .ogg, .aac, and .wav.
  • Maximum file size - 15 MB for .mp3, .aac, and .ogg, and 100 MB for .wav
  • You can upload only one audio file at a time.
  • Uploaded files will persist on the Browserstack server only for 30 days.

The REST API returns a response media_url for each successful file upload.

{
    "media_url": "media://<hashedid>"
}
Note: The media_url received is used to refer the audio file in test script.

Set the Audio Injection capability

Use the capability values enableAudioInjection for W3C protocol and browserstack.enableAudioInjection for JSON wire protocol in your test scripts.

Capability Description
enableAudioInjection Set this capability to "true" if you want to use your audio files in the test. Upload your audio files to BrowserStack servers using REST API.
Capability Description
browserstack.enableAudioInjection Set this capability to "true" if you want to use your audio files in the test. Upload your audio files to BrowserStack servers using REST API.

Example:

DesiredCapabilities capabilities = new DesiredCapabilities();
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
browserstackOptions.put("enableAudioInjection", "true");
capabilities.setCapability("bstack:options", browserstackOptions);
capabilities.setCapability("platformName", "android");
// Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
capabilities.setCapability("platformVersion", "12.0");
capabilities.setCapability("deviceName", "Samsung Galaxy S22");
var capabilities = {
	"platformName" : "android",
	// Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
	"appium:platformVersion" : "12.0",
	"appium:deviceName" : "Samsung Galaxy S22",
		'bstack:options' : { 
			"enableAudioInjection" : "true"
		}
}
AppiumOptions capabilities = new AppiumOptions();
Dictionary<string, object> browserstackOptions = new Dictionary<string, object>();
browserstackOptions.Add("enableAudioInjection", "true");
capabilities.AddAdditionalCapability("bstack:options", browserstackOptions);
capabilities.AddAdditionalCapability("platformName", "android");
// Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
capabilities.AddAdditionalCapability("platformVersion", "12.0");
capabilities.AddAdditionalCapability("appium:deviceName", "Samsung Galaxy S22");
desired_cap = {
	"platformName" : "android",
	# Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
	"platformVersion" : "12.0",
	"deviceName" : "Samsung Galaxy S22",
	'bstack:options' : {
		"enableAudioInjection" : "true",
	},
}
capabilities = {
	"platformName" => "android",
	# Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
	"platformVersion" => "12.0",
	"deviceName" => "Samsung Galaxy S22",
	'bstack:options' => {
		"enableAudioInjection" => "true",
	},
}
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("browserstack.enableAudioInjection","true");
// Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
caps.setCapability("os_version", "12.0");
caps.setCapability("device", "Samsung Galaxy S22");
var capabilities = {
	"browserstack.enableAudioInjection" : "true",
	// Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
	"os_version" : "12.0",
	"device" : "Samsung Galaxy S22"
}
AppiumOptions capabilities = new AppiumOptions();
capability.AddAdditionalCapability("browserstack.enableAudioInjection", "true");
// Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
capability.AddAdditionalCapability("os_version", "12.0");
capability.AddAdditionalCapability("device", "Samsung Galaxy S22");
desired_cap = {
	"browserstack.enableAudioInjection" = "true",
	# Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
	"os_version" : "12.0",
	"device" : "Samsung Galaxy S22"
}
caps = Selenium::WebDriver::Remote::Capabilities.new
caps["browserstack.enableAudioInjection"] = "true"
# Ensure to use Android version 11 or above. Passing an invalid version (below Android 11 or non-Android OS) drops the session.
caps["os_version"] = "12.0"
caps["device"] = "Samsung Galaxy S22"

Add audio injection logic

The uploaded audio file is played on the remote device using custom JavaScript executors. Your test script must be modified to add these custom executors along with the logic to click the UI element that in the mobile apps that triggers the microphone and plays the audio in a specified order.

Ensure that the following order is maintained in your script:

  1. Add the inject audio executor statement
  2. Add framework logic to click a UI element
  3. Add the start audio executor statement
  4. Add the implicit wait to play complete audio
  5. Add the stop audio executor statement

Inject audio

Specify the audio file to play during the test using the following custom executor. Set the audioUrl argument with the media_url you received on successful upload of the audio file.

driver.execute_script('browserstack_executor: {\"action\":\"injectAudio\", \"arguments\": {\"audioUrl\" : \"media://<hashedid>\"}}')

Add framework command to click UI element

Your script must include the logic to handle any permission-related popups that might be encountered. Check out Handle permission pop-ups for more information.

Start audio

After you click the UI element, the device starts listening for input. Use the startAudio action to play the audio.

driver.execute_script('browserstack_executor: {\"action\":\"startAudio\"}')

Add the implicit wait to play complete audio

Ensure that you add the logic to add implicit waits in your script such that the stopAudio action occurs only after the complete audio is played.

Note: The default idle timeout for a session is 90 sec. The idle timeout can be extended to 5 minutes using these workarounds).

Stop audio

Use the stopAudio action to stop the audio.

driver.execute_script("browserstack_executor: {\"action\":\"stopAudio\"}")

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy