Skip to content

Commit

Permalink
Merge pull request #183 from kurtisnelson/mediaplayer
Browse files Browse the repository at this point in the history
Media Play
  • Loading branch information
Brandyn White committed Mar 22, 2014
2 parents 6d92afc + 8fb2aa7 commit ce2689c
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 6 deletions.
2 changes: 2 additions & 0 deletions WearScript/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
android:allowBackup="true"
android:icon="@drawable/launcher"
android:label="@string/app_name">
<activity android:name=".ui.WSActivity"
></activity>
<activity
android:name=".ui.ScriptActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
Expand Down
3 changes: 3 additions & 0 deletions WearScript/src/main/assets/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@ function WearScript() {
return this;
}
}
this.createMedia = function (url, looping) {
WSRAW.mediaLoad(url, looping);
}
this.scriptVersion = function (num) {
WSRAW.scriptVersion(num);
}
Expand Down
12 changes: 6 additions & 6 deletions WearScript/src/main/assets/init.js.min
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ a.msgpack={pack:e,unpack:g,worker:"msgpack.js",upload:function(d,b,g){b.method="
Object.prototype.toString.call(a)},m=String.fromCharCode,D=512;self.importScripts&&(onmessage=function(a){"pack"===a.data.method?postMessage(C(e(a.data.data))):postMessage(g(a.data.data))});(function(){for(var a=0,b;256>a;++a)b=m(a),w[b]=a,t[a]=b;for(a=128;256>a;++a)w[m(63232+a)]=a})();z&&document.write('<script type="text/vbscript">Function vblen(b)vblen=LenB(b.responseBody)End Function\nFunction vbstr(b)vbstr=CStr(b.responseBody)+chr(0)End Function\x3c/script>')}(this);
function WearScript(){this._randstr=function(a){for(var e="",g=0;g<a;g++)e+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".charAt(Math.floor(52*Math.random()));return e};this._cbPrefix="WS"+this._randstr(4);this.callbacks={};this.cbCount=0;this.Cards=function(a){this.cards=a||[];this.isFunc=function(a){return"function"===typeof a};this.isObj=function(a){return"object"===typeof a};this.isUndef=function(a){return"undefined"===typeof a};this.isStr=function(a){return"string"===typeof a};this.addHTML=
function(){var a={card:{type:"html",html:document.getElementById(arguments[0]).innerHTML}},g=Array.prototype.slice.call(arguments).slice(1);return this._add(a,g)};this.add=function(){var a={card:{type:"card",text:arguments[0],info:arguments[1]}},g=Array.prototype.slice.call(arguments).slice(2);return this._add(a,g)};this._add=function(a,g){if(0<g.length&&this.isFunc(g[0])||this.isUndef(g[0]))this.isFunc(g[0])&&(a.selected=WS._funcwrap(g[0])),g=g.slice(1);if(0<g.length&&this.isFunc(g[0])||this.isUndef(g[0]))this.isFunc(g[0])&&
(a.click=WS._funcwrap(g[0])),g=g.slice(1);if(0<g.length&&this.isObj(g[0]))a.children=g[0].cards,g.slice(1);else if(0<g.length&&0==g.length%2){a.menu=[];for(var k=0;k<g.length&&this.isStr(g[k])&&(this.isFunc(g[k+1])||this.isUndef(g[k+1]));k+=2)a.menu.push({label:g[k],callback:WS._funcwrap(g[k+1])})}this.cards.push(a);return this}};this.scriptVersion=function(a){WSRAW.scriptVersion(a)};this.shutdown=function(){WSRAW.shutdown()};this.sensor=function(a){return WSRAW.sensor(a)};this.sensors=function(){return JSON.parse(WSRAW.sensors())};
this._funcwrap=function(a){var e={};return a&&"[object Function]"===e.toString.call(a)?(e=this._cbPrefix+this.cbCount,this.cbCount+=1,window[e]=a,e):a};this._funcfix=function(a){return"string"===typeof a?(this.log("Warning: deprecated use of string as a function: "+a),window[a]):a};this.publish=function(){var a=msgpack.pack(Array.prototype.slice.call(arguments));WSRAW.publish(arguments[0],btoa(a.map(function(a){return String.fromCharCode(a)}).join("")))};this.subscribe=function(a,e){e=this._funcfix(e);
WSRAW.subscribe(a,this._funcwrap(function(a){return e.apply(null,msgpack.unpack(atob(a)))}))};this.sensorOn=function(a,e,g){"string"===typeof a&&(a=this.sensor(a));g?WSRAW.sensorOn(a,e,this._funcwrap(g)):WSRAW.sensorOn(a,e)};this.sensorOff=function(a){WSRAW.sensorOff(a)};this.dataLog=function(a,e,g){WSRAW.dataLog(a,e,g)};this.say=function(a){WSRAW.say(a)};this.qr=function(a){a=this._funcfix(a);WSRAW.qr(this._funcwrap(function(e,g){a(atob(e),g)}))};this.log=function(a){WSRAW.log(a)};this.displayWebView=
function(){WSRAW.displayWebView()};this.displayWarpView=function(a){a?WSRAW.displayWarpView(JSON.stringify(a)):WSRAW.displayWarpView()};this.warpDraw=function(a,e,g,k,n,r){WSRAW.warpDraw(a,e,g,k,n,r)};this.warpGlassToPreviewH=function(a){a=this._funcfix(a);WSRAW.warpGlassToPreviewH(this._funcwrap(function(e){a(JSON.parse(e))}))};this.warpSetOverlay=function(a){WSRAW.warpSetOverlay(a)};this.warpPreviewSampleGlass=function(a){a=a?this._funcwrap(this._funcfix(a)):"";WSRAW.warpPreviewSampleGlass(a)};
this.displayCardTree=function(){WSRAW.displayCardTree()};this.cardTree=function(a){WSRAW.cardTree(JSON.stringify(a.cards))};this.cameraOn=function(a,e,g,k){e||(e=0);g||(maxwidth=0);k?(k=this._funcfix(k),WSRAW.cameraOn(a,e,g,!1,this._funcwrap(k))):WSRAW.cameraOn(a,e,g,!1)};this.cameraPhoto=function(a){a?(a=this._funcfix(a),WSRAW.cameraPhoto(this._funcwrap(a))):WSRAW.cameraPhoto()};this.cameraPhotoData=function(a){a?(a=this._funcfix(a),WSRAW.cameraPhotoData(this._funcwrap(a))):WSRAW.cameraPhoto()};
this.cameraVideo=function(a){a?(a=this._funcfix(a),WSRAW.cameraVideo(this._funcwrap(a))):WSRAW.cameraVideo()};this.cameraOff=function(){WSRAW.cameraOff()};this.activityCreate=function(){WSRAW.activityCreate()};this.activityDestroy=function(){WSRAW.activityDestroy()};this.wifiOn=function(a){a=this._funcfix(a);WSRAW.wifiOn(this._funcwrap(a))};this.wifiOff=function(){WSRAW.wifiOff()};this.wifiScan=function(){WSRAW.wifiScan()};this.serverConnect=function(a,e){e=this._funcfix(e);WSRAW.serverConnect(a,
(a.click=WS._funcwrap(g[0])),g=g.slice(1);if(0<g.length&&this.isObj(g[0]))a.children=g[0].cards,g.slice(1);else if(0<g.length&&0==g.length%2){a.menu=[];for(var k=0;k<g.length&&this.isStr(g[k])&&(this.isFunc(g[k+1])||this.isUndef(g[k+1]));k+=2)a.menu.push({label:g[k],callback:WS._funcwrap(g[k+1])})}this.cards.push(a);return this}};this.createMedia=function(a,e){WSRAW.mediaLoad(a,e)};this.scriptVersion=function(a){WSRAW.scriptVersion(a)};this.shutdown=function(){WSRAW.shutdown()};this.sensor=function(a){return WSRAW.sensor(a)};
this.sensors=function(){return JSON.parse(WSRAW.sensors())};this._funcwrap=function(a){var e={};return a&&"[object Function]"===e.toString.call(a)?(e=this._cbPrefix+this.cbCount,this.cbCount+=1,window[e]=a,e):a};this._funcfix=function(a){return"string"===typeof a?(this.log("Warning: deprecated use of string as a function: "+a),window[a]):a};this.publish=function(){var a=msgpack.pack(Array.prototype.slice.call(arguments));WSRAW.publish(arguments[0],btoa(a.map(function(a){return String.fromCharCode(a)}).join("")))};
this.subscribe=function(a,e){e=this._funcfix(e);WSRAW.subscribe(a,this._funcwrap(function(a){return e.apply(null,msgpack.unpack(atob(a)))}))};this.sensorOn=function(a,e,g){"string"===typeof a&&(a=this.sensor(a));g?WSRAW.sensorOn(a,e,this._funcwrap(g)):WSRAW.sensorOn(a,e)};this.sensorOff=function(a){WSRAW.sensorOff(a)};this.dataLog=function(a,e,g){WSRAW.dataLog(a,e,g)};this.say=function(a){WSRAW.say(a)};this.qr=function(a){a=this._funcfix(a);WSRAW.qr(this._funcwrap(function(e,g){a(atob(e),g)}))};this.log=
function(a){WSRAW.log(a)};this.displayWebView=function(){WSRAW.displayWebView()};this.displayWarpView=function(a){a?WSRAW.displayWarpView(JSON.stringify(a)):WSRAW.displayWarpView()};this.warpDraw=function(a,e,g,k,n,r){WSRAW.warpDraw(a,e,g,k,n,r)};this.warpGlassToPreviewH=function(a){a=this._funcfix(a);WSRAW.warpGlassToPreviewH(this._funcwrap(function(e){a(JSON.parse(e))}))};this.warpSetOverlay=function(a){WSRAW.warpSetOverlay(a)};this.warpPreviewSampleGlass=function(a){a=a?this._funcwrap(this._funcfix(a)):
"";WSRAW.warpPreviewSampleGlass(a)};this.displayCardTree=function(){WSRAW.displayCardTree()};this.cardTree=function(a){WSRAW.cardTree(JSON.stringify(a.cards))};this.cameraOn=function(a,e,g,k){e||(e=0);g||(maxwidth=0);k?(k=this._funcfix(k),WSRAW.cameraOn(a,e,g,!1,this._funcwrap(k))):WSRAW.cameraOn(a,e,g,!1)};this.cameraPhoto=function(a){a?(a=this._funcfix(a),WSRAW.cameraPhoto(this._funcwrap(a))):WSRAW.cameraPhoto()};this.cameraPhotoData=function(a){a?(a=this._funcfix(a),WSRAW.cameraPhotoData(this._funcwrap(a))):
WSRAW.cameraPhoto()};this.cameraVideo=function(a){a?(a=this._funcfix(a),WSRAW.cameraVideo(this._funcwrap(a))):WSRAW.cameraVideo()};this.cameraOff=function(){WSRAW.cameraOff()};this.activityCreate=function(){WSRAW.activityCreate()};this.activityDestroy=function(){WSRAW.activityDestroy()};this.wifiOn=function(a){a=this._funcfix(a);WSRAW.wifiOn(this._funcwrap(a))};this.wifiOff=function(){WSRAW.wifiOff()};this.wifiScan=function(){WSRAW.wifiScan()};this.serverConnect=function(a,e){e=this._funcfix(e);WSRAW.serverConnect(a,
this._funcwrap(e))};this.wake=function(){WSRAW.wake()};this.sound=function(a){WSRAW.sound(a)};this.gestureCallback=function(a,e){e=this._funcfix(e);WSRAW.gestureCallback(a,this._funcwrap(e))};this.speechRecognize=function(a,e){e=this._funcfix(e);WSRAW.speechRecognize(a,this._funcwrap(function(a){e(atob(a))}))};this.liveCardCreate=function(a,e){WSRAW.liveCardCreate(a,e)};this.liveCardDestroy=function(){WSRAW.liveCardDestroy()};this.bluetoothList=function(a){a=this._funcfix(a);WSRAW.bluetoothList(his._funcwrap(function(e){a(JSON.parse(e))}))};
this.bluetoothRead=function(a,e){e=this._funcfix(e);WSRAW.bluetoothRead(a,this._funcwrap(e))};this.bluetoothWrite=function(a,e){WSRAW.bluetoothWrite(a,e)}}WS=new WearScript;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.dappervision.wearscript.events.DataLogEvent;
import com.dappervision.wearscript.events.JsCall;
import com.dappervision.wearscript.events.LambdaEvent;
import com.dappervision.wearscript.events.MediaEvent;
import com.dappervision.wearscript.events.SayEvent;
import com.dappervision.wearscript.events.ScreenEvent;
import com.dappervision.wearscript.events.ScriptEvent;
Expand All @@ -41,7 +42,9 @@
import com.dappervision.wearscript.managers.ManagerManager;
import com.dappervision.wearscript.managers.WarpManager;
import com.dappervision.wearscript.managers.WifiManager;
import com.dappervision.wearscript.ui.MediaPlayerFragment;
import com.dappervision.wearscript.ui.ScriptActivity;
import com.dappervision.wearscript.ui.WSActivity;
import com.google.android.glass.widget.CardScrollView;

import org.msgpack.MessagePack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.dappervision.wearscript.events.GistSyncEvent;
import com.dappervision.wearscript.events.JsCall;
import com.dappervision.wearscript.events.LiveCardEvent;
import com.dappervision.wearscript.events.MediaEvent;
import com.dappervision.wearscript.events.MediaPlayEvent;
import com.dappervision.wearscript.events.SayEvent;
import com.dappervision.wearscript.events.ScreenEvent;
import com.dappervision.wearscript.events.SendEvent;
Expand Down Expand Up @@ -131,6 +133,15 @@ public void sensorOff(int type) {
Utils.eventBusPost(new SensorJSEvent(type, false));
}

@JavascriptInterface
public void mediaLoad(String uri, boolean looping){
try {
Utils.eventBusPost(new MediaEvent(new URI(uri), looping));
} catch (URISyntaxException e) {
// TODO(kurtisnelson): Handle
}
}

@JavascriptInterface
public void serverConnect(String server, String callback) {
Log.i(TAG, "serverConnect: " + server);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dappervision.wearscript.events;

import android.net.Uri;

import java.net.URI;

public class MediaEvent {
private final URI uri;
private final boolean looping;
private final boolean status;

public MediaEvent(URI uri, boolean looping) {
this.uri = uri;
this.looping = looping;
this.status = false;
}

public boolean isLooping() {
return looping;
}

public Uri getUri() {
return android.net.Uri.parse(uri.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dappervision.wearscript.events;

public class MediaPlayEvent {

private final boolean playing;

public MediaPlayEvent(boolean playing) {
this.playing = playing;
}

public boolean isPlaying() {
return playing;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dappervision.wearscript.ui;

import android.support.v4.app.Fragment;

import com.google.android.glass.touchpad.GestureDetector;


public abstract class GestureFragment extends Fragment implements GestureDetector.BaseListener, GestureDetector.ScrollListener {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.dappervision.wearscript.ui;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.dappervision.wearscript.Log;
import com.dappervision.wearscript.R;
import com.dappervision.wearscript.Utils;
import com.dappervision.wearscript.events.MediaPlayEvent;
import com.google.android.glass.touchpad.Gesture;

import java.io.IOException;

public class MediaPlayerFragment extends GestureFragment implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener {
public static final String ARG_URL = "ARG_URL";
public static final String ARG_LOOP = "ARG_LOOP";
private static final String TAG = "MediaPlayerFragment";
private MediaPlayer mp;
private Uri mediaUri;
private SurfaceHolder holder;
private ProgressBar progressBar;
private SurfaceView surfaceView;

public static MediaPlayerFragment newInstance(Uri uri, boolean looping) {
Bundle args = new Bundle();
args.putParcelable(ARG_URL, uri);
args.putBoolean(ARG_LOOP, looping);
MediaPlayerFragment f = new MediaPlayerFragment();
f.setArguments(args);
return f;
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.getEventBus().register(this);
setRetainInstance(true);
mediaUri = getArguments().getParcelable(ARG_URL);
createMediaPlayer();
}

private void createMediaPlayer(){
if(progressBar != null)
progressBar.setVisibility(View.VISIBLE);
mp = new MediaPlayer();
try {
mp.setDataSource(getActivity(), mediaUri);
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnErrorListener(this);
mp.setOnPreparedListener(this);

if (getArguments().getBoolean(ARG_LOOP))
mp.setLooping(true);
mp.prepareAsync();
}

public void onEvent(MediaPlayEvent e) {
if (e.isPlaying()) {
mp.start();
} else {
mp.stop();
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_media_player, container, false);
surfaceView = (SurfaceView) v.findViewById(R.id.media_surface);
progressBar = (ProgressBar) v.findViewById(R.id.video_progressBar);
holder = surfaceView.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {

public void surfaceCreated(SurfaceHolder holder) {
if (mp != null) {
mp.setDisplay(holder);
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
if (mp != null) {
mp.setDisplay(null);
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
}
});

return v;
}

@Override
public void onDestroy() {
super.onDestroy();
if (mp.isPlaying())
mp.stop();
mp.release();
mp = null;
Utils.getEventBus().unregister(this);
}

@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
Log.e(TAG, "MediaPlayer Error: ");
if(i == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
Log.w(TAG, "Server Died");
mediaPlayer.release();
mp = null;
createMediaPlayer();
}else if (i == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
Log.w(TAG, "Unknown Error, resetting");
mediaPlayer.reset();
mediaPlayer.prepareAsync();
}
return false;
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
if(progressBar != null){
progressBar.setVisibility(View.GONE);
}
surfaceView.setVisibility(View.VISIBLE);
mediaPlayer.start();
}

@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
if (mp.isPlaying()) {
mp.pause();
} else {
mp.start();
}
return true;
}
return false;
}

@Override
public boolean onScroll(float v, float v2, float v3) {
if(mp.isPlaying())
return false;
int newPosition = mp.getCurrentPosition() + (int)(v * 10);
if(newPosition < 0)
newPosition = 0;
if(newPosition > mp.getDuration())
newPosition = mp.getDuration() - 5;
mp.seekTo(newPosition);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.dappervision.wearscript.Log;
import com.dappervision.wearscript.Utils;
import com.dappervision.wearscript.events.ActivityResultEvent;
import com.dappervision.wearscript.events.MediaEvent;
import com.dappervision.wearscript.events.ScriptEvent;
import com.dappervision.wearscript.events.StartActivityEvent;
import com.dappervision.wearscript.managers.CameraManager;
Expand Down Expand Up @@ -164,6 +165,14 @@ public void onEvent(StartActivityEvent event) {
startActivityForResult(event.getIntent(), event.getRequestCode());
}

public void onEventMainThread(MediaEvent e){
Intent intent = new Intent(this, WSActivity.class);
intent.putExtra(WSActivity.MODE_KEY, WSActivity.MODE_MEDIA);
intent.putExtra(MediaPlayerFragment.ARG_URL, e.getUri());
intent.putExtra(MediaPlayerFragment.ARG_LOOP, e.isLooping());
startActivity(intent);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.i(TAG, "Request code: " + requestCode + " Result code: " + resultCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.dappervision.wearscript.ui;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.MotionEvent;

import com.dappervision.wearscript.R;
import com.google.android.glass.touchpad.GestureDetector;

public class WSActivity extends FragmentActivity {
public static final String MODE_KEY = "MODE";
public static final String MODE_MEDIA = "MODE_MEDIA";
private GestureDetector gestureDetector;

protected GestureFragment createFragment() {
if (getIntent().getStringExtra(MODE_KEY).equals(MODE_MEDIA)){
return new MediaPlayerFragment().newInstance((Uri) getIntent().getParcelableExtra(MediaPlayerFragment.ARG_URL), getIntent().getBooleanExtra(MediaPlayerFragment.ARG_LOOP, false));
}else{
return null;
}
}

protected int getLayoutResId() {
return R.layout.activity_fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
FragmentManager manager = getSupportFragmentManager();
GestureFragment fragment = (GestureFragment) manager.findFragmentById(R.id.fragmentContainer);

if (fragment == null) {
fragment = createFragment();
manager.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}

gestureDetector = new GestureDetector(this);
gestureDetector.setBaseListener(fragment);
gestureDetector.setScrollListener(fragment);
}

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onMotionEvent(event);
}
return false;
}
}
Loading

0 comments on commit ce2689c

Please sign in to comment.