openharmony 鸿蒙 arkts-apis-media-MediaSource

2026-08-25 浏览 (1)

Interface (MediaSource)

The MediaSource class defines the media data information, which is from createMediaSourceWithUrl.

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.
  • The initial APIs of this interface are supported since API version 12.

Modules to Import

import { media } from '@kit.MediaKit';

setMimeType12+

setMimeType(mimeType: AVMimeTypes): void

Sets the MIME type to help the player process extended media sources.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.Media.Core

Parameters

NameTypeMandatoryDescription
mimeTypeAVMimeTypesYesMIME type.

setMediaResourceLoaderDelegate18+

setMediaResourceLoaderDelegate(resourceLoader: MediaSourceLoader): void

Sets a MediaSourceLoader object, which is used to help the player request media data.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Multimedia.Media.Core

Parameters

NameTypeMandatoryDescription
resourceLoaderMediaSourceLoaderYesMediaSourceLoader object used to obtain media data for the player.

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 sourceOpenCallback: media.SourceOpenCallback = (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;
};

let sourceReadCallback: media.SourceReadCallback = (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.
};

let sourceCloseCallback: media.SourceCloseCallback = (uuid: number) => {
  console.info(`Closing resource with handle ${uuid}`);
  // Clear resources related to the current UUID.
  requests.remove(uuid);
};

// Implemented by applications as required.
let resourceLoader: media.MediaSourceLoader = {
  open: sourceOpenCallback,
  read: sourceReadCallback,
  close: sourceCloseCallback
};

mediaSource.setMediaResourceLoaderDelegate(resourceLoader);

enableOfflineCache23+

enableOfflineCache(enable: boolean): void

Sets whether to enable offline caching during video playback.

System capability: SystemCapability.Multimedia.Media.Core

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether to enable offline caching during video playback. true to enable, false otherwise.

getID

getID(): string

Obtains the ID of a media source.

Since: 26.0.0

Atomic service API: This API can be used in atomic services since API version 26.0.0.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Multimedia.Media.Core

Return value

TypeDescription
stringID of the media source. If the operation fails, an empty string is returned.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-avrecorder-oh-avrecorder-range

openharmony 鸿蒙 errorcode-media

openharmony 鸿蒙 capi-avplayer

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

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/kJs8vDe0