package de.donmanfred;
import com.devbrackets.android.exomedia.EMVideoView;
import com.devbrackets.android.exomedia.builder.RenderBuilder;
import com.devbrackets.android.exomedia.event.EMMediaProgressEvent;
import com.devbrackets.android.exomedia.listener.EMProgressCallback;
import com.devbrackets.android.exomedia.listener.EMVideoViewControlsCallback;
import com.devbrackets.android.exomedia.listener.ExoPlayerListener;
import com.devbrackets.android.exomedia.util.EMEventBus;
import com.devbrackets.android.exomedia.util.MediaUtil;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common.DesignerCustomView;
import anywheresoftware.b4a.objects.CustomViewWrapper;
import anywheresoftware.b4a.objects.LabelWrapper;
import anywheresoftware.b4a.objects.PanelWrapper;
import anywheresoftware.b4a.objects.ViewWrapper;
import anywheresoftware.b4a.objects.collections.Map;
@SuppressWarnings("deprecation")
@Version(1.00f)
@ShortName("EMVideoView")
@Author(value = "DonManfred (wrapper)")
//@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
//@Events(values={"onSigned(sign As Object)"})
//@DependsOn(values={"android-viewbadger"})
//@DesignerProperties(values = {
// @Property(key="Checked", displayName="Checked", defaultValue="False", fieldType="Boolean")
// })
public class EMVideoViewWrapper extends ViewWrapper<EMVideoView> implements DesignerCustomView{
private BA ba;
private String eventName;
private ExoPlayerListener ExoPlayerL = null;
private MediaPlayer.OnPreparedListener OnPreparedL = null;
private MediaPlayer.OnCompletionListener OnCompletionL = null;
private MediaPlayer.OnErrorListener OnErrorL = null;
private MediaPlayer.OnInfoListener OnInfoL = null;
@Override
public void DesignerCreateView(PanelWrapper base, LabelWrapper lw, Map props) {
//getObject().setChecked((Boolean)props.Get("Checked"));
CustomViewWrapper.replaceBaseWithView(base, getObject());
}
public void Initialize(final BA ba, String EventName) {
_initialize(ba, null, EventName);
}
@Hide
@Override
public void _initialize(final BA ba, Object activityClass, String EventName) {
this.eventName = EventName.toLowerCase(BA.cul);
this.ba = ba;
final EMVideoView _obj = new EMVideoView(ba.context);
final String eventName = EventName.toLowerCase(BA.cul);
setObject(_obj);
OnInfoL = new MediaPlayer.OnInfoListener(){
@Override
public boolean onInfo(MediaPlayer mp, int arg1, int arg2) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_oninfo")) {
BA.Log("lib:Raising.. "+eventName + "_oninfo()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_oninfo", true, new Object[] {mp,arg1,arg2});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_oninfo");
}
return false;
}
};
getObject().setOnInfoListener(OnInfoL);
OnErrorL = new MediaPlayer.OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int arg1, int arg2) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onerror")) {
BA.Log("lib:Raising.. "+eventName + "_onerror()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onerror", true, new Object[] {mp,arg1,arg2});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onerror");
}
return false;
}
};
getObject().setOnErrorListener(OnErrorL);
OnCompletionL = new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_oncompletion")) {
BA.Log("lib:Raising.. "+eventName + "_oncompletion()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_oncompletion", true, new Object[] {mp});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_oncompletion");
}
}
};
getObject().setOnCompletionListener(OnCompletionL);
OnPreparedL = new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onprepared")) {
BA.Log("lib:Raising.. "+eventName + "_onprepared()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onprepared", true, new Object[] {mp});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onprepared");
}
}
};
getObject().setOnPreparedListener(OnPreparedL);
EMVideoViewControlsCallback EMVideoViewControlsC = new EMVideoViewControlsCallback(){
@Override
public boolean onControlsHidden() {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_oncontrolshidden")) {
BA.Log("lib:Raising.. "+eventName + "_oncontrolshidden()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_oncontrolshidden", true, new Object[] {});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_oncontrolshidden");
}
return false;
}
@Override
public boolean onControlsShown() {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_oncontrolsshown")) {
BA.Log("lib:Raising.. "+eventName + "_oncontrolsshown()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_oncontrolsshown", true, new Object[] {});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_oncontrolsshown");
}
return false;
}
@Override
public boolean onNextClicked() {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onnextclicked")) {
BA.Log("lib:Raising.. "+eventName + "_onnextclicked()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onnextclicked", true, new Object[] {});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onnextclicked");
}
return false;
}
@Override
public boolean onPlayPauseClicked() {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onpauseclicked")) {
BA.Log("lib:Raising.. "+eventName + "_onpauseclicked()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onpauseclicked", true, new Object[] {});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onpauseclicked");
}
return false;
}
@Override
public boolean onPreviousClicked() {
if (ba.subExists(eventName + "_onprevioisclicked")) {
BA.Log("lib:Raising.. "+eventName + "_onprevioisclicked()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onprevioisclicked", true, new Object[] {});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onprevioisclicked");
}
// TODO Auto-generated method stub
return false;
}
};
getObject().setVideoViewControlsCallback(EMVideoViewControlsC);
EMProgressCallback progressCallback = new EMProgressCallback() {
@Override
public boolean onProgressUpdated(EMMediaProgressEvent arg0) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onprogressupdated")) {
BA.Log("lib:Raising.. "+eventName + "_onprogressupdated()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onprogressupdated", true, new Object[] {arg0});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onprogressupdated");
}
return false;
}
};
getObject().setProgressCallback(progressCallback);
innerInitialize(ba, eventName, true);
}
public void setReleaseOnDetachFromWindow(boolean releaseOnDetach){
getObject().setReleaseOnDetachFromWindow(releaseOnDetach);
}
public void release(){
getObject().release();
}
public String getUserAgent(){
return getObject().getUserAgent();
}
public void setPreviewImageDrawable(@Nullable Drawable drawable){
getObject().setPreviewImage(drawable);
}
public void setPreviewImageResource(String DrawableName){
int resourceId=BA.applicationContext.getResources().getIdentifier(DrawableName, "drawable", BA.packageName);
getObject().setPreviewImage(resourceId);
}
public void setPreviewImageBitmap(@Nullable Bitmap bitmap) {
getObject().setPreviewImage(bitmap);
}
public void setPreviewImageUri(@Nullable Uri uri) {
getObject().setPreviewImage(uri);
}
public ImageView getPreviewImageView(){
return getObject().getPreviewImageView();
}
public void setShutterColor(int color){
getObject().setShutterColor(color);
}
public void setProgressPollDelay(int milliSeconds){
getObject().setProgressPollDelay(milliSeconds);
}
public void setBus(@Nullable EMEventBus bus){
getObject().setBus(bus);
}
public void startProgressPoll(@Nullable EMEventBus bus){
}
public void startProgressPoll(EMProgressCallback callback){
}
public void startProgressPoll(){
getObject().startProgressPoll();
}
public void stopProgressPoll(){
getObject().stopProgressPoll();
}
public void setDefaultControlsEnabled(boolean enabled){
}
public void showDefaultControls(){
}
public void setPreviousButtonEnabled(boolean enabled){
}
public void setNextButtonEnabled(boolean enabled){
}
public void setRewindButtonEnabled(boolean enabled){
}
public void setFastForwardButtonEnabled(boolean enabled){
}
public void setPlayPauseImages(@DrawableRes int playResourceId, @DrawableRes int pauseResourceId){
}
public void setPreviousImageResource(@DrawableRes int resourceId){
}
public void setNextImageResource(@DrawableRes int resourceId){
}
public void setRewindImageResource(@DrawableRes int resourceId){
}
public void setFastForwardImageResource(@DrawableRes int resourceId){
}
public void setPreviousButtonRemoved(boolean removed){
getObject().setPreviousButtonRemoved(removed);
}
public void setNextButtonRemoved(boolean removed){
getObject().setNextButtonRemoved(removed);
}
public void setRewindButtonRemoved(boolean removed){
getObject().setRewindButtonRemoved(removed);
}
public void setFastForwardButtonRemoved(boolean removed){
getObject().setFastForwardButtonRemoved(removed);
}
public void setVideoURI(Uri uri){
getObject().setVideoURI(uri);
}
public void setVideoURI2(Uri uri, MediaUtil.MediaType defaultMediaType){
getObject().setVideoURI(uri, defaultMediaType);
}
public void setVideoURI3(Uri uri, RenderBuilder renderBuilder){
getObject().setVideoURI(uri, renderBuilder);
}
public void setVideoPath(String path){
getObject().setVideoPath(path);
}
@Nullable
public Uri getVideoUri(){
return getObject().getVideoUri();
}
public boolean setVolume(float volume){
return getObject().setVolume(volume);
}
public void reset(){
getObject().reset();
}
public void seekTo(int milliSeconds){
getObject().seekTo(milliSeconds);
}
public boolean isPlaying(){
return getObject().isPlaying();
}
public void start() {
getObject().start();
}
public void pause(){
getObject().pause();
}
public void stopPlayback(){
getObject().stopPlayback();
}
public void suspend(){
getObject().suspend();
}
public long getDuration(){
return getObject().getDuration();
}
public void overrideDuration(int duration){
getObject().overrideDuration(duration);
}
public long getCurrentPosition(){
return getObject().getCurrentPosition();
}
public void setPositionOffset(int offset){
getObject().setPositionOffset(offset);
}
public void restartOverridePosition(){
getObject().restartOverridePosition();
}
public void overridePosition(boolean override){
getObject().overridePosition(override);
}
public int getBufferPercentage(){
return getObject().getBufferPercentage();
}
public void addExoPlayerListener(){
ExoPlayerL = new ExoPlayerListener(){
@Override
public void onError(Exception e) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onerror")) {
BA.Log("lib:Raising.. "+eventName + "_onerror()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onerror", true, new Object[] {e.getMessage()});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onerror");
}
}
@Override
public void onStateChanged(boolean arg0, int arg1) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onstatechange")) {
BA.Log("lib:Raising.. "+eventName + "_onstatechange()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onstatechange", true, new Object[] {arg0,arg1});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onstatechange");
}
}
@Override
public void onVideoSizeChanged(int arg0, int arg1, int arg2, float arg3) {
// TODO Auto-generated method stub
if (ba.subExists(eventName + "_onvideosizechanged")) {
BA.Log("lib:Raising.. "+eventName + "_onvideosizechanged()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onvideosizechanged", true, new Object[] {arg0,arg1,arg2,arg3});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onvideosizechanged");
}
}
};
getObject().addExoPlayerListener(ExoPlayerL);
}
public void removeExoPlayerListener(){
getObject().removeExoPlayerListener(ExoPlayerL);
ExoPlayerL = null;
}
}