Try to fix sound pools

This commit is contained in:
Vitaliy 2020-10-29 14:35:26 +02:00
parent 1a9f9229cf
commit a8560b8667
2 changed files with 75 additions and 35 deletions

View file

@ -44,6 +44,7 @@ import net.osmand.IProgress;
import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.StateChangedListener;
import net.osmand.data.DataTileManager;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
@ -158,7 +159,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
public static final int AV_CAMERA_FOCUS_CONTINUOUS = 5;
// photo shot:
private static int shotId = 0;
private SoundPool sp = null;
private SoundPool soundPool = null;
public static final int FULL_SCEEN_RESULT_DELAY_MS = 3000;
public final CommonPreference<Integer> AV_CAMERA_PICTURE_SIZE;
@ -596,6 +597,17 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
@Override
public boolean init(@NonNull final OsmandApplication app, Activity activity) {
if (AV_PHOTO_PLAY_SOUND.get()) {
loadCameraSound();
}
AV_PHOTO_PLAY_SOUND.addListener(new StateChangedListener<Boolean>() {
@Override
public void stateChanged(Boolean change) {
if (AV_PHOTO_PLAY_SOUND.get() && soundPool == null) {
loadCameraSound();
}
}
});
// initializeRemoteControlRegistrationMethods();
// AudioManager am = (AudioManager) app.getSystemService(Context.AUDIO_SERVICE);
// if (am != null) {
@ -604,6 +616,21 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
return true;
}
private void loadCameraSound() {
if (soundPool == null) {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}
if (shotId == 0) {
try {
AssetFileDescriptor assetFileDescriptor = app.getAssets().openFd("sounds/camera_click.ogg");
shotId = soundPool.load(assetFileDescriptor, 1);
assetFileDescriptor.close();
} catch (Exception e) {
log.error("cannot get shotId for sounds/camera_click.ogg");
}
}
}
@Override
public void registerLayers(MapActivity activity) {
this.mapActivity = activity;
@ -1338,21 +1365,6 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
// load sound befor shot
if (AV_PHOTO_PLAY_SOUND.get()) {
if (sp == null)
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
if (shotId == 0) {
try {
AssetFileDescriptor assetFileDescriptor = app.getAssets().openFd("sounds/camera_click.ogg");
shotId = sp.load(assetFileDescriptor, 1);
assetFileDescriptor.close();
} catch (Exception e) {
log.error("cannot get shotId for sounds/camera_click.ogg");
}
}
}
Parameters parameters = cam.getParameters();
parameters.setPictureSize(selectedCamPicSize.width, selectedCamPicSize.height);
log.debug("takePhotoWithCamera() set Picture size: width=" + selectedCamPicSize.width
@ -1734,6 +1746,11 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
@Override
public void disable(OsmandApplication app) {
if (soundPool != null) {
soundPool.release();
soundPool = null;
shotId = 0;
}
// AudioManager am = (AudioManager) app.getSystemService(Context.AUDIO_SERVICE);
// if (am != null) {
// unregisterMediaListener(am);
@ -2043,8 +2060,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
photoJpegData = data;
if (AV_PHOTO_PLAY_SOUND.get()) {
if (sp != null && shotId != 0) {
sp.play(shotId, 0.7f, 0.7f, 0, 0, 1);
if (soundPool != null && shotId != 0) {
soundPool.play(shotId, 0.7f, 0.7f, 0, 0, 1);
}
}

View file

@ -1,20 +1,22 @@
package net.osmand.plus.routing;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import net.osmand.Location;
import net.osmand.StateChangedListener;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.PointDescription;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.helpers.WaypointHelper.LocationPointWrapper;
import net.osmand.plus.routing.AlarmInfo.AlarmInfoType;
import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo;
import net.osmand.plus.routing.data.StreetName;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.voice.AbstractPrologCommandPlayer;
import net.osmand.plus.voice.CommandBuilder;
import net.osmand.plus.voice.CommandPlayer;
@ -80,7 +82,10 @@ public class VoiceRouter {
private int TURN_IN_DISTANCE;
private int TURN_IN_DISTANCE_END;
private int TURN_NOW_DISTANCE;
private SoundPool soundPool;
private int soundClick = -1;
private VoiceCommandPending pendingCommand = null;
private RouteDirectionInfo nextRouteDirection;
@ -103,8 +108,36 @@ public class VoiceRouter {
}
};
app.getAppCustomization().addListener(customizationListener);
if (!isMute()) {
loadCameraSound();
}
settings.VOICE_MUTE.addListener(new StateChangedListener<Boolean>() {
@Override
public void stateChanged(Boolean change) {
if (!isMute() && soundPool == null) {
loadCameraSound();
}
}
});
}
private void loadCameraSound() {
if (soundPool == null) {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}
if (soundClick == -1) {
try {
// Taken unaltered from https://freesound.org/people/Corsica_S/sounds/91926/ under license http://creativecommons.org/licenses/by/3.0/ :
AssetFileDescriptor assetFileDescriptor = app.getAssets().openFd("sounds/ding.ogg");
soundClick = soundPool.load(assetFileDescriptor, 1);
assetFileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void setPlayer(CommandPlayer player) {
this.player = player;
if (pendingCommand != null && player != null) {
@ -1015,18 +1048,8 @@ public class VoiceRouter {
if (isMute()) {
return;
}
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
int soundClick = -1;
boolean success = true;
try {
// Taken unaltered from https://freesound.org/people/Corsica_S/sounds/91926/ under license http://creativecommons.org/licenses/by/3.0/ :
soundClick = sp.load(settings.getContext().getAssets().openFd("sounds/ding.ogg"), 1);
} catch (IOException e) {
e.printStackTrace();
success = false;
}
if (success) {
sp.play(soundClick, 1 ,1, 0, 0, 1);
if (soundPool != null && soundClick != -1) {
soundPool.play(soundClick, 1, 1, 0, 0, 1);
}
}