Browse Source

bugfix: 解决部分机器不支持部分音频解码格式问题

hexleo 5 years ago
parent
commit
fd2ec192ed

+ 7 - 1
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AudioPlayer.kt

@@ -75,6 +75,12 @@ class AudioPlayer(val player: AnimPlayer) {
         extractor.selectTrack(audioIndex)
         extractor.selectTrack(audioIndex)
         val format = extractor.getTrackFormat(audioIndex)
         val format = extractor.getTrackFormat(audioIndex)
         val mime =format.getString(MediaFormat.KEY_MIME) ?: ""
         val mime =format.getString(MediaFormat.KEY_MIME) ?: ""
+        if (!MediaUtil.checkSupportCodec(mime)) {
+            ALog.e(TAG, "mime=$mime not support")
+            release()
+            return
+        }
+
         val decoder = MediaCodec.createDecoderByType(mime).apply {
         val decoder = MediaCodec.createDecoderByType(mime).apply {
             configure(format, null, null, 0)
             configure(format, null, null, 0)
             start()
             start()
@@ -91,7 +97,7 @@ class AudioPlayer(val player: AnimPlayer) {
         val bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT)
         val bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT)
         val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM)
         val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM)
         this.audioTrack = audioTrack
         this.audioTrack = audioTrack
-        val state = audioTrack.getState()
+        val state = audioTrack.state
         if (state != AudioTrack.STATE_INITIALIZED) {
         if (state != AudioTrack.STATE_INITIALIZED) {
             release()
             release()
             ALog.e(TAG, "init audio track failure")
             ALog.e(TAG, "init audio track failure")

+ 20 - 10
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/MediaUtil.kt

@@ -20,16 +20,21 @@ import android.media.MediaExtractor
 import android.media.MediaFormat
 import android.media.MediaFormat
 import com.tencent.qgame.animplayer.Constant
 import com.tencent.qgame.animplayer.Constant
 import com.tencent.qgame.animplayer.FileContainer
 import com.tencent.qgame.animplayer.FileContainer
+import kotlin.collections.HashMap
 
 
 
 
 object MediaUtil {
 object MediaUtil {
 
 
     private const val TAG = "${Constant.TAG}.MediaUtil"
     private const val TAG = "${Constant.TAG}.MediaUtil"
 
 
+    private var isTypeMapInit = false
+    private val supportTypeMap = HashMap<String, Boolean>()
+
     val isDeviceSupportHevc by lazy {
     val isDeviceSupportHevc by lazy {
-        checkCodec("video/hevc")
+        checkSupportCodec("video/hevc")
     }
     }
 
 
+
     fun getExtractor(file: FileContainer): MediaExtractor {
     fun getExtractor(file: FileContainer): MediaExtractor {
         val extractor = MediaExtractor()
         val extractor = MediaExtractor()
         file.setDataSource(extractor)
         file.setDataSource(extractor)
@@ -70,11 +75,19 @@ object MediaUtil {
         return -1
         return -1
     }
     }
 
 
-
     /**
     /**
      * 检查设备解码支持类型
      * 检查设备解码支持类型
      */
      */
-    private fun checkCodec(mimeType: String): Boolean {
+    fun checkSupportCodec(mimeType: String): Boolean {
+        if (!isTypeMapInit) {
+            isTypeMapInit = true
+            getSupportType()
+        }
+        return supportTypeMap.containsKey(mimeType.toLowerCase())
+    }
+
+
+    private fun getSupportType() {
         try {
         try {
             val numCodecs = MediaCodecList.getCodecCount()
             val numCodecs = MediaCodecList.getCodecCount()
             for (i in 0 until numCodecs) {
             for (i in 0 until numCodecs) {
@@ -84,16 +97,13 @@ object MediaUtil {
                 }
                 }
                 val types = codecInfo.supportedTypes
                 val types = codecInfo.supportedTypes
                 for (j in types.indices) {
                 for (j in types.indices) {
-                    if (types[j].equals(mimeType, ignoreCase = true)) {
-                        return true
-                    }
+                    supportTypeMap[types[j].toLowerCase()] = true
                 }
                 }
             }
             }
-            return false
+            ALog.i(TAG, "supportType=${supportTypeMap.keys}")
         } catch (t: Throwable) {
         } catch (t: Throwable) {
-            ALog.e(TAG, "checkCodec $t")
-            return false
+            ALog.e(TAG, "getSupportType $t")
         }
         }
-
     }
     }
+
 }
 }