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