Recently, I have been doing audio and video related content, so I cannot avoid the problem of video acquisition and playback. The player uses ijkplayer. There is no problem with using url to play this thing, but our solution is to receive the video stream of flv for playback, which requires the IAndroidIO interface, which can also be used to play local files.
Implement IAndroidIO interface
Playback class implementation
class ReadByteIO private constructor(): IAndroidIO { companion object { private var instance: ReadByteIO? = null var URL_SUFFIX = "recv_data_online" @Synchronized fun getInstance(): ReadByteIO { // Single case instance?.let { return it } instance = ReadByteIO() return instance!! } } private var TAG = ReadByteIO:: private var flvData = LinkedBlockingDeque<Byte>() // Memory queue is used to cache the obtained streaming data. To achieve the frame-chasing effect, you only need to discard the local cached content according to the policy. private fun takeFirstWithLen(len : Int): ByteArray { // Get byte data for interface rendering var byteList = ByteArray(len) for (i in 0 until len) { byteList[i] = () } return byteList } @Synchronized fun addLast(bytes: ByteArray): Boolean { var tmpList:List<Byte> = () (TAG, "tmpList size " + ) return (tmpList) } // If you are playing local files, you can open the file stream here and then read the file stream later. override fun open(url: String?): Int { if (url == URL_SUFFIX) { return 1 // Open the play stream successfully } return -1 // Open playback failed } override fun read(buffer: ByteArray?, size: Int): Int { var tmpBytes = takeFirstWithLen(size) // Blocking reading, no data, no rendering of the picture (tmpBytes, 0, buffer, 0, size) return size } override fun seek(offset: Long, whence: Int): Long { return 0 } override fun close(): Int { return 0 } }
Call playback class
Next, let’s take a look at how to call the playback instance. Note: We still need to pass in a url, but this url is our custom
public class RecordVideoActivity extends AppCompatActivity implements { private String TAG = (); private IjkMediaPlayer player; private Surface surface; private TextureView playView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_record_video); playView = findViewById(.v_play); (this); } @Override protected void onDestroy() { (); if (player != null) { (); } } @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { if (surface != null) { = new Surface(surface); play(); // Play the surface instance } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } private void play() { player = new IjkMediaPlayer(); (); (IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzemaxduration", 100); (IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 25 * 1024); (IjkMediaPlayer.OPT_CATEGORY_PLAYER, "packet-buffering", 0); (IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 1); (IjkMediaPlayer.OPT_CATEGORY_CODEC, "threads", 1); (IjkMediaPlayer.OPT_CATEGORY_PLAYER, "sync-av-start", 0); (IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec",1); (IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1); (IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1); (IjkMediaPlayer.OPT_CATEGORY_FORMAT, "protocol_whitelist", "ijkio,crypto,file,http,https,tcp,tls,udp"); // Properties setting support, transfer to our customized playback class (); (()); Uri uri = ("ijkio:androidio:" + .getURL_SUFFIX()); // Set our custom url try { (()); } catch (IOException e) { (); } (); (); } }
This is the article about android using IJKPlayer to play video streams. For more related android video streaming content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!