use of com.codename1.media.Media in project CodenameOne by codenameone.
the class AndroidImplementation method createMedia.
/**
* @inheritDoc
*/
@Override
public Media createMedia(final String uri, boolean isVideo, final Runnable onCompletion) throws IOException {
if (getActivity() == null) {
return null;
}
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")) {
return null;
}
if (uri.startsWith("file://")) {
return createMedia(removeFilePrefix(uri), isVideo, onCompletion);
}
File file = null;
if (uri.indexOf(':') < 0) {
// use a file object to play to try and workaround this issue:
// http://code.google.com/p/android/issues/detail?id=4124
file = new File(uri);
}
Media retVal;
if (isVideo) {
final AndroidImplementation.Video[] video = new AndroidImplementation.Video[1];
final boolean[] flag = new boolean[1];
final File f = file;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
VideoView v = new VideoView(getActivity());
v.setZOrderMediaOverlay(true);
if (f != null) {
v.setVideoURI(Uri.fromFile(f));
} else {
v.setVideoURI(Uri.parse(uri));
}
video[0] = new AndroidImplementation.Video(v, getActivity(), onCompletion);
flag[0] = true;
synchronized (flag) {
flag.notify();
}
}
});
while (!flag[0]) {
synchronized (flag) {
try {
flag.wait(100);
} catch (InterruptedException ex) {
}
}
}
return video[0];
} else {
MediaPlayer player;
if (file != null) {
FileInputStream is = new FileInputStream(file);
player = new MediaPlayer();
player.setDataSource(is.getFD());
player.prepare();
} else {
player = MediaPlayer.create(getActivity(), Uri.parse(uri));
}
retVal = new Audio(getActivity(), player, null, onCompletion);
}
return retVal;
}
use of com.codename1.media.Media in project CodenameOne by codenameone.
the class AndroidImplementation method createMedia.
/**
* @inheritDoc
*/
@Override
public Media createMedia(InputStream stream, String mimeType, final Runnable onCompletion) throws IOException {
if (getActivity() == null) {
return null;
}
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")) {
return null;
}
boolean isVideo = mimeType.contains("video");
if (!isVideo && stream instanceof FileInputStream) {
MediaPlayer player = new MediaPlayer();
player.setDataSource(((FileInputStream) stream).getFD());
player.prepare();
return new Audio(getActivity(), player, stream, onCompletion);
}
String extension = MimeTypeMap.getFileExtensionFromUrl(mimeType);
final File temp = File.createTempFile("mtmp", extension == null ? "dat" : extension);
temp.deleteOnExit();
OutputStream out = createFileOuputStream(temp);
byte[] buf = new byte[256];
int len = 0;
while ((len = stream.read(buf, 0, buf.length)) > -1) {
out.write(buf, 0, len);
}
out.close();
stream.close();
final Runnable finish = new Runnable() {
@Override
public void run() {
if (onCompletion != null) {
Display.getInstance().callSerially(onCompletion);
// makes sure the file is only deleted after the onCompletion was invoked
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
temp.delete();
}
});
return;
}
temp.delete();
}
};
if (isVideo) {
final AndroidImplementation.Video[] retVal = new AndroidImplementation.Video[1];
final boolean[] flag = new boolean[1];
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
VideoView v = new VideoView(getActivity());
v.setZOrderMediaOverlay(true);
v.setVideoURI(Uri.fromFile(temp));
retVal[0] = new AndroidImplementation.Video(v, getActivity(), finish);
flag[0] = true;
synchronized (flag) {
flag.notify();
}
}
});
while (!flag[0]) {
synchronized (flag) {
try {
flag.wait(100);
} catch (InterruptedException ex) {
}
}
}
return retVal[0];
} else {
return createMedia(createFileInputStream(temp), mimeType, finish);
}
}
use of com.codename1.media.Media in project CodenameOne by codenameone.
the class AndroidImplementation method createBackgroundMedia.
@Override
public Media createBackgroundMedia(final String uri) throws IOException {
int mediaId = nextMediaId++;
Intent serviceIntent = new Intent(getContext(), AudioService.class);
serviceIntent.putExtra("mediaLink", uri);
serviceIntent.putExtra("mediaId", mediaId);
final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
background = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
AudioService.LocalBinder mLocalBinder = (AudioService.LocalBinder) service;
AudioService svc = (AudioService) mLocalBinder.getService();
background = svc;
}
};
boolean boundSuccess = getContext().bindService(serviceIntent, mConnection, getContext().BIND_AUTO_CREATE);
if (!boundSuccess) {
throw new RuntimeException("Failed to bind background media service for uri " + uri);
}
getContext().startService(serviceIntent);
while (background == null) {
Display.getInstance().invokeAndBlock(new Runnable() {
@Override
public void run() {
Util.sleep(200);
}
});
}
while (background.getMedia(mediaId) == null) {
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
Util.sleep(200);
}
});
}
Media ret = new MediaProxy(background.getMedia(mediaId)) {
@Override
public void play() {
super.play();
}
@Override
public void cleanup() {
super.cleanup();
getContext().unbindService(mConnection);
}
};
return ret;
}
use of com.codename1.media.Media in project CodenameOne by codenameone.
the class IOSImplementation method captureAudio.
public void captureAudio(ActionListener response) {
if (!nativeInstance.checkMicrophoneUsage()) {
throw new RuntimeException("Please add the ios.NSMicrophoneUsageDescription build hint");
}
dropEvents = false;
String p = FileSystemStorage.getInstance().getAppHomePath();
if (!p.endsWith("/")) {
p += "/";
}
try {
final Media media = MediaManager.createMediaRecorder(p + "cn1TempAudioFile", MediaManager.getAvailableRecordingMimeTypes()[0]);
media.play();
boolean b = Dialog.show("Recording", "", "Save", "Cancel");
final Dialog d = new Dialog("Recording");
media.pause();
media.cleanup();
d.dispose();
if (b) {
response.actionPerformed(new ActionEvent(p + "cn1TempAudioFile"));
} else {
FileSystemStorage.getInstance().delete(p + "cn1TempAudioFile");
response.actionPerformed(null);
}
} catch (IOException err) {
err.printStackTrace();
response.actionPerformed(null);
}
}
use of com.codename1.media.Media in project CodenameOne by codenameone.
the class CodenameOneImplementation method createBackgroundMedia.
/**
* Creates an audio media that can be played in the background.
*
* @param uri the uri of the media can start with jar://, file://, http://
* (can also use rtsp:// if supported on the platform)
*
* @return Media a Media Object that can be used to control the playback
* of the media
*
* @throws IOException if creation of media from the given URI has failed
*/
public Media createBackgroundMedia(String uri) throws IOException {
if (uri.startsWith("jar://")) {
uri = uri.substring(6);
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
InputStream is = getResourceAsStream(this.getClass(), uri);
String mime = "";
if (uri.endsWith(".mp3")) {
mime = "audio/mp3";
} else if (uri.endsWith(".wav")) {
mime = "audio/x-wav";
} else if (uri.endsWith(".amr")) {
mime = "audio/amr";
} else if (uri.endsWith(".3gp")) {
mime = "audio/3gpp";
}
return createMedia(is, mime, null);
}
return createMedia(uri, false, null);
}
Aggregations