Browse Source

Merge pull request #111 from iostyle/master

支持ByteArray播放动画
hexleo 4 years ago
parent
commit
2aee1bd219

+ 35 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/StreamContainer.kt

@@ -0,0 +1,35 @@
+package com.tencent.qgame.animplayer.file
+
+import android.media.MediaExtractor
+import android.os.Build
+import androidx.annotation.RequiresApi
+import java.io.ByteArrayInputStream
+
+@RequiresApi(Build.VERSION_CODES.M)
+class StreamContainer(val bytes: ByteArray) : IFileContainer {
+
+    private var stream: ByteArrayInputStream = ByteArrayInputStream(bytes)
+
+    override fun setDataSource(extractor: MediaExtractor) {
+        val dataSource = StreamMediaDataSource(bytes)
+        extractor.setDataSource(dataSource)
+    }
+
+    override fun startRandomRead() {
+    }
+
+    override fun read(b: ByteArray, off: Int, len: Int): Int {
+        return stream.read(b, off, len)
+    }
+
+    override fun skip(pos: Long) {
+        stream.skip(pos)
+    }
+
+    override fun closeRandomRead() {
+    }
+
+    override fun close() {
+        stream.close()
+    }
+}

+ 34 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/StreamMediaDataSource.kt

@@ -0,0 +1,34 @@
+package com.tencent.qgame.animplayer.file
+
+import android.media.MediaDataSource
+import android.os.Build
+import androidx.annotation.RequiresApi
+
+@RequiresApi(Build.VERSION_CODES.M)
+class StreamMediaDataSource(val bytes: ByteArray) : MediaDataSource() {
+
+    override fun close() {
+    }
+
+    override fun readAt(position: Long, buffer: ByteArray, offset: Int, size: Int): Int {
+        var newSize = size
+        synchronized(StreamMediaDataSource::class) {
+            val length = bytes.size
+            if (position >= length) {
+                return -1
+            }
+            if (position + newSize > length) {
+                newSize -= (position + newSize).toInt() - length
+            }
+            System.arraycopy(bytes, position.toInt(), buffer, offset, newSize)
+            return newSize
+        }
+
+    }
+
+    override fun getSize(): Long {
+        synchronized(StreamMediaDataSource::class) {
+            return bytes.size.toLong()
+        }
+    }
+}