3

I have a set of rooted android devices I can access through adb over tcp/ip. I am trying to identify a specific one by playing a sound from the command line (one of the .ogg files from /system/media/audio).

I know I could probably build an app for this but it feels like an overkill to use an APK, and I'm hoping there is a more native way. I already know how to start intents from the command line.

Things I have tried :

  • Investigated using service call media.player but could not find any useful reference about the syntax. I can see with dumpsys media.player references about AwesomePlayer but couln't get anything going.
  • Tried to find a compiled version of "/system/bin/media" from the android source code, as it is missing from the device, but without any luck so far.
  • Tried to build a command line java app to call android.media.MediaPlayer with dalvikvm but my knowledge of Android and java is too limited (class compiled but complained about missing dependencies at runtime)

Any ideas?

4

3 Answers 3

6

After some trial and error, I will answer what is now working for me.

Based on other threads, I found the following java code to be the bare minimum to play some sound through the speaker. The MediaPlayer class seems the only one that will play with no user interaction, as others need a real activity context...

public class Beep {

    public static void main(String[] a) {

        MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource("/system/media/audio/alarms/Ticktac.ogg");
            mp.prepare();
            mp.start();
        }
        catch (IOException e) {
            Log.w("Beep", "IOException", e);
        }
}

(actual working example is here)

This must then be compiled as a dex file, linked to android.jar,
and can finally be run from the adb shell command line using app_process (not dalvikvm) :

export CLASSPATH=./Beep.dex
app_process /system/bin Beep /system/media/audio/ringtones/Beep-beep.ogg

It is not the one-liner I was hoping for but does what I needed...

6

As seen in this other thread

you can do this with a single shell command if you have a file there:

am start -a android.intent.action.VIEW -d /sdcard/somthing/example.wav -t audio/wav
3
  • You can also use audio/* for any type of audio file.
    – Pixsa
    Commented Aug 17, 2020 at 11:21
  • If I try to run this in Termux, I get Error: Activity not started, unable to resolve Intent ... Or does this work only on rooted devices? Commented Apr 12, 2021 at 11:28
  • 2
    All right, it seems to work if I prepend the path with file://. Commented Apr 12, 2021 at 14:22
1

adb shell am start -a android.intent.action.VIEW -d file:///sdcard/download/mysong.mp3 -t audio/mp3

You can use this as an cmd command

"sdcard/download" is the path for "download" folder in the Internal storage.

You can also use the path "storage/emulated/0/download/" for the internal storage's download folder"

You will get the below error if, file not found due to incorrect location or filename has spaces or re-running the command again before closing the previous activity

file name must not have any spaces otherwise give it as a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.