• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (MediaSourceLoadingRequest)
2<!--Kit: Media Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @wang-haizhou6-->
5<!--Designer: @HmQQQ-->
6<!--Tester: @xchaosioda-->
7<!--Adviser: @zengyawen-->
8
9> **NOTE**
10>
11> - 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.
12> - The initial APIs of this interface are supported since API version 18.
13
14The MediaSourceLoadingRequest class defines a loading request object. Applications use this object to obtain the location of the requested resource and to interact with the player for data exchange.
15
16## Modules to Import
17
18```ts
19import { media } from '@kit.MediaKit';
20```
21
22## Properties
23
24**System capability**: SystemCapability.Multimedia.Media.Core
25
26| Name  | Type   | Read-Only  | Optional  | Description               |
27| --------------------------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
28| url       | string                        | No  | No  | Resource URL, which is the path to the resource that the application needs to open.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
29| header     | Record<string, string>        | No  | Yes  | HTTP request header. If the header exists, the application should set the header information in the HTTP request when downloading data.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
30
31## respondData<sup>18+</sup>
32
33respondData(uuid: number, offset: number, buffer: ArrayBuffer): number
34
35Sends data to the player.
36
37**Atomic service API**: This API can be used in atomic services since API version 18.
38
39**System capability**: SystemCapability.Multimedia.Media.Core
40
41**Parameters**
42
43| Name  | Type    | Mandatory| Description                |
44| -------- | -------- | ---- | -------------------- |
45| uuid | number | Yes | 	ID for the resource handle. The source is [SourceOpenCallback](arkts-apis-media-t.md#sourceopencallback18).|
46| offset | number | Yes | 	Offset of the current media data relative to the start of the resource. The value cannot be less than 0.|
47| buffer | ArrayBuffer | Yes | 	Media data sent to the player.<br>**Note**: Do not transmit irrelevant data, as it can affect normal data parsing and playback.|
48
49**Return value**
50
51| Type          | Description                               |
52| -------------- | ----------------------------------- |
53| number | Number of bytes received by the server.<br>- A return value less than 0 indicates failure.<br>- A return value of -2 indicates that the player no longer needs the current data, and the client should stop the current read process.<br>- A return value of -3 indicates that the player's buffer is full, and the client should wait for the next read.|
54
55**Example**
56
57```ts
58import { HashMap } from '@kit.ArkTS';
59let requests: HashMap<number, media.MediaSourceLoadingRequest> = new HashMap();
60let uuid = 1;
61
62let request = requests.get(uuid);
63let offset = 0; // Offset of the current media data relative to the start of the resource.
64let buf = new ArrayBuffer(0); // Data defined by the application and pushed to the player.
65let num = request?.respondData(uuid, offset, buf);
66```
67
68## respondHeader<sup>18+</sup>
69
70respondHeader(uuid: number, header?: Record<string, string>, redirectUrl?: string): void
71
72Sends response header information to the player. This API must be called before the first call to [respondData](#responddata18).
73
74**Atomic service API**: This API can be used in atomic services since API version 18.
75
76**System capability**: SystemCapability.Multimedia.Media.Core
77
78**Parameters**
79
80| Name  | Type    | Mandatory| Description                |
81| -------- | -------- | ---- | -------------------- |
82| uuid | number | Yes | 	ID for the resource handle. The source is [SourceOpenCallback](arkts-apis-media-t.md#sourceopencallback18).|
83| header | Record<string, string> | No | Header information in the HTTP response. The application can intersect the header fields with the fields supported by the underlying layer for parsing or directly pass in all corresponding header information.<br> - The following fields need to be parsed by the underlying player: Transfer-Encoding, Location, Content-Type, Content-Range, Content-Encode, Accept-Ranges, and content-length.|
84| redirectUrl | string | No | 	Redirect URL in the HTTP response.|
85
86**Example**
87
88```ts
89import { HashMap } from '@kit.ArkTS';
90let requests: HashMap<number, media.MediaSourceLoadingRequest> = new HashMap();
91let uuid = 1;
92
93// The application fills this in as needed.
94let header:Record<string, string> = {
95  'Transfer-Encoding':'xxx',
96  'Location' : 'xxx',
97  'Content-Type' : 'xxx',
98  'Content-Range' : 'xxx',
99  'Content-Encode' : 'xxx',
100  'Accept-Ranges' : 'xxx',
101  'content-length' : 'xxx'
102};
103let request = requests.get(uuid);
104request?.respondHeader(uuid, header);
105```
106
107## finishLoading<sup>18+</sup>
108
109finishLoading(uuid: number, state: LoadingRequestError): void
110
111Notifies the player of the current request status. After pushing all the data for a single resource, the application should send the **LOADING_ERROR_SUCCESS** state to notify the player that the resource push is complete.
112
113**Atomic service API**: This API can be used in atomic services since API version 18.
114
115**System capability**: SystemCapability.Multimedia.Media.Core
116
117**Parameters**
118
119| Name  | Type    | Mandatory| Description                |
120| -------- | -------- | ---- | -------------------- |
121| uuid | number | Yes | 	ID for the resource handle. The source is [SourceOpenCallback](arkts-apis-media-t.md#sourceopencallback18).|
122| state  | [LoadingRequestError](arkts-apis-media-e.md#loadingrequesterror18) | Yes | Request status.|
123
124**Example**
125
126```ts
127import { HashMap } from '@kit.ArkTS';
128
129let requests: HashMap<number, media.MediaSourceLoadingRequest> = new HashMap();
130let uuid = 1;
131
132let request = requests.get(uuid);
133let loadingError = media.LoadingRequestError.LOADING_ERROR_SUCCESS;
134request?.finishLoading(uuid, loadingError);
135```
136