Skip to content

Instantly share code, notes, and snippets.

@bgmf
Created February 10, 2017 13:00
Show Gist options
  • Save bgmf/d87a2bac0a5623f359637a3da334f980 to your computer and use it in GitHub Desktop.
Save bgmf/d87a2bac0a5623f359637a3da334f980 to your computer and use it in GitHub Desktop.
JavaFXPorts Audio/Video Examples with RoboVM (no native code needed)
package my.application;
public class Constants {
public static final String OBJECTS_BASE_PATH = "objects";
// and more
protected Constants() {
// hide the constructor
}
}
package my.application;
import javafx.beans.property.ReadOnlyObjectProperty;
public interface NativeAudioService {
void init(String filename) throws NativeServiceException;
void play() throws NativeServiceException;
void pause() throws NativeServiceException;
void resume() throws NativeServiceException;
void stop() throws NativeServiceException;
ReadOnlyObjectProperty<Status> statusProperty();
Status getStatus();
public enum Status {
PLAY, PAUSE, STOP, ERROR
}
}
package my.application;
import ch.cnlab.disentis.util.Constants;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import org.robovm.apple.avfoundation.AVAudioPlayer;
import org.robovm.apple.foundation.NSErrorException;
import org.robovm.apple.foundation.NSURL;
import org.robovm.apple.foundation.NSURLScheme;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NativeAudioServiceIOS extends PathHelperIOS implements NativeAudioService {
private static final Logger LOG = Logger.getLogger(NativeAudioServiceIOS.class.getName());
private static final String DIR_NAME = Constants.OBJECTS_BASE_PATH;
private final ReadOnlyObjectWrapper<Status> status = new ReadOnlyObjectWrapper<>(this, "status", Status.STOP);
private String filename = null;
private AVAudioPlayer player = null;
public NativeAudioServiceIOS() {
super();
}
@Override
public void init(String filename) throws NativeServiceException {
this.filename = filename.startsWith("/") ? filename.substring(1) : filename;
LOG.warning("Called with file: " + filename);
status.set(Status.STOP);
try {
if(!filename.startsWith("/")) filename = "/" + filename;
File fullfile = new File(pathBase.getAbsolutePath() + filename);
if(fullfile.exists()) {
NSURL fullurl = new NSURL(NSURLScheme.File, "", fullfile.getAbsolutePath());
LOG.log(Level.SEVERE, "Loading URL: " + fullurl);
// Create audio player object and initialize with URL to sound
player = new AVAudioPlayer(fullurl);
LOG.log(Level.SEVERE, "Player initialized: " + player);
status.set(Status.STOP);
} else {
LOG.log(Level.WARNING, String.format("Audiofile doesn't exist: %s (%s / %s)",
fullfile.getAbsolutePath(),
pathBase.getAbsolutePath(),
filename));
player = null;
status.set(Status.ERROR);
}
} catch(NSErrorException error) {
LOG.log(Level.SEVERE, "Audio Setup Failed: " + error.toString(), error);
status.set(Status.ERROR);
}
}
@Override
public void play() throws NativeServiceException {
if(player == null) return;
player.play();
status.set(Status.PLAY);
}
@Override
public void pause() throws NativeServiceException {
if(player == null) return;
player.pause();
status.set(Status.PAUSE);
}
@Override
public void resume() throws NativeServiceException {
if(player == null) return;
player.play();
status.set(Status.PLAY);
}
@Override
public void stop() throws NativeServiceException {
if(player == null) return;
player.stop();
player.setCurrentTime(0.0);
status.set(Status.STOP);
}
@Override
public ReadOnlyObjectProperty<Status> statusProperty() {
return status.getReadOnlyProperty();
}
@Override
public Status getStatus() {
return status.get();
}
}
package my.application;
public interface NativeVideoService {
void triggerPlatformApp(String filename);
}
package ch.cnlab.disentis.platform;
import ch.cnlab.disentis.util.Constants;
import org.robovm.apple.foundation.*;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIDocumentInteractionController;
import org.robovm.apple.uikit.UIDocumentInteractionControllerDelegateAdapter;
import org.robovm.apple.uikit.UIViewController;
import java.io.File;
import java.util.logging.Logger;
public class NativeVideoServiceIOS extends PathHelperIOS implements NativeVideoService {
private static final Logger LOG = Logger.getLogger(NativeVideoServiceIOS.class.getName());
public NativeVideoServiceIOS() {
LOG.warning("Initialized Native Video Service with path: " + this.pathBase);
}
@Override
public void triggerPlatformApp(String filename) {
String fullfile = pathBase.getAbsolutePath() + filename;
NSURL url = new NSURL(NSURLScheme.File, "", fullfile);
UIDocumentInteractionController popup = new UIDocumentInteractionController(url);
popup.setDelegate(new UIDocumentInteractionControllerDelegateAdapter() {
@Override
public UIViewController getViewControllerForPreview(UIDocumentInteractionController controller) {
return UIApplication.getSharedApplication()
.getWindows().first().getRootViewController();
}
});
popup.presentPreview(true);
}
}
package my.application;
import my.application.Constants;
import org.robovm.apple.foundation.*;
import java.io.File;
import java.util.logging.Logger;
public class PathHelperIOS {
private static final Logger LOG = Logger.getLogger(PathHelperIOS.class.getName());
protected static final String DIR_NAME = "gluon/" + Constants.OBJECTS_BASE_PATH;
protected File pathObjects = null;
protected File pathBase = null;
public PathHelperIOS() {
NSArray<NSURL> paths = NSFileManager.getDefaultManager()
.getURLsForDirectory(NSSearchPathDirectory.LibraryDirectory,
NSSearchPathDomainMask.UserDomainMask);
if (paths.size() > 0) {
NSURL url = paths.first();
NSURL path = url.newURLByAppendingPathComponent(DIR_NAME);
this.pathBase = new File(url.getPath() + "/gluon/");
this.pathObjects = new File(path.getPath());
if(!this.pathObjects.exists()) {
boolean ok = this.pathObjects.mkdirs();
LOG.warning("Creating " + DIR_NAME + " storage -> " + ok);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment