无法证明 Long :: String :: Option[Long] :: Option[java.util.UUID] :: shapeless.HNil 可以转换为 to/from SimpleMp4BoxHeader

Could not prove that Long :: String :: Option[Long] :: Option[java.util.UUID] :: shapeless.HNil can be converted to/from SimpleMp4BoxHeader

我是初学者,正在处理 MP4 文件 decoding.when 我的代码如下,它报告“无法证明 Long :: String :: Option[Long] :: Option[java.util.UUID] :: shapeless.HNil 可以转换为 to/from SimpleMp4BoxHeader”。我该如何解决这个问题?

sealed trait Mp4BoxHeader {
  def size: Int
  def btype: String
}

case class SimpleMp4BoxHeader(size: Int, btype: String) extends Mp4BoxHeader

object SimpleMp4BoxHeader {
  // val scodec = uint32 ~ ascii32

  def apply(size: Int, btype: String): SimpleMp4BoxHeader = SimpleMp4BoxHeader(size, btype)

  implicit def codec: Codec[SimpleMp4BoxHeader] = "simpleMp4BoxHeader" | { ("size" | uint32 ) :: ("btype" | ascii32) }.as[SimpleMp4BoxHeader]

}

因为 uint32Codec 对于 Long 类型但是你的 size 应该是 Int, 看看 scodec.codecs 包对象:

/**
 * Codec for 16-bit unsigned big-endian integers.
 * @group numbers
 */
val uint16: Codec[Int] = new IntCodec(16, false, ByteOrdering.BigEndian)

/**
 * Codec for 24-bit unsigned big-endian integers.
 * @group numbers
 */
val uint24: Codec[Int] = new IntCodec(24, false, ByteOrdering.BigEndian)

/**
 * Codec for 32-bit unsigned big-endian integers.
 * @group numbers
 */
val uint32: Codec[Long] = new LongCodec(32, false, ByteOrdering.BigEndian)

您应该为 Intuint24uint16)选择最兼容的 Codec 一些编解码器,将 uint32 替换为您选择的代码将编译。