Android video player third party framework for ijkplayer

I: IJKPlayer is B station open source super easy to use video player oh, I believe everyone has heard. IJKPlayer is available for Android and iOS, and also supports a variety of video hard decoding II: Use Step 1: Add dependencies

Dependencies {// ARMv7a CPU Dependencies { Support for ARMV7A and ARM64 covers most mobile phones // REQUIRED, Enough for most devices. The implementation 'TV. Danmaku. Ijk. Media: ijkplayer - Java: 0.8.8' implementation 'TV. Danmaku. Ijk. Media: ijkplayer - armv7a: 0.8.8' / / Other ABIs: Optional / / here x86 x86_64 for tablets and simulator implementation 'TV. Danmaku. Ijk. Media: ijkplayer - armv5:0.8.8' implementation 'TV. Danmaku. Ijk. Media: ijkplayer - arm64:0.8.8' implementation 'TV. Danmaku. Ijk. Media: ijkplayer - x86:0.8.8' implementation 'TV. Danmaku. Ijk. Media: ijkplayer - x86_64:0.8.8' / / ExoPlayer as IMediaPlayer: Optional, experimental implementation 'TV. Danmaku. Ijk. Media: ijkplayer - exobiology: 0.8.8'}

Step 2: Add three SO libraries



See that we have configured the ·so library under libs

You need the app under build.gradle

Android {defaultConfig{NDK {// Select the.so library for the corresponding CPU type to add. , packaging compiler would only add to a armeabi - v7a, so library abiFilters' armeabi - v7a '}} / / should be under the libs configuration need to, Srcdirs = ['libs']}} Srcdirs = ['libs']}}

The three SO libraries libijkffmpeg. So encapsulate the use of decoding, libijkplayer.so for video playback, and libijksdl.so Step 3: Create a View

public class MyIjkVideoView extends FrameLayout { private Context mContext; // Context private iMediaPlayer mMediaPlayer = null; // private TextView subtitleDisplay; Private SurfaceView private SurfaceView; // The view is mainly used for passive updates, such as frequently refreshing the private VideoPlayerListener listener; // private String mPath = "; // private String mPath = "; private boolean mEnableBackgroundPlay = false; Public MyIJKVideoView (@nonnull Context Context) {super(Context); initVideoView(context); } public MyIjkVideoView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); initVideoView(context); } public MyIjkVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initVideoView(context); } private void initVideoView(Context Context) {mContext = Context;} private void initVideoView(Context Context); // Whether or not to enable background playback. If background playback is enabled, start a Service to do background playback. initBackground(); // Initialize the renderer, create a SurfaceView or TextuReview, and addView(). Renders(); / / focus setFocusable (true); setFocusableInTouchMode(true); requestFocus(); // View subtitleDisplay = new TextView(context); subtitleDisplay.setTextSize(24); subtitleDisplay.setGravity(Gravity.CENTER); FrameLayout.LayoutParams layoutParams_txt = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); addView(subtitleDisplay, layoutParams_txt); } renders renders () {private void renders () {// render renders () {// render renders () {// render renders () {// render renders () {// render renders (); Specific need to deal with the if (mEnableBackgroundPlay) {MediaPlayerService. IntentToStart (getContext ()); mMediaPlayer = MediaPlayerService.getMediaPlayer(); }} /** * Set the video address. * Do different actions depending on whether the video is playing for the first time. * * @param path the path of the video. */ public void setVideoPath(String path) { if (TextUtils.equals("", MPath)) {// create a new surfaceView mPath = path if this is the first time the video is played; createSurfaceView(); } else {// load mPath = path; load(); Private void createSurfaceView() {public void createSurfaceView() {public void createSurfaceView(); private void createSurfaceView() SurfaceView(mContext); surfaceView.getHolder().addCallback(new LmnSurfaceCallback()); LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.MATCH_PARENT, Gravity.CENTER); surfaceView.setLayoutParams(layoutParams); this.addView(surfaceView); } /** * private class LMNSurfaceCallback implements SurfaceHolder.callback {@Override public Public void surfaceCreated(@nonnull surfaceHolder holder) {// Override public void surfaceChanged(@nonnull surfaceHolder holder) SurfaceHolder holder, int format, int width, int height) {// SurfaceView will change, load(); } @Override public void surfaceDestroyed(@Nonnull surfaceHolder holder) {// Override public void surfaceDestroyed(@Nonnull surfaceHolder holder) {// Override public void surfaceDestroyed(@Nonnull surfaceView) Void load() {// Create iMediaPlayer createPlayer() each time; try { mMediaPlayer.setDataSource(mPath); } catch (IOException e) { e.printStackTrace(); } // Set the MediaPlayer view mMediaPlayer. SetDisplay (SurfaceView.getHolder ()); mMediaPlayer.prepareAsync(); } private void createPlayer() {if (MmediaPlayer! = null) { mMediaPlayer.stop(); mMediaPlayer.setDisplay(null); mMediaPlayer.release(); } // Create a new IJKMediaPlayer iJKMediaPlayer = new IJKMediaPlayer (); ijkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG); // Enable hard decoding ijkmediaPlayer.setOption (ijkmediaPlayer.opt_category_player, "mediaCodec ", 1); mMediaPlayer = ijkMediaPlayer; if (listener ! = null) { mMediaPlayer.setOnPreparedListener(listener); mMediaPlayer.setOnErrorListener(listener); mMediaPlayer.setOnCompletionListener(listener); } } public void setListener(VideoPlayerListener listener) { this.listener = listener; if (mMediaPlayer ! = null) { mMediaPlayer.setOnPreparedListener(listener); } } public interface VideoPlayerListener extends IMediaPlayer.OnPreparedListener, IMediaPlayer.OnCompletionListener, IMediaPlayer. OnErrorListener {} / * * the following configuration some pause, methods such as start, stop * / public Boolean isPlaying () {if (mMediaPlayer! = null) { return mMediaPlayer.isPlaying(); } return false; } public void start() { if (mMediaPlayer ! = null) { mMediaPlayer.start(); } } public void pause() { if (mMediaPlayer ! = null) { mMediaPlayer.pause(); } } public void stop() { if (mMediaPlayer ! = null) { mMediaPlayer.stop(); } } public void reset() { if (mMediaPlayer ! = null) { mMediaPlayer.reset(); } } public void release() { if (mMediaPlayer ! = null) { mMediaPlayer.reset(); mMediaPlayer.release(); mMediaPlayer = null; }}}

Use views in the layout

<com.ruan.mygitignore.MyIjkVideoView
        android:id="@+id/ijk_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Step 4: Invoke

MyIjkVideoView ijkVideoView=findViewById(R.id.ijk_video); String url = "https://wdl.wallstreetcn.com/41aae4d2-390a-48ff-9230-ee865552e72d"; / / load the local library IjkMediaPlayer. LoadLibrariesOnce (null). IjkMediaPlayer.native_profileBegin("libijkplayer.so"); / / monitor ijkVideoView setListener (new MyIjkVideoView VideoPlayerListener () {@ Override public void OnCompletion (imediaPlayer imediaPlayer) {log. d("aa"," complete "); } @Override public Boolean onError(iMediaPlayer iMediaPlayer, int I, int i1) {log. d("aa"," failed "); return false; } @Override public void onPrepared(iMediaPlayer iMediaPlayer) {log. d("aa"," prepare "); }}); / / set the path ijkVideoView. SetVideoPath (url); // Start loading ijkvideoview.start ();

The following are examples of a lot on the official https://github.com/Bilibili/i…

Ending: don’t pretend to work hard, the result will not accompany you acting