HDR หรือ High Dynamic Range มีช่วงสีที่กว้างขึ้นและมี คอนทราสต์ที่มากขึ้นระหว่างสีขาวที่สว่างที่สุดกับเงาที่มืดที่สุด จึงทำให้วิดีโอมี คุณภาพที่ใกล้เคียงกับสิ่งที่ตาเปล่ามองเห็นมากขึ้น
คุณตั้งค่าการเล่นวิดีโอ HDR ในแอปเพื่อดูตัวอย่างและเล่นเนื้อหาวิดีโอ HDR ได้
บทความนี้ถือว่าคุณได้เพิ่มการรองรับการเล่นวิดีโอพื้นฐานลงในแอปแล้ว ดูรายละเอียดเพิ่มเติมเกี่ยวกับการเล่นได้ในเอกสารประกอบของ ExoPlayer
ข้อกำหนดเบื้องต้นของอุปกรณ์
อุปกรณ์ Android บางรุ่นไม่รองรับการเล่น HDR ก่อนเล่นเนื้อหาวิดีโอ HDR ในแอป ให้ตรวจสอบว่าอุปกรณ์มีคุณสมบัติตรงตามข้อกำหนดเบื้องต้นต่อไปนี้
- กำหนดเป้าหมายเป็น Android 7.0 ขึ้นไป (API เลเยอร์ 24)
- มีตัวถอดรหัสที่รองรับ HDR และเข้าถึงจอแสดงผลที่รองรับ HDR ได้
ตรวจสอบว่าอุปกรณ์รองรับการเล่นแบบ HDR หรือไม่
ใช้ Display.getHdrCapabilities()
เพื่อค้นหาความสามารถ HDR ของจอแสดงผล เมธอดนี้จะแสดงข้อมูลเกี่ยวกับโปรไฟล์ HDR ที่รองรับและช่วงความสว่างสำหรับจอแสดงผล
โค้ดต่อไปนี้จะตรวจสอบว่าอุปกรณ์รองรับการเล่น HLG10 หรือไม่ ตั้งแต่ Android 13 เป็นต้นไป HLG10 จะเป็นมาตรฐานขั้นต่ำที่ผู้ผลิตอุปกรณ์ต้องรองรับหากอุปกรณ์สามารถเล่น HDR ได้
Kotlin
// Check if display supports the HDR type val capabilities = display?.hdrCapabilities?.supportedHdrTypes ?: intArrayOf() if (!capabilities.contains(HDR_TYPE_HLG)) { throw RuntimeException("Display does not support desired HDR type"); }
Java
// Check if display supports the HDR type int[] list = getDisplay().getHdrCapabilities().getSupportedHdrTypes(); Listcapabilities = Arrays.stream(list).boxed().collect(Collectors.toList()); if (!capabilities.contains(HDR_TYPE_HLG)) { throw new RuntimeException("Display does not support desired HDR type"); }
ตั้งค่าการเล่น HDR ในแอป
หากแอปใช้ ExoPlayer แอปจะรองรับการเล่น HDR โดยค่าเริ่มต้น ดูขั้นตอนถัดไปได้ที่ตรวจสอบการรองรับการเล่น HDR
หากแอปไม่ได้ใช้ ExoPlayer ให้ตั้งค่าการเล่น HDR โดยใช้ MediaCodec
ผ่าน SurfaceView
ตั้งค่า MediaCodec โดยใช้ SurfaceView
ตั้งค่าโฟลว์การเล่น MediaCodec
มาตรฐานโดยใช้ SurfaceView
ซึ่งจะช่วยให้คุณแสดงเนื้อหาวิดีโอ HDR ได้โดยไม่ต้องมีการจัดการพิเศษสำหรับการเล่น HDR ดังนี้
MediaCodec
: ถอดรหัสเนื้อหาวิดีโอ HDRSurfaceView
: แสดงเนื้อหาวิดีโอ HDR
โค้ดต่อไปนี้จะตรวจสอบว่าตัวแปลงรหัสรองรับโปรไฟล์ HDR หรือไม่ จากนั้นจะตั้งค่า MediaCodec
โดยใช้ SurfaceView
Kotlin
// Check if there's a codec that supports the specific HDR profile val list = MediaCodecList(MediaCodecList.REGULAR_CODECS) var format = MediaFormat() /* media format from the container */; format.setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AV1ProfileMain10) val codecName = list.findDecoderForFormat (format) ?: throw RuntimeException ("No codec supports the format") // Here is a standard MediaCodec playback flow val codec: MediaCodec = MediaCodec.createByCodecName(codecName); val surface: Surface = surfaceView.holder.surface val callback: MediaCodec.Callback = (object : MediaCodec.Callback() { override fun onInputBufferAvailable(codec: MediaCodec, index: Int) { queue.offer(index) } override fun onOutputBufferAvailable( codec: MediaCodec, index: Int, info: MediaCodec.BufferInfo ) { codec.releaseOutputBuffer(index, timestamp) } override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) { // handle error } override fun onOutputFormatChanged( codec: MediaCodec, format: MediaFormat ) { // handle format change } }) codec.setCallback(callback) codec.configure(format, surface, crypto, 0 /* flags */) codec.start() while (/* until EOS */) { val index = queue.poll() val buffer = codec.getInputBuffer(index) buffer?.put(/* write bitstream */) codec.queueInputBuffer(index, offset, size, timestamp, flags) } codec.stop() codec.release()
Java
// Check if there's a codec that supports the specific HDR profile MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS); MediaFormat format = /* media format from the container */; format.setInteger( MediaFormat.KEY_PROFILE, CodecProfileLevel.AV1ProfileMain10); String codecName = list.findDecoderForFormat(format); if (codecName == null) { throw new RuntimeException("No codec supports the format"); } // Below is a standard MediaCodec playback flow MediaCodec codec = MediaCodec.getCodecByName(codecName); Surface surface = surfaceView.getHolder().getSurface(); MediaCodec.Callback callback = new MediaCodec.Callback() { @Override void onInputBufferAvailable(MediaCodec codec, int index) { queue.offer(index); } @Override void onOutputBufferAvailable(MediaCodec codec, int index) { // release the buffer for render codec.releaseOutputBuffer(index, timestamp); } @Override void onOutputFormatChanged(MediaCodec codec, MediaFormat format) { // handle format change } @Override void onError(MediaCodec codec, MediaCodec.CodecException ex) { // handle error } }; codec.setCallback(callback); codec.configure(format, surface, crypto, 0 /* flags */); codec.start(); while (/* until EOS */) { int index = queue.poll(); ByteBuffer buffer = codec.getInputBuffer(index); buffer.put(/* write bitstream */); codec.queueInputBuffer(index, offset, size, timestamp, flags); } codec.stop(); codec.release();
ดูMediaCodec
การใช้งานเพิ่มเติมโดยใช้ SurfaceView
ได้ที่ตัวอย่างกล้อง Android
แหล่งข้อมูล
ดูข้อมูลเพิ่มเติมเกี่ยวกับการเล่น HDR ได้จากแหล่งข้อมูลต่อไปนี้
HDR
- การจับภาพวิดีโอ HDR: ดูวิธีตั้งค่าการจับภาพวิดีโอ HDR โดยใช้ Camera2 API
- ตัวอย่าง Camera2Video ใน Github: ดูแอปที่ใช้งานได้ซึ่งมีฟังก์ชันการจับภาพและการเล่น HDR
สื่อ
- เอกสารอ้างอิง Media API: ดูข้อมูลเพิ่มเติมเกี่ยวกับ Media API
- ExoPlayer: ดูวิธีตั้งค่าแอปด้วยไลบรารี ExoPlayer