All Downloads are FREE. Search and download functionalities are using the official Maven repository.

missingOldImpl.LocalAudio Maven / Gradle / Ivy

package berlin.yuna.tinkerforgesensor.model.missing;

import berlin.yuna.tinkerforgesensor.util.AudioControl;
import berlin.yuna.tinkerforgesensor.model.AudioCmd;
import berlin.yuna.tinkerforgesensor.model.exception.NetworkConnectionException;
import berlin.yuna.tinkerforgesensor.model.sensor.Sensor;
import berlin.yuna.tinkerforgesensor.model.AudioDevice;
import com.tinkerforge.Device;
import com.tinkerforge.DummyDevice;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;

import static berlin.yuna.tinkerforgesensor.model.AudioCmd.REPLAY;
import static berlin.yuna.tinkerforgesensor.model.AudioCmd.UNKNOWN;
import static java.lang.String.format;
import static java.util.Arrays.asList;

/**
 * 

{@link LocalAudio}


* Mini audio player plays wav only
* *

Technical Info

*
Play file (allowed = STRING/FILE/PATH/URL/URI)
* sensor.send("/Downloads/mySoundFile.wav"); *
Set volume to 50% (0-100)
* sensor.send(50); *
Mute
* sensor.send(true); *
Play file, volume 20%, unmuted
* sensor.send("/Downloads/mySoundFile.wav", 20, false, PLAY); * *

Audio commands {@link AudioCmd}

* One can be added anytime at sending the commands from above
* sensor.send(PLAY); * sensor.send(REPLAY); * sensor.send(PAUSE); * sensor.send(STOP); * sensor.send(MUTE); * sensor.send(UNMUTE); * *

Parallel sounds

* Can be done by adding different playerIds at the startAsync of the command
* sensor.send(1, "/Downloads/mySoundFile.wav"); * sensor.send(2, "/Downloads/mySoundFile.wav"); */ public class LocalAudio extends Sensor { private final HashMap players = new HashMap<>(); public LocalAudio(final Device device, final String uid) throws NetworkConnectionException { super((DummyDevice) device, uid); } @Override protected Sensor initListener() { return this; } private AudioDevice getPlayer(final int playerId) { if (playerId == -1) { return new AudioControl().getDefaultDevice(); } else if (players.containsKey(playerId)) { return players.get(playerId); } final AudioDevice player = new AudioDevice(); players.put(playerId, player); return player; } @Override public Sensor send(final Object... valuesArray) { List values = asList(valuesArray); try { int playerId = 0; if (values.size() == 1 && values.get(0) instanceof Number) { getPlayer(playerId).setVolume(((Number) values.get(0)).intValue()); } else if (values.size() > 1 && values.get(0) instanceof Number) { playerId = ((Number) values.get(0)).intValue(); values = values.subList(1, values.size()); } final AudioDevice player = getPlayer(playerId); AudioCmd audioCmd = (values.size() == 1 && values.get(0) instanceof URI || values.get(0) instanceof URL || values.get(0) instanceof File || values.get(0) instanceof Path || values.get(0) instanceof String) ? REPLAY : UNKNOWN; for (Object value : values) { if (value instanceof Number) { new AudioControl().getDefaultDevice().setVolume(((Number) value).intValue()); } else if (value instanceof Boolean) { new AudioControl().getDefaultDevice().setMute((Boolean) value); } else if (value instanceof URL) { setAudioFile(playerId, (URL) value); } else if (value instanceof URI) { setAudioFile(playerId, ((URI) value).toURL()); } else if (value instanceof String) { final File file = new File((String) value); if (file.exists()) { setAudioFile(playerId, file.toURI().toURL()); } else { System.err.printf("File not found [%s]%n", file); } } else if (value instanceof File) { final File file = (File) value; if (file.exists()) { setAudioFile(playerId, file.toURI().toURL()); } else { System.err.printf("File not found [%s]%n", file); } } else if (value instanceof Path) { final Path path = (Path) value; if (Files.exists(path)) { setAudioFile(playerId, path.toUri().toURL()); } } else if (value instanceof AudioCmd) { audioCmd = (AudioCmd) value; } } switch (audioCmd) { case PLAY: player.play(); break; case PAUSE: player.pause(); break; case REPLAY: player.replay(); break; case STOP: player.stop(); break; case MUTE: new AudioControl().getDefaultDevice().setMute(true); break; case UNMUTE: new AudioControl().getDefaultDevice().setMute(false); break; } } catch (MalformedURLException ignored) { } return this; } private void setAudioFile(final int playerId, final URL url) { getPlayer(playerId).setSource(url); } @Override public Sensor send(final Object value) { return send(new Object[]{value}); } @Override public Sensor setLedStatus(final Integer value) { return this; } @Override public Sensor ledAdditional(final Integer value) { return this; } @Override public Sensor refreshPeriod(final int milliseconds) { return this; } @Override public Sensor initLedConfig() { return this; } }