Created
April 30, 2014 18:33
-
-
Save basketofsoftkittens/0481d0cb07680a6c1d4b to your computer and use it in GitHub Desktop.
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
{ | |
albumId: "728903889" | |
albumName: "Frozen (Deluxe Edition) [Original Motion Picture Soundtrack]" | |
artistId: "3297504" | |
artistName: "Idina Menzel" | |
currency: "USD" | |
source: "itunes" | |
syncEnabled: true | |
thumbnailUrl: "http://a262.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.170x170-75.jpg" | |
trackBuyUrl: "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2&at=11lpqJ" | |
trackGenre: "Soundtrack" | |
trackId: "728904000" | |
trackLabel: "℗ 2013 Walt Disney Records" | |
trackName: "Let It Go" | |
trackPreviewLength: 30000 | |
trackPreviewUrl: "http://d3okur0sc2jsmx.cloudfront.net/728904000.m4a" | |
trackPrice: "$1.29" | |
} |
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
package com.cheerfulinc.flipagram.view.webview; | |
import java.io.IOException; | |
import android.media.MediaPlayer; | |
import android.webkit.JavascriptInterface; | |
import android.webkit.WebView; | |
/** | |
* An audio player that can be used via JavaScript within | |
* a WebView. | |
*/ | |
public class JavaScriptAudioPlayer | |
extends AbstractJavaScriptInterface | |
implements MediaPlayer.OnPreparedListener, | |
MediaPlayer.OnCompletionListener, | |
MediaPlayer.OnErrorListener { | |
private MediaPlayer player; | |
private boolean startAfterPrepare = false; | |
private int seekAfterPrepare = 0; | |
private boolean isPrepared = false; | |
public static JavaScriptAudioPlayer addInterface(WebView webView, String name) { | |
JavaScriptAudioPlayer ret = new JavaScriptAudioPlayer(webView, name); | |
webView.addJavascriptInterface(ret, name); | |
return ret; | |
} | |
private JavaScriptAudioPlayer(WebView webView, String name) { | |
super(webView, name); | |
init(); | |
} | |
@JavascriptInterface | |
public void setUrl(String url) | |
throws IOException { | |
if (isPlaying()) { | |
player.stop(); | |
} | |
isPrepared = false; | |
startAfterPrepare = false; | |
player.reset(); | |
player.setDataSource(url); | |
execJS(getName() + "_event", "loadstart"); | |
player.prepareAsync(); | |
} | |
@JavascriptInterface | |
public void play() { | |
if (isPrepared) { | |
execJS(getName() + "_event", "play"); | |
player.start(); | |
} else { | |
startAfterPrepare = true; | |
} | |
} | |
@JavascriptInterface | |
public void pause() { | |
if (isPrepared) { | |
player.pause(); | |
execJS(getName() + "_event", "pause"); | |
} else { | |
startAfterPrepare = false; | |
} | |
} | |
@JavascriptInterface | |
public void seek(int milliseconds) { | |
if (isPrepared) { | |
player.seekTo(milliseconds); | |
execJS(getName() + "_event", "pause"); | |
} else { | |
seekAfterPrepare = milliseconds; | |
} | |
} | |
@JavascriptInterface | |
public boolean isPlaying() { | |
return player != null && player.isPlaying(); | |
} | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
execJS(getName() + "_event", "pause"); | |
} | |
@Override | |
public void onPrepared(MediaPlayer mp) { | |
isPrepared = true; | |
execJS(getName() + "_event", "prepared"); | |
player.seekTo(seekAfterPrepare); | |
if (startAfterPrepare) { | |
execJS(getName() + "_event", "start"); | |
player.start(); | |
} | |
} | |
@Override | |
public boolean onError(MediaPlayer mp, int what, int extra) { | |
execJS(getName() + "_event", "error", what, extra); | |
return true; | |
} | |
public void cleanup() { | |
if (player == null) { | |
throw new IllegalStateException(); | |
} | |
if (player.isPlaying()) { | |
player.stop(); | |
} | |
player.release(); | |
player = null; | |
} | |
public void init() { | |
if (player != null) { | |
throw new IllegalStateException(); | |
} | |
player = new MediaPlayer(); | |
player.setOnErrorListener(this); | |
player.setOnPreparedListener(this); | |
player.setOnCompletionListener(this); | |
execJS(getName() + "_event", "created"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment