I need add mirrored video midea player in Unity plugin from Android project.
So , I use TextureView to show my mp4 file , and I have build Android native project success work in my android device.
But , when I build a jar file import to my unity project , run the application will crash.
This is my Android code “mirrored video.class” :
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.Surface;
import android.view.TextureView;
public class mirroredvideo extends Activity implements TextureView.SurfaceTextureListener{
boolean isMirrored = true;
public CustomMediaPlayer customMediaPlayer;
public String video_url = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
int layoutID = getResources().getIdentifier("activity_mirroredvideo", "layout", getPackageName());
int textureViewID = getResources().getIdentifier("mirrored_textureView", "id", getPackageName());
setContentView(layoutID);
//video_url = this.getIntent().getStringExtra("VURL"); // your URL here
video_url = "http://devstreaming.apple.com/videos/wwdc/2015/217wu453thu1r1/217/217_hd_adopting_new_trackpad_features.mp4";
customMediaPlayer = new CustomMediaPlayer ();
TextureView textureView = (TextureView)findViewById(textureViewID);
textureView.setSurfaceTextureListener(this);
textureView.setScaleX(isMirrored ? -1 : 1);
}catch (Error e)
{
Log.v("mirroredvideo","catch error : "+e);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
customMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
customMediaPlayer.setSurface(new Surface(surface));
customMediaPlayer.setDataSource(video_url);
customMediaPlayer.prepare(); // might take long! (for buffering, etc)
customMediaPlayer.start();
if(customMediaPlayer.isPlaying())
{
Log.v("customMediaPlayer:","customMediaPlayer really start..");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
protected void onPause() {
super.onPause();
customMediaPlayer.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
customMediaPlayer.stop();
}
@Override
protected void onRestart() {
super.onRestart();
customMediaPlayer.start();
}
@Override
protected void onResume() {
if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}
}
And I create a class “CustomMediaPlayer.class” :
import android.media.MediaPlayer;
import java.io.IOException;
/**
* Created by playar on 2016/2/24.
*/
class CustomMediaPlayer extends MediaPlayer
{
String dataSource;
@Override
public void setDataSource(String path) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException
{
// TODO Auto-generated method stub
super.setDataSource(path);
dataSource = path;
}
public String getDataSource()
{
return dataSource;
}
}
Then , this is my request and response with Unity class “requestUnity.class” :
public class requestUnity extends UnityPlayerActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void videomirrored(Activity gactivity) {
Intent intent = new Intent(gactivity, mirroredvideo.class);
gactivity.startActivity(intent);
}
}
Finally , this is my unity C# script code :
using (AndroidJavaClass androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using(AndroidJavaObject jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity"))
{
using(AndroidJavaClass jc = new AndroidJavaClass("com.xxx.xxx.requestUnity"))
{
jc.CallStatic("videomirrored", jo);
}
}
}
There is my crash log when I run this application :
[65296-螢幕快照-2016-03-06-上午103722.png|65296]
And I have find the application can’t run to onSurfaceTextureAvailable function! (If in the native code project , it’s nice work for me.)
Did anyone can help me this problem ?