• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Local AVSession Overview
2<!--Kit: AVSession Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @ccfriend; @liao_qian-->
5<!--SE: @ccfriend-->
6<!--TSE: @chenmingxi1_huawei-->
7
8## Interaction Process
9
10For a local AVSession, the data sources are on the local device. The figure below illustrates the interaction process.
11
12![Local AVSession Interaction Process](figures/local-avsession-interaction-process.png)
13
14This process involves two roles: provider and controller.
15
16> **NOTE**
17>
18> The controller must be a system application. A third-party application can be a provider.
19
20In the local AVSession, the provider exchanges information with the controller through AVSessionManager.
21
221. The provider creates an AVSession object through AVSessionManager.
23
242. 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).
25
263. The controller creates an AVSessionController object through AVSessionManager.
27
284. Through the AVSessionController object, the controller listens for changes of the session metadata and playback attributes.
29
305. Through the AVSessionController object, the controller sends control commands to the AVSession object.
31
326. Through the AVSession object, the provider listens for the control commands, for example, play, playNext, fastForward, and setSpeed, from the controller.
33
34## AVSessionManager
35
36AVSessionManager 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.
37
38Unlike the AVSession and AVSessionController objects, AVSessionManager is not a specific object, but the root namespace of AVSessions. You can import AVSessionManager as follows:
39
40```ts
41import { avSession as AVSessionManager } from '@kit.AVSessionKit';
42```
43
44All the APIs in the root namespace can be used as APIs of AVSessionManager.
45
46The code snippet below shows how the provider creates an AVSession object by using AVSessionManager:
47
48> **NOTE**
49>
50> The sample code below demonstrates only the API call for creating an AVSession object. When actually using it, the application must ensure that the AVSession object remains throughout the application's background playback activities. This prevents the system from reclaiming or releasing it, which could lead to playback being controlled by the system.
51
52```ts
53import { BusinessError } from '@kit.BasicServicesKit';
54import { avSession } from '@kit.AVSessionKit';
55import { avSession as AVSessionManager } from '@kit.AVSessionKit';
56
57@Entry
58@Component
59struct Index {
60  @State message: string = 'hello world';
61
62  build() {
63    Column() {
64      Text(this.message)
65        .onClick(async () => {
66          // Create an AVSession object.
67          let context = this.getUIContext().getHostContext() as Context;
68          let session: AVSessionManager.AVSession = await AVSessionManager.createAVSession(context, 'SESSION_NAME', 'audio');
69          console.info(`session create done : sessionId : ${session.sessionId}`);
70        })
71    }
72    .width('100%')
73    .height('100%')
74  }
75}
76```
77<!--Del-->
78The code snippet below shows how the controller creates an AVSessionController object by using AVSessionManager:
79
80```ts
81// Create an AVSessionController object.
82async function createController() {
83  // Obtain the descriptors of all live AVSession objects.
84  let descriptorsArray: Array<AVSessionManager.AVSessionDescriptor> = await AVSessionManager.getAllSessionDescriptors();
85  if (descriptorsArray.length > 0) {
86    // For demonstration, the session ID of the first descriptor is used to create the AVSessionController object.
87    let sessionId: string = descriptorsArray[0].sessionId;
88    let avSessionController: AVSessionManager.AVSessionController = await AVSessionManager.createController(sessionId);
89    console.info(`controller create done : sessionId : ${avSessionController.sessionId}`);
90  }
91}
92```
93<!--DelEnd-->
94
95For more information about AVSessionManager APIs, see [API Reference](../../reference/apis-avsession-kit/arkts-apis-avsession.md).
96