Skip to content

Instantly share code, notes, and snippets.

@bendenoz
Last active July 24, 2024 13:35
Show Gist options
  • Save bendenoz/d9832f24caa6c826f1c4 to your computer and use it in GitHub Desktop.
Save bendenoz/d9832f24caa6c826f1c4 to your computer and use it in GitHub Desktop.
Basic class to play system sound without user interaction from adb command line. Must be compiled to .dex file
import android.media.MediaPlayer;
import android.media.AudioManager;
import android.util.Log;
import java.io.IOException;
public class Beep {
/**
* Command-line entry point.
*
* @param args The command-line arguments
*/
public static void main(String[] args) {
(new Beep()).run(args);
}
private void run(String[] a) {
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(a[0]);
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mp.setVolume(1f,1f);
mp.prepare();
mp.start();
}
catch (IOException e) {
Log.w("Beep", "IOException", e);
}
System.out.print("Playing " + mp.getDuration() + "ms");
int i = 0;
while(mp.isPlaying() && i < 20) {
System.out.print(".");
i++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
Log.w("Beep", "IOException", e);
}
}
System.out.println();
mp.stop();
mp.release();
System.out.println("Stopped.");
}
}
export CLASSPATH=./Beep.dex
app_process /system/bin Beep /system/media/audio/ringtones/Beep-beep.ogg
@fogged
Copy link

fogged commented Oct 16, 2017

is this method still work on android 7 Nougat or 8 Oreo?

@alimsk
Copy link

alimsk commented Dec 22, 2021

@fogged its working fine on android Q (no root), so yes i guess

@Mitezuss
Copy link

@fogged its working fine on android Q (no root), so yes i guess

do you know if is working on Android 12 ?

i get error

@mocarela
Copy link

I see it playing on Android 9, but no sound comes out.
Any idea what could be wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment