|
| 1 | +package com.asha.md360player4android; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.graphics.SurfaceTexture; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.support.annotation.Nullable; |
| 10 | +import android.view.Surface; |
| 11 | +import android.view.TextureView; |
| 12 | +import android.view.View; |
| 13 | +import android.view.Window; |
| 14 | +import android.view.WindowManager; |
| 15 | + |
| 16 | +import tv.danmaku.ijk.media.player.IMediaPlayer; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by hzqiujiadi on 16/7/6. |
| 21 | + |
| 22 | + */ |
| 23 | +public class IjkPlayerDemoActivity extends Activity implements TextureView.SurfaceTextureListener { |
| 24 | + |
| 25 | + private Surface surface; |
| 26 | + |
| 27 | + private MediaPlayerWrapper mMediaPlayerWrapper = new MediaPlayerWrapper(); |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 31 | + super.onCreate(savedInstanceState); |
| 32 | + // no title |
| 33 | + requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 34 | + |
| 35 | + // full screen |
| 36 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 37 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 38 | + |
| 39 | + setContentView(R.layout.activity_ijkdemo); |
| 40 | + |
| 41 | + mMediaPlayerWrapper.init(); |
| 42 | + mMediaPlayerWrapper.setPreparedListener(new IMediaPlayer.OnPreparedListener() { |
| 43 | + @Override |
| 44 | + public void onPrepared(IMediaPlayer mp) { |
| 45 | + cancelBusy(); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + TextureView textureView = (TextureView) findViewById(R.id.video_view); |
| 50 | + textureView.setSurfaceTextureListener(this); |
| 51 | + |
| 52 | + Uri uri = getUri(); |
| 53 | + if (uri != null){ |
| 54 | + mMediaPlayerWrapper.openRemoteFile(uri.toString()); |
| 55 | + mMediaPlayerWrapper.prepare(); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + public static void start(Context context, Uri uri){ |
| 61 | + Intent i = new Intent(context,IjkPlayerDemoActivity.class); |
| 62 | + i.setData(uri); |
| 63 | + context.startActivity(i); |
| 64 | + } |
| 65 | + |
| 66 | + protected Uri getUri() { |
| 67 | + Intent i = getIntent(); |
| 68 | + if (i == null || i.getData() == null){ |
| 69 | + return null; |
| 70 | + } |
| 71 | + return i.getData(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { |
| 76 | + surface = new Surface(surfaceTexture); |
| 77 | + mMediaPlayerWrapper.getPlayer().setSurface(surface); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
| 88 | + mMediaPlayerWrapper.getPlayer().setSurface(null); |
| 89 | + this.surface = null; |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void onDestroy() { |
| 100 | + super.onDestroy(); |
| 101 | + mMediaPlayerWrapper.onDestroy(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected void onPause() { |
| 106 | + super.onPause(); |
| 107 | + mMediaPlayerWrapper.onPause(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void onResume() { |
| 112 | + super.onResume(); |
| 113 | + mMediaPlayerWrapper.onResume(); |
| 114 | + } |
| 115 | + |
| 116 | + public void cancelBusy(){ |
| 117 | + findViewById(R.id.progress).setVisibility(View.GONE); |
| 118 | + } |
| 119 | +} |
0 commit comments