-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from kurtisnelson/mediaplayer
Media Play
- Loading branch information
Showing
12 changed files
with
316 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
WearScript/src/main/java/com/dappervision/wearscript/events/MediaEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
WearScript/src/main/java/com/dappervision/wearscript/events/MediaPlayEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
WearScript/src/main/java/com/dappervision/wearscript/ui/GestureFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
161 changes: 161 additions & 0 deletions
161
WearScript/src/main/java/com/dappervision/wearscript/ui/MediaPlayerFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
WearScript/src/main/java/com/dappervision/wearscript/ui/WSActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.