I am trying to build calling app like what's app. When user call to another user then beep beep lopping sound start util call accept or decine.
private val progressPlayer: MediaPlayer by lazy {
MediaPlayer.create(applicationContext, R.raw.progress_out).apply { isLooping = true }
}
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
val isSpeakerOn = audioManager.isSpeakerphoneOn
if (isSpeakerOn) {
audioManager.isSpeakerphoneOn = false
}
audioManager.mode = AudioManager.MODE_IN_CALL
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0)
progressPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL)
progressPlayer.start()
I am testing this code android 13. it's not working. Sound always playing in speaker mode.
Related
The Audio interface allows to set its volume, but apps usually use the Media or Notifications volumes on Android. In a Gluon Mobile app, pressing the volume keys does nothing, while in other apps the device's volume changes.
Tested on Android 8 and 12 using Attach version 4.0.15.
Is there a way to play audio using the device's volume settings and allow the user to adjust the volume from within the device?
There seems to be no proper way to do this. Using the VideoService it's possible to change the volume of the device, but only while audio is playing, which requires the user to be ready to do so for short audio clips.
The VideoService also does not support playing a single file out of a playlist. The only solution I found was switching the playlist to a single audio clip whenever it's required to be played and isn't played already:
class MobileNotifier {
private static final String SMALL_BEEP_PATH = "/sounds/SmallBeep.wav";
private static final String BIG_BEEP_PATH = "/sounds/BigBeep.wav";
VideoService service;
private MobileNotifier(VideoService service) {
this.service = service;
service.getPlaylist().add(SHORT_BEEP_PATH);
}
public void play(Alert alert) {
switch (alert) {
case SMALL -> {
if (service.statusProperty().get() != Status.PLAYING || !SMALL_BEEP_PATH.equals(service.getPlaylist().get(0))) {
service.stop();
service.getPlaylist().set(0, SMALL_BEEP_PATH);
service.play();
}
}
case BIG -> {
if (service.statusProperty().get() != Status.PLAYING || !BIG_BEEP_PATH.equals(service.getPlaylist().get(0))) {
service.stop();
service.getPlaylist().set(0, LONG_BEEP_PATH);
service.play();
}
}
};
}
}
I developed a simple application to turn ON/OFF flashlight. But I couldn't find a way to turn on front flashlight of a device(if available).
Code for availability check :
boolean hasCameraFlash = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
My code for turn ON is :
#RequiresApi(api = Build.VERSION_CODES.M)
private void flashLightOn() {
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
} catch (CameraAccessException ignored) {
}
}
Is it possible to turn on front flashlight of device without turning on camera.
Any help will be appreciated.
(Please note that I'm a newcomer to android development)
I have a development scenario where I need to check whether WiFi throttling option is enable or disable?
If it is enable than I want to disable it programmatically.
private fun prepareForWiFiScan() {
Toast.makeText(this, "Wifi scan preparation started", Toast.LENGTH_SHORT).show()
circularProgressbar.visibility = View.VISIBLE
wiFiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (!wiFiManager.isWifiEnabled) {
wiFiManager.isWifiEnabled = true
}
wiFiScanReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(this#MainActivity, "Wifi scan finished", Toast.LENGTH_SHORT).senter code herehow()
val i'sSuccess: Boolean = when {
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M -> intent.getBooleanExtra(
WifiManager.EXTRA_RESULTS_UPDATED,
false
)
else -> intent.getBooleanExtra(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION, false)
}
circularProgressbar.visibility = View.GONE
when (isSuccess) {
true -> scanSuccess()
false -> scanFail()
}
}
}
registerReceiverAndStartScan()
}
private fun registerReceiverAndStartScan() {
val intentFilter = IntentFilter()
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
registerReceiver(wiFiScanReceiver, intentFilter)
val success = wiFiManager.startScan()
if (!success) {
scanFail()
}
}
There are no way present until android 10 where we can check whether WiFi scan throttle is enable or disable. However in android R the new method added to check whether WiFi Scan throttling is enable or disable.
Please refer following link.
https://developer.android.com/reference/android/net/wifi/WifiManager#isScanThrottleEnabled()
Actually you can also check the wifi throttle in Android versions < 10 by using
Settings.Global.getInt(this.getContentResolver(), "wifi_scan_throttle_enabled")
It returns 1 if wifi throttle is enabled, 0 otherwise
You have to use a broadcastReceiver for receiving this. Check here
https://developer.android.com/guide/topics/connectivity/wifi-scan
This works via a root command shell in android 11. Returns false if "Wi-Fi scan throttling" is disabled in developer options. Wish there was an easier way to set this value via a command vs having to set it via multiple presses of the android GUI.
dumpsys wifi | grep wifi_scan_throttle_enabled
When I run my code in Debug from Android Studio my app work good, but when I create the APK and install it on device and start my app, the app get NullPointerException error and get killed( using Android Debug Monitor). Here is the code:
public class MusicService extends IntentService {
boolean INTERNET_CONNECTION = false;
boolean COMMAND_DOWNALOAD_SDCARD = false;
String TABLE_NAME = "";
String COMMAND_ARGUMENT = "";
// Here throw sometimes the error - Line 42
AudioManager audio;
MediaPlayer mp = new MediaPlayer();
long TIME_ELAPSED = 0;
long TIME_STARTED = 0;
boolean ALIVE = true;
long SLEEP_TIME = 5000;
String SERVER_URL = "http://xxx.xxx";
public MusicService() {
super("Service");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("SERVICE", "STARTED");
initialize();
initializeTimer();
while (ALIVE) {
...
}
}
private void initialize() {
// Get phone number set the table name
TABLE_NAME = "t" + getPhoneNumber();
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Wait for internet connection
INTERNET_CONNECTION = waitInternetConnection();
Log.d("INITIALIZE", "COMPLETED");
}
...
}
I start my app when boot is completed. After some times of trying to run it I got error at line 42 and I changed the line from:
AudioManager audio = null;
to
AudioManager audio;
But still get the error but now don't show any more the line.
There is no difference between these: AudioManager audio = null; and AudioManager audio;.
Changing the line from explicitly assigning null to the object doesn't change any functionality at all; all it means is that audio now implicitly defaults to null.
Clearly there is a difference in what getSystemService() does, depending on whether you're in your debugging environment versus your production environment. To find out what's wrong, go into that service. We don't have enough information here to debug it for you.
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
I am using HTML5 audio player inside an android webview. I want to mute the audio and I am using the code like this
this.mute = function()
{
console.debug("Muted");
if(_audioHtml.muted == undefined) {
alert("Doesn't exist");
}
else {
if(_audioHtml.muted === true) {
alert("Muted");
}
else if (_audioHtml.muted === false) {
alert("Non Muted");
}
else {
alert("None of them");
}
}
_audioHtml.muted = true;
};
After each call I can see that the value of muted changes. But its not generating any effect on the device. It always generate the sound in set volume. How can I do this in android. I am using Android 4.0.4.
Thanks.
Assuming an audio tag like <audio id="player" src="something.mp3" />, you should be toggling the muted property of the tag itself using Javascript... something like
$('#player').muted = true;
It works if you set the audio this way: (the html5 tag will be muted)
In your JAVA activity:
wv.loadUrl("file:///android_asset/index.html");
wv.addJavascriptInterface(this, "jInterface");
in your html file:
to mute:
<script>
jInterface.mute();
</script>
now back to your java activity:
public void mute()
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
I haven't found a solution for my MotoX 4.2 device yet, but this tends to work in most cases, basically you have pause the video once it starts playing
The caveat here is that my Samsung 4.3 device is firing "playing" way too early, it should fire when the video actually starts playing, but it fires when the video starts loading
if (sdkVersion < 4.4) {
player.addEventListener("timeupdate", pauseIfPlaying); // check player.currentTime > 0
} else {
player.addEventListener("playing", function () {
player.muted = true;
});
}