Skip to content

Instantly share code, notes, and snippets.

@niusounds
Created December 26, 2016 05:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niusounds/5f37056938aa691b89fcb65e37ac7d10 to your computer and use it in GitHub Desktop.
Save niusounds/5f37056938aa691b89fcb65e37ac7d10 to your computer and use it in GitHub Desktop.
Using Rajawali (and AndroidAnnotations) to play 360 video.
import android.app.Activity;
import android.media.MediaPlayer;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.rajawali3d.view.SurfaceView;
import java.io.IOException;
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@ViewById
SurfaceView rajawaliView;
@Bean
MyRenderer renderer;
@AfterViews
void init() {
rajawaliView.setSurfaceRenderer(renderer);
}
MediaPlayer mediaPlayer;
@Override
protected void onResume() {
super.onResume();
mediaPlayer = new MediaPlayer();
// This is just a simple example.
// I recommend to load resource in background thread in production.
try {
mediaPlayer.setDataSource("/sdcard/video.mp4"); // video file path
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer.release();
throw new RuntimeException(e);
}
}
@Override
protected void onPause() {
super.onPause();
mediaPlayer.release();
mediaPlayer = null;
}
}
import android.content.Context;
import android.view.MotionEvent;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.renderer.Renderer;
@EBean
class MyRenderer extends org.rajawali3d.renderer.Renderer {
@RootContext
MainActivity activity;
MyRenderer(Context context) {
super(context);
}
@Override
protected void initScene() {
VRSphere vrSphere = new VRSphere();
getCurrentScene().addChild(vrSphere);
// Start playing (I put this to simple description but I recommend that you should put this to right place.)
try {
vrSphere.bindMediaPlayer(activity.mediaPlayer);
activity.mediaPlayer.start();
} catch (ATexture.TextureException e) {
throw new RuntimeException(e);
}
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
}
@Override
public void onTouchEvent(MotionEvent event) {
}
}
import android.media.MediaPlayer;
import org.rajawali3d.cameras.Camera;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.materials.textures.StreamingTexture;
import org.rajawali3d.math.Matrix4;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Sphere;
public class VRSphere extends Sphere {
private StreamingTexture streamingTexture;
public VRSphere() {
super(100, 128, 64);
setRotation(Vector3.Axis.Y, -90);
setScale(1, 1, -1);
setColor(0);
setMaterial(new Material());
}
public void bindMediaPlayer(MediaPlayer mp) throws ATexture.TextureException {
if (streamingTexture != null) {
streamingTexture.updateMediaPlayer(mp);
} else {
streamingTexture = new StreamingTexture("video", mp);
getMaterial().addTexture(streamingTexture);
}
}
@Override
public void render(Camera camera, Matrix4 vpMatrix, Matrix4 projMatrix, Matrix4 vMatrix, Matrix4 parentMatrix, Material sceneMaterial) {
if (streamingTexture != null) {
streamingTexture.update();
}
super.render(camera, vpMatrix, projMatrix, vMatrix, parentMatrix, sceneMaterial);
}
}
@strength0179
Copy link

I don't like "org.androidannotations.annotations." But you still save my day.... No. you save my year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment