Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
HDR, czyli High Dynamic Range, zapewnia szerszy zakres kolorów i większy kontrast między najjaśniejszą bielą a najciemniejszymi cieniami, dzięki czemu jakość wideo jest bardziej zbliżona do tego, co widzi nieuzbrojone oko.
W aplikacji możesz skonfigurować odtwarzanie filmów HDR, aby wyświetlać podgląd treści w tym formacie i je odtwarzać.
W tym artykule zakładamy, że w aplikacji masz już dodaną podstawową obsługę odtwarzania filmów. Więcej informacji o odtwarzaniu znajdziesz w dokumentacji ExoPlayera.
Wymagania dotyczące urządzenia
Nie wszystkie urządzenia z Androidem obsługują odtwarzanie w HDR. Zanim zaczniesz odtwarzać w aplikacji treści wideo w HDR, sprawdź, czy Twoje urządzenie spełnia te wymagania wstępne:
Aplikacja jest kierowana na Androida 7.0 lub nowszego (warstwa API 24).
ma dekoder obsługujący HDR i dostęp do wyświetlacza obsługującego HDR;
Sprawdzanie obsługi odtwarzania w HDR
Użyj Display.getHdrCapabilities(), aby wysłać zapytanie o możliwości HDR wyświetlacza. Metoda zwraca informacje o obsługiwanych profilach HDR i zakresie luminancji wyświetlacza.
Poniższy kod sprawdza, czy urządzenie obsługuje odtwarzanie w formacie HLG10. Od Androida 13 HLG10 jest minimalnym standardem, który producenci urządzeń muszą obsługiwać, jeśli urządzenie umożliwia odtwarzanie HDR:
Kotlin
// Check if display supports the HDR typevalcapabilities=display?.hdrCapabilities?.supportedHdrTypes?:intArrayOf()if(!capabilities.contains(HDR_TYPE_HLG)){throwRuntimeException("Display does not support desired HDR type");}
Java
// Check if display supports the HDR typeint[]list=getDisplay().getHdrCapabilities().getSupportedHdrTypes();Listcapabilities=Arrays.stream(list).boxed().collect(Collectors.toList());if(!capabilities.contains(HDR_TYPE_HLG)){thrownewRuntimeException("Display does not support desired HDR type");}
Konfigurowanie odtwarzania w jakości HDR w aplikacji
Jeśli Twoja aplikacja nie korzysta z ExoPlayera, skonfiguruj odtwarzanie HDR za pomocą MediaCodec przez SurfaceView.
Konfigurowanie MediaCodec za pomocą SurfaceView
Skonfiguruj standardowy przepływ odtwarzania MediaCodec za pomocą SurfaceView. Dzięki temu możesz wyświetlać treści wideo HDR bez specjalnej obsługi odtwarzania HDR:
MediaCodec: dekoduje treści wideo HDR.
SurfaceView: wyświetla treści wideo HDR.
Ten kod sprawdza, czy kodek obsługuje profil HDR, a następnie konfiguruje MediaCodec za pomocą SurfaceView:
Kotlin
// Check if there's a codec that supports the specific HDR profilevallist=MediaCodecList(MediaCodecList.REGULAR_CODECS)varformat=MediaFormat()/* media format from the container */;format.setInteger(MediaFormat.KEY_PROFILE,MediaCodecInfo.CodecProfileLevel.AV1ProfileMain10)valcodecName=list.findDecoderForFormat(format)?:throwRuntimeException("No codec supports the format")// Here is a standard MediaCodec playback flowvalcodec:MediaCodec=MediaCodec.createByCodecName(codecName);valsurface:Surface=surfaceView.holder.surfacevalcallback:MediaCodec.Callback=(object:MediaCodec.Callback(){overridefunonInputBufferAvailable(codec:MediaCodec,index:Int){queue.offer(index)}overridefunonOutputBufferAvailable(codec:MediaCodec,index:Int,info:MediaCodec.BufferInfo){codec.releaseOutputBuffer(index,timestamp)}overridefunonError(codec:MediaCodec,e:MediaCodec.CodecException){// handle error}overridefunonOutputFormatChanged(codec:MediaCodec,format:MediaFormat){// handle format change}})codec.setCallback(callback)codec.configure(format,surface,crypto,0/* flags */)codec.start()while(/* until EOS */){valindex=queue.poll()valbuffer=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 profileMediaCodecListlist=newMediaCodecList(MediaCodecList.REGULAR_CODECS);MediaFormatformat=/* media format from the container */;format.setInteger(MediaFormat.KEY_PROFILE,CodecProfileLevel.AV1ProfileMain10);StringcodecName=list.findDecoderForFormat(format);if(codecName==null){thrownewRuntimeException("No codec supports the format");}// Below is a standard MediaCodec playback flowMediaCodeccodec=MediaCodec.getCodecByName(codecName);Surfacesurface=surfaceView.getHolder().getSurface();MediaCodec.Callbackcallback=newMediaCodec.Callback(){@OverridevoidonInputBufferAvailable(MediaCodeccodec,intindex){queue.offer(index);}@OverridevoidonOutputBufferAvailable(MediaCodeccodec,intindex){// release the buffer for rendercodec.releaseOutputBuffer(index,timestamp);}@OverridevoidonOutputFormatChanged(MediaCodeccodec,MediaFormatformat){// handle format change}@OverridevoidonError(MediaCodeccodec,MediaCodec.CodecExceptionex){// handle error}};codec.setCallback(callback);codec.configure(format,surface,crypto,0/* flags */);codec.start();while(/* until EOS */){intindex=queue.poll();ByteBufferbuffer=codec.getInputBuffer(index);buffer.put(/* write bitstream */);codec.queueInputBuffer(index,offset,size,timestamp,flags);}codec.stop();codec.release();
ExoPlayer: dowiedz się, jak skonfigurować aplikację za pomocą biblioteki ExoPlayer.
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-08-27 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-08-27 UTC."],[],[],null,["# HDR video playback\n\nHDR, or High Dynamic Range, provides a wider range of colors and greater\ncontrast between the brightest whites and darkest shadows, resulting in video\nquality that more closely resembles what the naked eye perceives.\n\nYou can set up HDR video playback in your app to preview and play back HDR video\ncontent.\n\nThis article assumes that you've already added basic video playback support to\nyour app. See the [ExoPlayer](/guide/topics/media/exoplayer) documentation for\nmore details on playback.\n\nDevice prerequisites\n--------------------\n\nNot all Android devices support HDR playback. Before playing back HDR\nvideo content in your app, determine if your device meets the following\nprerequisites:\n\n- Targets Android 7.0 or higher (API layer 24).\n- Has a HDR-capable decoder and access to a HDR-capable display.\n\nCheck for HDR playback support\n------------------------------\n\nUse [`Display.getHdrCapabilities()`](/reference/android/view/Display#getHdrCapabilities()) to query a display's HDR capabilities. The method returns information about the supported HDR profiles and luminance range for the display.\n\nThe following code checks if the device supports HLG10 playback. Starting in Android 13, HLG10 is the minimum standard that device makers must support if the device is capable of HDR playback: \n\n### Kotlin\n\n```kotlin\n// Check if display supports the HDR type\nval capabilities = display?.hdrCapabilities?.supportedHdrTypes ?: intArrayOf()\nif (!capabilities.contains(HDR_TYPE_HLG)) {\n throw RuntimeException(\"Display does not support desired HDR type\");\n}\n```\n\n### Java\n\n```java\n// Check if display supports the HDR type\nint[] list = getDisplay().getHdrCapabilities().getSupportedHdrTypes();\nList capabilities = Arrays.stream(list).boxed().collect(Collectors.toList());\nif (!capabilities.contains(HDR_TYPE_HLG)) {\n throw new RuntimeException(\"Display does not support desired HDR type\");\n}\n```\n\nSet up HDR playback in your app\n-------------------------------\n\nIf your app uses [ExoPlayer](https://exoplayer.dev/), it supports HDR playback by default. See [Check for HDR playback support](#check_for_hdr_playback_support) for next steps.\n\nIf your app does not use ExoPlayer, set up HDR playback using `MediaCodec` via `SurfaceView`.\n| **Note:** HDR playback has limited support on [`TextureView`](/reference/android/view/TextureView) in Android 13 (API layer 33) and higher. When playing back HDR video content, `TextureView` transcodes the video from HDR to SDR, resulting in playback with possible loss of detail including clipped colors and video banding. If at all possible, you should use `SurfaceView` for HDR playback.\n\n### Set up MediaCodec using SurfaceView\n\nSet up a standard [`MediaCodec`](/reference/android/media/MediaCodec) playback flow using [`SurfaceView`](https://developer.android.com/reference/android/view/SurfaceView). This allows you to display HDR video content without any special handling for HDR playback:\n\n- `MediaCodec`: Decodes HDR video content.\n- `SurfaceView`: Displays HDR video content.\n\nThe following code checks if the codec supports the HDR profile, then sets up `MediaCodec` using `SurfaceView`: \n\n### Kotlin\n\n```kotlin\n// Check if there's a codec that supports the specific HDR profile\nval list = MediaCodecList(MediaCodecList.REGULAR_CODECS) var format = MediaFormat() /* media format from the container */;\nformat.setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AV1ProfileMain10)\nval codecName = list.findDecoderForFormat (format) ?: throw RuntimeException (\"No codec supports the format\")\n\n// Here is a standard MediaCodec playback flow\nval codec: MediaCodec = MediaCodec.createByCodecName(codecName);\nval surface: Surface = surfaceView.holder.surface\nval callback: MediaCodec.Callback = (object : MediaCodec.Callback() {\n override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {\n queue.offer(index)\n }\n\n override fun onOutputBufferAvailable(\n codec: MediaCodec,\n index: Int,\n info: MediaCodec.BufferInfo\n ) {\n codec.releaseOutputBuffer(index, timestamp)\n }\n\n override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {\n // handle error\n }\n\n override fun onOutputFormatChanged(\n codec: MediaCodec, format: MediaFormat\n ) {\n // handle format change\n }\n})\n\ncodec.setCallback(callback)\ncodec.configure(format, surface, crypto, 0 /* flags */)\ncodec.start()\nwhile (/* until EOS */) {\n val index = queue.poll()\n val buffer = codec.getInputBuffer(index)\n buffer?.put(/* write bitstream */)\n codec.queueInputBuffer(index, offset, size, timestamp, flags)\n}\ncodec.stop()\ncodec.release()\n```\n\n### Java\n\n```java\n// Check if there's a codec that supports the specific HDR profile\nMediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS);\nMediaFormat format = /* media format from the container */;\nformat.setInteger(\n MediaFormat.KEY_PROFILE, CodecProfileLevel.AV1ProfileMain10);\nString codecName = list.findDecoderForFormat(format);\nif (codecName == null) {\n throw new RuntimeException(\"No codec supports the format\");\n}\n\n// Below is a standard MediaCodec playback flow\nMediaCodec codec = MediaCodec.getCodecByName(codecName);\nSurface surface = surfaceView.getHolder().getSurface();\nMediaCodec.Callback callback = new MediaCodec.Callback() {\n @Override\n void onInputBufferAvailable(MediaCodec codec, int index) {\n queue.offer(index);\n }\n\n @Override\n void onOutputBufferAvailable(MediaCodec codec, int index) {\n // release the buffer for render\n codec.releaseOutputBuffer(index, timestamp);\n }\n\n @Override\n void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {\n // handle format change\n }\n\n @Override\n void onError(MediaCodec codec, MediaCodec.CodecException ex) {\n // handle error\n }\n\n};\ncodec.setCallback(callback);\ncodec.configure(format, surface, crypto, 0 /* flags */);\ncodec.start();\nwhile (/* until EOS */) {\n int index = queue.poll();\n ByteBuffer buffer = codec.getInputBuffer(index);\n buffer.put(/* write bitstream */);\n codec.queueInputBuffer(index, offset, size, timestamp, flags);\n}\ncodec.stop();\ncodec.release();\n```\n\nFor more `MediaCodec` implementations using `SurfaceView`, see the [Android Camera samples](https://github.com/android/camera-samples).\n| **Note:** Android takes screenshots in SDR. HDR content is tonemapped to SDR in screenshots.\n\nResources\n---------\n\nFor more information related to HDR playback, see the following resources:\n\n### HDR\n\n- [HDR video capture](/training/camera2/hdr-video-capture): learn how to set up HDR video capture using the Camera2 APIs.\n- [Camera2Video sample on Github](https://github.com/android/camera-samples/tree/main/Camera2Video): see a working app with HDR capture and playback functionality.\n\n### Media\n\n- [Media API reference](/reference/android/media/package-summary): learn more about the Media APIs.\n- [ExoPlayer](https://exoplayer.dev/): learn how to set up your app with the ExoPlayer library."]]