Interfaces (Others)
NOTE
The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
AVFileDescriptor9+
Describes an audio and video file asset. It is used to specify a particular asset for playback based on its offset and length within a file.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| fd | number | No | No | Resource handle, which is obtained by calling resourceManager.getRawFd or fileIo.open. |
| offset | number | No | Yes | Resource offset. The value ranges from 0 to the size of the audio/video file to be played. The default value is 0. The value needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources. |
| length | number | No | Yes | Resource length. The value ranges from 0 to the size of the audio or video file to be played. The default value is the number of remaining bytes starting from the offset in the file. The value needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources. |
AVDataSrcDescriptor10+
Defines the descriptor of an audio and video file, which is used in DataSource playback mode.
Use scenario: An application can create a playback instance and start playback before it finishes downloading the audio and video resources.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Multimedia.Media.AVPlayer
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| fileSize | number | No | No | Size of the file to play, in bytes. The value -1 indicates that the size is unknown. If fileSize is set to -1, the playback mode is similar to the live mode. In this mode, the seek and setSpeed operations cannot be performed, and the loop property cannot be set, indicating that loop playback is unavailable. |
| callback | (buffer: ArrayBuffer, length: number, pos?: number) => number | No | No | Callback used to fill in data. - buffer: memory to be filled. This parameter is mandatory. - length: maximum length of the memory to be filled. This parameter is mandatory. - pos: position of the data to be filled in the file. This parameter is optional. When fileSize is set to -1, this parameter cannot be used. - Return value: length of the data filled, which is of the number type. If -1 is returned, the end of stream is reached. If -2 is returned, an unrecoverable error occurs. |
SubtitleInfo12+
Describes the external subtitle information. When a subtitle update event is subscribed to, the information about the external subtitle is returned through a callback.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| text | string | No | Yes | Text information of the subtitle. |
| startTime | number | No | Yes | Start time for displaying the subtitle, in milliseconds. |
| duration | number | No | Yes | Duration for displaying the subtitle, in milliseconds. |
SeiMessage18+
Describes the information of an SEI message.
Atomic service API: This API can be used in atomic services since API version 18.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| payloadType | number | No | No | Payload type of the SEI message. |
| payload | ArrayBuffer | No | No | Payload data of the SEI message. |
MediaDescription8+
Defines media information in key-value mode.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| [key: string] | Object | No | Yes | For details about the key range supported and the object type and range of each key, see MediaDescriptionKey. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';
function printfItemDescription(obj: media.MediaDescription, key: string) {
let property: Object = obj[key];
console.info('audio key is ' + key); // Obtain the value of the key. For details about the keys, see MediaDescriptionKey.
console.info('audio value is ' + property); // Obtain the value of the key. The value can be any type. For details about the types, see MediaDescriptionKey.
}
let avPlayer: media.AVPlayer|undefined = undefined;
media.createAVPlayer((err: BusinessError, player: media.AVPlayer) => {
if(player != null) {
avPlayer = player;
console.info(`Succeeded in creating AVPlayer`);
avPlayer.getTrackDescription((error: BusinessError, arrList: Array<media.MediaDescription>) => {
if (arrList != null) {
for (let i = 0; i < arrList.length; i++) {
printfItemDescription(arrList[i], media.MediaDescriptionKey.MD_KEY_TRACK_TYPE); // Print the MD_KEY_TRACK_TYPE value of each track.
}
} else {
console.error(`Failed to get TrackDescription, error:${error}`);
}
});
} else {
console.error(`Failed to create AVPlayer, error message:${err.message}`);
}
});
PlaybackInfo12+
Defines the playback information in key-value pairs.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| [key: string] | Object | No | Yes | For details about the value range of each key, see PlaybackInfoKey. For details about the object type and range of each key, see PlaybackInfoKey. |
AVRecorderConfig9+
Describes the audio and video recording parameters.
The audioSourceType and videoSourceType parameters are used to distinguish audio-only recording, video-only recording, and audio and video recording. For audio-only recording, set only audioSourceType. For video-only recording, set only videoSourceType. For audio and video recording, set both audioSourceType and videoSourceType.
System capability: SystemCapability.Multimedia.Media.AVRecorder
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| audioSourceType | AudioSourceType | No | Yes | Type of the audio source to record. This parameter is mandatory for audio recording. Atomic service API: This API can be used in atomic services since API version 12. |
| videoSourceType | VideoSourceType | No | Yes | Type of the video source to record. This parameter is mandatory for video recording. |
| profile | AVRecorderProfile | No | No | Recording profile. This parameter is mandatory. Atomic service API: This API can be used in atomic services since API version 12. |
| url | string | No | No | Recording output URL: fd://xx (fd number).![]() This parameter is mandatory. Atomic service API: This API can be used in atomic services since API version 12. |
| fileGenerationMode12+ | FileGenerationMode | No | Yes | Mode for creating the file, which is used together with on('photoAssetAvailable'). |
| rotation(deprecated) | number | No | Yes | Rotation angle of the recorded video. The value can be 0 (default), 90, 180, or 270 for MP4 videos. This API is supported since API version 6 and deprecated since API version 12. You are advised to use AVMetadata.videoOrientation instead. If both parameters are set, AVMetadata.videoOrientation is used. |
| location(deprecated) | Location | No | Yes | Geographical location of the recorded video. By default, the geographical location information is not recorded. This API is supported since API version 6 and deprecated since API version 12. You are advised to use AVMetadata.location instead. If both parameters are set, AVMetadata.location is used. |
| metadata12+ | AVMetadata | No | Yes | Metadata. For details, see AVMetadata. |
| maxDuration18+ | number | No | Yes | Maximum recording duration, in seconds. The value range is [1, 2^31-1]. If an invalid value is provided, it is reset to the maximum allowed duration. Once the recording reaches the specified duration, it stops automatically and notifies via the stateChange callback that the recording has stopped: AVRecorderState = 'stopped', StateChangeReason = BACKGROUND. |
AVRecorderProfile9+
Describes the audio and video recording profile.
System capability: SystemCapability.Multimedia.Media.AVRecorder
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| audioBitrate | number | No | Yes | Audio encoding bit rate. This parameter is mandatory for audio recording. Supported bit rate ranges: - Range [32000, 500000] for the AAC encoding format. - 64000 for the G.711 μ-law encoding format. - Range [8000, 16000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000] for the MP3 encoding format. When the MP3 encoding format is used, the mapping between the sample rate and bit rate is as follows: - When the sample rate is lower than 16 kHZ, the bit rate range is [8000 - 64000]. - When the sample rate ranges from 16 kHz to 32 kHz, the bit rate range is [8000, 160000]. - When the sample rate is greater than 32 kHz, the bit rate range is [32000, 320000]. - Range [4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200] for the AMR-NB encoding format. - Range [6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850] for the AMR-WB encoding format. Atomic service API: This API can be used in atomic services since API version 12. |
| audioChannels | number | No | Yes | Audio channel count. This parameter is mandatory for audio recording. - Range [1, 2] for the AAC encoding format. - 1 for the G.711 μ-law encoding format. - Range [1, 2] for the MP3 encoding format. - 1 for the AMR-NB and AMR-WB encoding formats. Atomic service API: This API can be used in atomic services since API version 12. |
| audioCodec | CodecMimeType | No | Yes | Audio encoding format. This parameter is mandatory for audio recording. Currently, AUDIO_AAC, AUDIO_MP3, AUDIO_G711MU, AUDIO_AMR_NB, and AUDIO_AMR_WB are supported. Atomic service API: This API can be used in atomic services since API version 12. |
| aacProfile22+ | AacProfile | No | Yes | Extended audio encoding format. The default value is AAC_LC. Currently, the following formats are supported: AAC_LC, AAC_HE, and AAC_HE_V2. Atomic service API: This API can be used in atomic services since API version 22. |
| audioSampleRate | number | No | Yes | Audio sample rate. This parameter is mandatory for audio recording. Supported sample rate ranges: - Range [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000] for the AAC encoding format. - 8000 for the G.711 μ-law encoding format. - Range [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000] for the MP3 encoding format. - 8000 for the AMR-NB encoding format. - 16000 for the AMR-WB encoding format. Variable bit rate. The bit rate is for reference only. Atomic service API: This API can be used in atomic services since API version 12. |
| fileFormat | ContainerFormatType | No | No | Container format of a file. This parameter is mandatory. Currently, the MP4, M4A, MP3, WAV, AMR, and AAC container formats are supported. The default container format for AAC audio is ADTS frame format. The AUDIO_MP3 encoding format is not supported within the MP4 container format. Atomic service API: This API can be used in atomic services since API version 12. |
| videoBitrate | number | No | Yes | Video encoding bit rate. This parameter is mandatory for video recording. The value range is [10000, 100000000]. |
| videoCodec | CodecMimeType | No | Yes | Video encoding format. This parameter is mandatory for video recording. Currently, VIDEO_AVC and VIDEO_HEVC are supported. |
| videoFrameWidth | number | No | Yes | Width of a video frame. This parameter is mandatory for video recording. The value range is [176, 4096]. |
| videoFrameHeight | number | No | Yes | Height of a video frame. This parameter is mandatory for video recording. The value range is [144, 4096]. |
| videoFrameRate | number | No | Yes | Video frame rate. This parameter is mandatory for video recording. The recommended value range is [1, 60]. |
| isHdr11+ | boolean | No | Yes | HDR encoding. This parameter is optional for video recording. The default value is false, and there is no requirement on the encoding format. When isHdr is set to true, the encoding format must be video/hevc. |
| enableTemporalScale12+ | boolean | No | Yes | Whether temporal layered encoding is supported. This parameter is optional for video recording. The default value is false. If this parameter is set to true, some frames in the video output streams can be skipped without being encoded. |
| enableBFrame20+ | boolean | No | Yes | Whether B-frame encoding is enabled for video recording. true if enabled (valid only when the video encoding format is H.265 and the device hardware supports B-frame encoding), false otherwise. This parameter is optional in video recording scenarios. The default value is false. |
Audio Parameters
The following table lists the audio parameters. For details about each parameter, see the field description below.
| Encoding Format | Container Format | Sample Rate | Bit Rate | Audio Channel Count |
|---|---|---|---|---|
| AUDIO_AAC | MP4, M4A | [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000] | [32000-500000] | [1-2] |
| AUDIO_MP3 | MP3 | [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000] | - When the sample rate is lower than 16000, the bit rate range is [8000, 16000, 32000, 40000, 48000, 56000, 64000]. - When the sample rate ranges from 16000 to 32000, the bit rate range is [8000, 16000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000]. - When the sample rate is greater than 32000, the bit rate range is [32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000]. | [1-2] |
| AUDIO_G711MU | WAV | [8000] | [64000] | [1] |
| AUDIO_AMR_NB18+ | AMR | [8000] | [4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200] | [1] |
| AUDIO_AMR_WB18+ | AMR | [16000] | [6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850] | [1] |
Location
Describes the geographical location of the recorded video.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| latitude | number | No | No | Latitude of the geographical location. The value range is [-90, 90]. |
| longitude | number | No | No | Longitude of the geographical location. The value range is [-180, 180]. |
EncoderInfo11+
Describes the information about an encoder.
System capability: SystemCapability.Multimedia.Media.AVRecorder
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| mimeType | CodecMimeType | No | No | MIME type of the encoder. |
| type | string | No | No | Encoder type. The value audio means an audio encoder, and video means a video encoder. |
| bitRate | Range | No | Yes | Bit rate range of the encoder, with the minimum and maximum bit rates specified. |
| frameRate | Range | No | Yes | Video frame rate range, with the minimum and maximum frame rates specified. This parameter is available only for video encoders. |
| width | Range | No | Yes | Video frame width range, with the minimum and maximum widths specified. This parameter is available only for video encoders. |
| height | Range | No | Yes | Video frame height range, with the minimum and maximum heights specified. This parameter is available only for video encoders. |
| channels | Range | No | Yes | Audio channel count for the audio capturer, with the minimum and maximum numbers of audio channels specified. This parameter is available only for audio encoders. |
| sampleRate | Array<number> | No | Yes | Audio sample rate, including all available audio sample rates. The value depends on the encoder type, and this parameter is available only for audio encoders. |
Range11+
Describes a range.
System capability: SystemCapability.Multimedia.Media.AVRecorder
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| min | number | No | No | Minimum value. |
| max | number | No | No | Maximum value. |
AVTranscoderConfig12+
Describes the video transcoding parameters.
System capability: SystemCapability.Multimedia.Media.AVTranscoder
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| audioBitrate | number | No | Yes | Bit rate of the output audio, in bit/s. The value range is [1, 500000]. The default value is 48 kbit/s. Atomic service API: This API can be used in atomic services since API version 22. |
| audioCodec | CodecMimeType | No | Yes | Encoding format of the output audio. Currently, only AAC is supported. The default value is AAC. Atomic service API: This API can be used in atomic services since API version 22. |
| fileFormat | ContainerFormatType | No | No | Container format of the output video file. Currently, only MP4 is supported. Atomic service API: This API can be used in atomic services since API version 22. |
| videoBitrate | number | No | Yes | Bit rate of the output video, in bit/s. The default bit rate depends on the resolution of the output video. The default bit rate is 1 Mbit/s for the resolution in the range [240p, 480P], 2 Mbit/s for the range (480P, 720P], 4 Mbit/s for the range (720P, 1080P], and 8 Mbit/s for 1080p or higher. Atomic service API: This API can be used in atomic services since API version 22. |
| videoCodec | CodecMimeType | No | Yes | Encoding format of the output video. Currently, only AVC and HEVC are supported. If the source video is in HEVC format, the default value is HEVC. Otherwise, the default value is AVC. Atomic service API: This API can be used in atomic services since API version 22. |
| videoFrameWidth | number | No | Yes | Width of the output video frame, in px. The value range is [240, 3840]. The default value is the width of the source video frame. Atomic service API: This API can be used in atomic services since API version 22. |
| videoFrameHeight | number | No | Yes | Height of the output video frame, in px. The value range is [240, 2160]. The default value is the height of the source video frame. Atomic service API: This API can be used in atomic services since API version 22. |
| enableBFrame20+ | boolean | No | Yes | Whether B-frame encoding is enabled for transcoding. true if enabled, false otherwise. For details about the restrictions on B-frame video encoding, see Constraints in B-Frame Video Encoding. If the current environment does not meet these constraints, B-frames will be skipped, and encoding will proceed as if B-frame video encoding were not enabled. Atomic service API: This API can be used in atomic services since API version 22. |
AVMetadata11+
Defines the audio and video metadata.
System capability: SystemCapability.Multimedia.Media.AVMetadataExtractor
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| album | string | No | Yes | Title of the album. This property cannot be set for the AVRecorder. |
| albumArtist | string | No | Yes | Artist of the album. This property cannot be set for the AVRecorder. |
| artist | string | No | Yes | Artist of the media asset. This property cannot be set for the AVRecorder. |
| author | string | No | Yes | Author of the media asset. This property cannot be set for the AVRecorder. |
| dateTime | string | No | Yes | Time when the media asset is created. This property cannot be set for the AVRecorder. |
| dateTimeFormat | string | No | Yes | Time when the media asset is created. The value is in the YYYY-MM-DD HH:mm:ss format. This property cannot be set for the AVRecorder. |
| composer | string | No | Yes | Composer of the media asset. This property cannot be set for the AVRecorder. |
| duration | string | No | Yes | Duration of the media asset. This property cannot be set for the AVRecorder. |
| genre | string | No | Yes | Type or genre of the media asset. |
| hasAudio | string | No | Yes | Whether the media asset contains audio. This property cannot be set for the AVRecorder. |
| hasVideo | string | No | Yes | Whether the media asset contains a video. This property cannot be set for the AVRecorder. |
| mimeType | string | No | Yes | MIME type of the media asset. This property cannot be set for the AVRecorder. |
| trackCount | string | No | Yes | Number of tracks of the media asset. This property cannot be set for the AVRecorder. |
| sampleRate | string | No | Yes | Audio sample rate, in Hz. This property cannot be set for the AVRecorder. |
| title | string | No | Yes | Title of the media asset. This parameter is read-only in the current version. This property cannot be set for the AVRecorder. |
| description23+ | string | No | Yes | Description of a media resource. This parameter is read-only in the current version. This property cannot be set for the AVRecorder. |
| videoHeight | string | No | Yes | Video height, in px. This property cannot be set for the AVRecorder. |
| videoWidth | string | No | Yes | Video width, in px. This property cannot be set for the AVRecorder. |
| videoOrientation | string | No | Yes | Video rotation direction, in degrees. |
| hdrType12+ | HdrType | No | Yes | HDR type of the media asset. This property cannot be set for the AVRecorder. |
| location12+ | Location | No | Yes | Geographical location of the media asset. |
| customInfo12+ | Record<string, string> | No | Yes | Custom key-value mappings obtained from moov.meta.list. |
| tracks20+ | Array<MediaDescription> | No | Yes | Track information of the media asset. This property cannot be set for the AVRecorder. |
| encoder | string | No | Yes | Identifier of the software, hardware, and settings used for encoding. This property cannot be set for the AVRecorder. Since: 26.0.0 Model restriction: This API can be used only in the stage model. |
MediaDescriptionKey8+ Values Supported by AVMetadata.tracks
The following table lists the values of MediaDescriptionKey supported by AVMetadata.tracks.
| Name | Value | Applicable Track Type |
|---|---|---|
| MD_KEY_TRACK_INDEX | 'track_index' | All |
| MD_KEY_TRACK_TYPE | 'track_type' | All |
| MD_KEY_CODEC_MIME | 'codec_mime' | Audio and video |
| MD_KEY_WIDTH | 'width' | Video |
| MD_KEY_HEIGHT | 'height' | Video |
| MD_KEY_FRAME_RATE | 'frame_rate' | Video |
| MD_KEY_AUD_CHANNEL_COUNT | 'channel_count' | Audio |
| MD_KEY_AUD_SAMPLE_RATE | 'sample_rate' | Audio |
| MD_KEY_HDR_TYPE12+ | 'hdr_type' | Video |
| MD_KEY_ORIGINAL_WIDTH21+ | 'original_width' | Video |
| MD_KEY_ORIGINAL_HEIGHT21+ | 'original_height' | Video |
| MD_KEY_BITRATE | 'bitrate' | Audio and video (supported since API version 21) |
| MD_KEY_AUD_SAMPLE_DEPTH12+ | 'sample_depth' | Audio (supported since API version 21) |
PixelMapParams12+
Defines the format parameters of the video thumbnail to be obtained.
System capability: SystemCapability.Multimedia.Media.AVImageGenerator
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| width | number | No | Yes | Width of the thumbnail, in pixels. The value must be greater than 0 and less than or equal to the width of the original video. Otherwise, the returned thumbnail will not be scaled. |
| height | number | No | Yes | Height of the thumbnail, in pixels. The value must be greater than 0 and less than or equal to the height of the original video. Otherwise, the returned thumbnail will not be scaled. |
OutputSize20+
Describes the output size of the video thumbnail fetched.
System capability: SystemCapability.Multimedia.Media.AVImageGenerator
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| width | number | No | Yes | Width of the thumbnail, in pixels. - If this parameter is set to a value less than 0, the width will be the original video width. - If the value is 0 or is not assigned, the scaling ratio is the same as the height ratio. - If neither width nor height is assigned, the output is the width and height of the original video frame. |
| height | number | No | Yes | Height of the thumbnail, in pixels. - If this parameter is set to a value less than 0, the height will be the original video height. - If the value is 0 or is not assigned, the scaling ratio is the same as the width ratio. - If neither width nor height is assigned, the output is the width and height of the original video frame. |
FrameInfo23+
Describes the return value of the operation for obtaining video thumbnails in batches, including the time point for requesting frame extraction, actual time point for frame extraction, format of thumbnails output from the video, and result of obtaining a single thumbnail.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Multimedia.Media.AVMetadataExtractor
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| requestedTimeUs | number | No | No | Time point for requesting frame extraction. |
| actualTimeUs | number | No | Yes | Actual time point for frame extraction. |
| image | image.PixelMap | No | Yes | Format of the thumbnail output from the video. |
| result | FetchResult | No | No | Result of the task for obtaining a single thumbnail. For example, the task is successful, failed, or canceled. |
MediaStream19+
Defines the media stream data information.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| url | string | No | No | URL of the media resource. Only HTTP and HTTPS are supported. |
| width | number | No | No | Video width of the media resource. If the video width is unknown, set it to 0. In this case, PlaybackStrategy cannot be used for optimal matching. |
| height | number | No | No | Video height of the media resource. If the video width is unknown, set it to 0. In this case, PlaybackStrategy cannot be used for optimal matching. |
| bitrate | number | No | No | Bit rate of media resources, in bit/s. |
MediaSourceLoader18+
Defines a media data loader, which needs to be implemented by applications.
Atomic service API: This API can be used in atomic services since API version 18.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| open | SourceOpenCallback | No | No | Callback function implemented by applications to handle resource open requests. |
| read | SourceReadCallback | No | No | Callback function implemented by applications to handle resource read requests. |
| close | SourceCloseCallback | No | No | Callback function implemented by applications to handle resource close requests. |
Example
import { HashMap } from '@kit.ArkTS';
import { media } from '@kit.MediaKit';
let headers: Record<string, string> = {"User-Agent" : "User-Agent-Value"};
let mediaSource : media.MediaSource = media.createMediaSourceWithUrl("http://xxx", headers);
let uuid: number = 1;
let requests: HashMap<number, media.MediaSourceLoadingRequest> = new HashMap();
let mediaSourceLoader: media.MediaSourceLoader = {
open: (request: media.MediaSourceLoadingRequest) => {
console.info(`Opening resource: ${request.url}`);
// Open the resource and return a unique handle, ensuring the mapping between the UUID and request.
uuid += 1;
requests.set(uuid, request);
return uuid;
},
read: (uuid: number, requestedOffset: number, requestedLength: number) => {
console.info(`Reading resource with handle ${uuid}, offset: ${requestedOffset}, length: ${requestedLength}`);
// Check whether the UUID is valid and store the read request. Avoid blocking the request while pushing data and header information.
},
close: (uuid: number) => {
console.info(`Closing resource with handle ${uuid}`);
// Clear resources related to the current UUID.
requests.remove(uuid);
}
};
mediaSource.setMediaResourceLoaderDelegate(mediaSourceLoader);
let playStrategy : media.PlaybackStrategy = {
preferredBufferDuration: 20,
};
async function setupPlayer() {
let player = await media.createAVPlayer();
player.setMediaSource(mediaSource, playStrategy);
}
PlaybackStrategy12+
Describes the playback strategy.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| preferredWidth | number | No | Yes | Preferred width, in px. The value is an integer greater than 0, for example, 1080. Atomic service API: This API can be used in atomic services since API version 12. |
| preferredHeight | number | No | Yes | Preferred height, in px. The value is an integer greater than 0, for example, 1920. Atomic service API: This API can be used in atomic services since API version 12. |
| preferredBufferDuration | number | No | Yes | Preferred buffer duration, in seconds. The value range is [1, 20]. For details, see Minimizing Stuttering in Online Video Playback. Atomic service API: This API can be used in atomic services since API version 12. |
| preferredHdr | boolean | No | Yes | Whether HDR is preferred. true if preferred, false otherwise. The default value is false. Atomic service API: This API can be used in atomic services since API version 12. |
| enableSuperResolution18+ | boolean | No | Yes | Whether to enable super resolution. true to enable, false otherwise. The default value is false. If super resolution is disabled, super resolution APIs cannot be called. If super resolution is enabled, the default target resolution is 1920 × 1080, in px. Atomic service API: This API can be used in atomic services since API version 18. |
| showFirstFrameOnPrepare17+ | boolean | No | Yes | Whether to show the first frame after prepare is called. true to show, false otherwise. The default value is false. Atomic service API: This API can be used in atomic services since API version 17. |
| mutedMediaType | MediaType | No | Yes | Type of the media to mute. From API version 12 to 19, only MediaType.MEDIA_TYPE_AUD can be set. Starting from API version 20, MediaType.MEDIA_TYPE_VID is added. |
| preferredAudioLanguage13+ | string | No | Yes | Preferred audio track language. Set this parameter based on service requirements in DASH scenarios. In non-DASH scenarios, this parameter is not supported, and you are advised to retain the default value. Atomic service API: This API can be used in atomic services since API version 13. |
| preferredSubtitleLanguage13+ | string | No | Yes | Preferred subtitle language. Set this parameter based on service requirements in DASH scenarios. In non-DASH scenarios, this parameter is not supported, and you are advised to retain the default value. Atomic service API: This API can be used in atomic services since API version 13. |
| preferredBufferDurationForPlaying18+ | number | No | Yes | Preferred buffer duration for playback, in seconds. The playback starts once the buffering time exceeds this value. The value range is [0, 20]. Atomic service API: This API can be used in atomic services since API version 18. |
| thresholdForAutoQuickPlay18+ | number | No | Yes | Thread for starting smart frame catching, in seconds. The value must be greater than or equal to 2s and greater than preferredBufferDurationForPlaying. The default value is 5s. You can use the playback strategy to maintain the real-time quality of live streams by adjusting the smart frame-catch threshold. For FLV live streams, you can set this parameter based on service requirements. This parameter is not supported for non-FLV live streams yet. Fluctuations in network conditions can cause the player to build up a lot of data over time. The player periodically checks the gap between the current playback time and the timestamp of the latest frame in the cache. If this gap is too big, the player starts catching up at 1.2x speed. The on('speedDone') event will call back a specific value of 100, indicating that the smart frame catching feature is enabled successfully. Once the gap falls below preferredBufferDurationForPlaying, the player stops catching up and resumes the normal playback speed. Atomic service API: This API can be used in atomic services since API version 18. |
| keepDecodingOnMute20+ | boolean | No | Yes | Whether the decoder continues to run when the video media is muted, which helps in quickly opening the media. Currently, this feature is available only for videos. The default value is false, indicating that the decoder stops running when the media is muted, reducing power consumption. Atomic service API: This API can be used in atomic services since API version 20. |
AVScreenCaptureStrategy20+
Describes the screen capture strategy.
System capability: SystemCapability.Multimedia.Media.AVScreenCapture
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| keepCaptureDuringCall | boolean | No | Yes | Whether to keep screen capture during a cellular call. The value true means to keep screen capture during a cellular call, and false means the opposite. The default value is false. |
| enableBFrame | boolean | No | Yes | Whether to enable B-frame encoding for screen capture. true to enable, false otherwise. The default value is false. For details about the restrictions on B-frame video encoding, see Constraints in B-Frame Video Encoding. If the current environment does not meet the restrictions, B-frames will be skipped during screen capture, and no error will be returned. |
| privacyMaskMode23+ | number | No | Yes | Mask mode for privacy windows during screen capture. - 0: Full-screen mask mode for privacy windows. The default value is 0. - 1: Window mask mode for privacy windows. - If this parameter is set to other values, an error is returned. Model restriction: This API can be used only in the stage model. |
| enablePause | boolean | No | Yes | Whether screen capture can be paused. true: yes; false: no. The default value is false. Since: 26.0.0 Model restriction: This API can be used only in the stage model. |
AVScreenCaptureRecordConfig12+
Defines the screen capture parameters.
System capability: SystemCapability.Multimedia.Media.AVScreenCapture
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| fd | number | No | No | FD of the file output. |
| frameWidth | number | No | Yes | Width of the screen recording video, in pixels. The default screen width varies depending on the screen. |
| frameHeight | number | No | Yes | Height of the screen recording video, in pixels. The default screen height varies depending on the screen. |
| videoBitrate | number | No | Yes | Bit rate of the screen recording video. The default value is 10000000. |
| audioSampleRate | number | No | Yes | Audio sampling rate of the screen recording. This value is used for both the system sound and microphone. Only 48000 (default) and 16000 are supported. |
| audioChannelCount | number | No | Yes | Audio channel count. This value is used for both internal capture and external capture (using microphones). Only 1 and 2 (default) are supported. |
| audioBitrate | number | No | Yes | Audio bit rate. This value is used for both internal capture and external capture (using microphones). The default value is 96000. |
| preset | AVScreenCaptureRecordPreset | No | Yes | Encoding and container format used. The default value is SCREEN_RECORD_PRESET_H264_AAC_MP4. |
| displayId15+ | number | No | Yes | ID of the display used for screen capture. By default, the main screen is captured. |
| fillMode18+ | AVScreenCaptureFillMode | No | Yes | Video fill mode during screen capture. |
| strategy20+ | AVScreenCaptureStrategy | No | Yes | Screen capture strategy. |
AVMetricsEvent23+
Describes the information about a metric event.
System capability: SystemCapability.Multimedia.Media.AVPlayer
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| event | AVMetricsEventType | No | No | Type of the metric event. Model restriction: This API can be used only in the stage model. |
| timeStamp | number | No | No | System time when an event occurs. |
| playbackPosition | number | No | No | Playback position when an event occurs. |
| details | Record<string, Object> | No | No | Detailed information about an event. The information contained in an event varies according to the event type. The information includes the stalling duration (duration: number) and the media type (media: MediaType) of the stalling. Model restriction: This API can be used only in the stage model. |
VideoSize24+
Describes the video size.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| width | number | No | Yes | Video resolution width. The value must be a positive integer (greater than 0). |
| height | number | No | Yes | Video resolution height. The value must be a positive integer (greater than 0). |
TrackSelectionFilter24+
Describes the track selection filter.
System capability: SystemCapability.Multimedia.Media.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| maxVideoBitrate | number | No | Yes | Maximum bit rate of the video, in bit/s. The value must be a positive integer (greater than 0). |
| minVideoBitrate | number | No | Yes | Minimum bit rate of the video, in bit/s. The value must be a positive integer (greater than 0). |
| maxVideoFrameRate | number | No | Yes | Maximum frame rate of the video, in Hz. The value must be a positive integer (greater than 0). |
| minVideoFrameRate | number | No | Yes | Minimum frame rate of the video, in Hz. The value must be a positive integer (greater than 0). |
| maxVideoResolution | VideoSize | No | Yes | Maximum resolution of the video. |
| minVideoResolution | VideoSize | No | Yes | Minimum resolution of the video. |
| preferredVideoMimeTypes | Array<string> | No | Yes | Preferred MIME types of the video track, sorted by priority. An empty value indicates that any type is accepted. |
| maxAudioBitrate | number | No | Yes | Maximum bit rate of the audio, in bit/s. The value must be a positive integer (greater than 0). |
| minAudioBitrate | number | No | Yes | Minimum bit rate of the audio, in bit/s. The value must be a positive integer (greater than 0). |
| maxAudioChannels | number | No | Yes | Maximum number of audio channels. The value must be a positive integer (greater than 0). |
| preferredAudioMimeTypes | Array<string> | No | Yes | Preferred MIME type of the audio track, sorted by priority. An empty value indicates that any type is acceptable. |
| preferredAudioLanguages | Array<string> | No | Yes | Preferred languages of the audio track, sorted by priority. The value is a language tag that complies with the IETF BCP 47 criterion. An empty value indicates that any language is accepted. |
| preferredSubtitleLanguages | Array<string> | No | Yes | Preferred languages of the subtitle track, sorted by priority. The value is a language tag that complies with the IETF BCP 47 criterion. An empty value indicates that any language is accepted. |
AudioRecorderConfig(deprecated)
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorderConfig instead.
Describes audio recording configurations.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| audioEncoder | AudioEncoder | No | Yes | Audio encoding format. The default value is AAC_LC. Note: This parameter is supported since API version 6 and deprecated since API version 8. You are advised to use audioEncoderMime instead. |
| audioEncodeBitRate | number | No | Yes | Audio encoding bit rate. The default value is 48000. Note: This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use audioBitrate in AVRecorderProfile instead. |
| audioSampleRate | number | No | Yes | Audio sample rate. The default value is 48000. Variable bit rate. The bit rate is for reference only. Note: This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use audioSampleRate in AVRecorderProfile instead. |
| numberOfChannels | number | No | Yes | Audio channel count. The default value is 2. Note: This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use audioChannels in AVRecorderProfile instead. |
| format | AudioOutputFormat | No | Yes | Audio output format. The default value is MPEG_4. Note: This parameter is supported since API version 6 and deprecated since API version 8. You are advised to use fileFormat instead. |
| location | Location | No | Yes | Geographical location of the recorded audio. Note: This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use location in AVMetadata instead. |
| uri | string | No | No | Audio output URI. Supported: fd://xx (fd number) The file must be created by the caller and granted with proper permissions. Note: This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use url in AVRecorderConfig instead. |
| audioEncoderMime8+ | CodecMimeType | No | Yes | Container encoding format. Note: This parameter is supported since API version 8 and deprecated since API version 9. You are advised to use audioCodec in AVRecorderProfile instead. |
| fileFormat8+ | ContainerFormatType | No | Yes | Audio encoding format. Note: This parameter is supported since API version 8 and deprecated since API version 9. You are advised to use fileFormat in AVRecorderProfile instead. |
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 capi-avrecorder-oh-avrecorder-range
openharmony 鸿蒙 errorcode-media
openharmony 鸿蒙 capi-avplayer-base-h
openharmony 鸿蒙 capi-avimage-generator-h
openharmony 鸿蒙 capi-avscreencapture-oh-rect
openharmony 鸿蒙 capi-videoprocessing-videoprocessing-callback
openharmony 鸿蒙 capi-avsinkbase
openharmony 鸿蒙 capi-avmetadataextractor
openharmony 鸿蒙 capi-avscreencapture-oh-multidisplaycapability
