Index: sky/services/media/src/org/domokit/media/MediaPlayerImpl.java |
diff --git a/sky/services/media/src/org/domokit/media/MediaPlayerImpl.java b/sky/services/media/src/org/domokit/media/MediaPlayerImpl.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6864b0e2617751e7f5fbe824e64efdb2bfb31407 |
--- /dev/null |
+++ b/sky/services/media/src/org/domokit/media/MediaPlayerImpl.java |
@@ -0,0 +1,118 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.domokit.media; |
+ |
+import android.content.ActivityNotFoundException; |
+import android.content.Context; |
+import android.net.Uri; |
+import android.util.Log; |
+ |
+import org.chromium.mojo.common.DataPipeUtils; |
+import org.chromium.mojo.system.Core; |
+import org.chromium.mojo.system.DataPipe; |
+import org.chromium.mojo.system.MojoException; |
+import org.chromium.mojo.system.MojoResult; |
+import org.chromium.mojo.system.ResultAnd; |
+import org.chromium.mojom.media.MediaPlayer; |
+ |
+import java.io.File; |
+import java.io.IOException; |
+import java.util.concurrent.Executor; |
+import java.util.concurrent.Callable; |
+ |
+/** |
+ * Android implementation of MediaPlayer. |
+ */ |
+public class MediaPlayerImpl implements MediaPlayer, android.media.MediaPlayer.OnPreparedListener { |
+ private static final String TAG = "MediaPlayerImpl"; |
+ private final Core mCore; |
+ private final Context mContext; |
+ private final Executor mExecutor; |
+ private final android.media.MediaPlayer mPlayer; |
+ private PrepareResponse mPrepareResponse; |
+ private File mTempFile; |
+ |
+ public MediaPlayerImpl(Core core, Context context, Executor executor) { |
+ mCore = core; |
+ mContext = context; |
+ mExecutor = executor; |
+ mPlayer = new android.media.MediaPlayer(); |
+ mTempFile = null; |
+ mPrepareResponse = null; |
+ } |
+ |
+ @Override |
+ public void close() { |
+ mPlayer.release(); |
+ if (mTempFile != null) |
+ mTempFile.delete(); |
+ } |
+ |
+ @Override |
+ public void onConnectionError(MojoException e) { |
+ Log.e(TAG, "onConnectionError", e); |
+ } |
+ |
+ @Override |
+ public void onPrepared(android.media.MediaPlayer mp) { |
+ Log.e(TAG, "prepare onPrepared"); |
+ assert mPlayer == mp; |
+ assert mPrepareResponse != null; |
+ // ignored argument due to https://github.com/domokit/mojo/issues/282 |
+ boolean ignored = true; |
+ mPrepareResponse.call(ignored); |
+ } |
+ |
+ public void copyComplete() { |
+ Log.e(TAG, "prepare copyComplete"); |
+ try { |
+ mPlayer.setOnPreparedListener(this); |
+ mPlayer.setDataSource(mTempFile.getCanonicalPath()); |
+ mPlayer.prepare(); |
+ } catch (java.io.IOException e) { |
+ Log.e(TAG, "Exception", e); |
+ mPrepareResponse.call(false); |
+ mPrepareResponse = null; |
+ } |
+ } |
+ |
+ @Override |
+ public void prepare(DataPipe.ConsumerHandle consumerHandle, PrepareResponse callback) { |
+ Log.e(TAG, "prepare START"); |
+ File outputDir = mContext.getCacheDir(); |
+ mPrepareResponse = callback; |
+ try { |
+ mTempFile = File.createTempFile("hack", "hack", outputDir); |
+ } catch(IOException e) { |
+ Log.e(TAG, "Failed to create temporary file", e); |
+ callback.call(false); |
+ return; // TODO(eseidel): We should close the pipe? |
+ } |
+ |
+ // TODO(eseidel): I don't understand static methods in java. |
+ DataPipeUtils utils = new DataPipeUtils(); |
+ utils.copyToFile(mCore, consumerHandle, mTempFile, mExecutor, new Runnable() { |
+ @Override |
+ public void run() { |
+ copyComplete(); |
+ } |
+ }); |
+ } |
+ |
+ @Override |
+ public void start() { |
+ mPlayer.start(); |
+ } |
+ |
+ @Override |
+ public void seekTo(int msec) { |
+ mPlayer.seekTo(msec); |
+ } |
+ |
+ @Override |
+ public void pause() { |
+ mPlayer.pause(); |
+ } |
+} |