• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Local AVSession Overview
2
3## Interaction Process
4
5For a local AVSession, the data sources are on the local device. The figure below illustrates the interaction process.
6
7![Local AVSession Interaction Process](figures/local-avsession-interaction-process.png)
8
9This process involves two roles: provider and controller.
10
11> **NOTE**
12>
13> The controller must be a system application. A third-party application can be a provider.
14
15In the local AVSession, the provider exchanges information with the controller through AVSessionManager.
16
171. The provider creates an **AVSession** object through AVSessionManager.
18
192. Through the **AVSession** object, the provider sets session metadata (such as the asset ID, title, and duration) and playback attributes (such as the playback state, speed, and position).
20
213. The controller creates an **AVSessionController** object through AVSessionManager.
22
234. Through the **AVSessionController** object, the controller listens for changes of the session metadata and playback attributes.
24
255. Through the **AVSessionController** object, the controller sends control commands to the **AVSession** object.
26
276. Through the **AVSession** object, the provider listens for the control commands, for example, play, playNext, fastForward, and setSpeed, from the controller.
28
29## AVSessionManager
30
31AVSessionManager provides the capability of managing sessions. It can create an **AVSession** object, create an **AVSessionController** object, send control commands, and listen for session state changes.
32
33Unlike the **AVSession** and **AVSessionController** objects, AVSessionManager is not a specific object, but the root namespace of AVSessions. You can import AVSessionManager as follows:
34
35```ts
36import AVSessionManager from '@ohos.multimedia.avsession';
37```
38
39All the APIs in the root namespace can be used as APIs of AVSessionManager.
40
41The code snippet below shows how the provider creates an **AVSession** object by using AVSessionManager:
42
43```ts
44// Create an AVSession object.
45let context: Context = getContext(this);
46async function createSession() {
47  let session: AVSessionManager.AVSession = await AVSessionManager.createAVSession(context, 'SESSION_NAME', 'audio');
48  console.info(`session create done : sessionId : ${session.sessionId}`);
49}
50```
51
52The code snippet below shows how the controller creates an **AVSessionController** object by using AVSessionManager:
53
54```ts
55// Create an AVSessionController object.
56async function createController() {
57  // Obtain the descriptors of all live AVSession objects.
58  let descriptorsArray: Array<AVSessionManager.AVSessionDescriptor> = await AVSessionManager.getAllSessionDescriptors();
59  if (descriptorsArray.length > 0) {
60    // For demonstration, the session ID of the first descriptor is used to create the AVSessionController object.
61    let sessionId: string = descriptorsArray[0].sessionId;
62    let avSessionController: AVSessionManager.AVSessionController = await AVSessionManager.createController(sessionId);
63    console.info(`controller create done : sessionId : ${avSessionController.sessionId}`);
64  }
65}
66```
67
68For more information about AVSessionManager APIs, see [API Reference](../reference/apis-avsession-kit/js-apis-avsession.md).
69