• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimedia.systemSoundManager (系统声音管理)
2
3系统声音管理提供管理系统声音的一些基础能力,包括对系统铃声的资源设置与读取、获取系统铃声播放器等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块接口为系统接口。
10
11## 导入模块
12
13```ts
14import systemSoundManager from '@ohos.multimedia.systemSoundManager';
15```
16
17## RingtoneType
18
19枚举,铃声类型。
20
21**系统接口:** 该接口为系统接口。
22
23**系统能力:** SystemCapability.Multimedia.SystemSound.Core
24
25| 名称                            |  值     | 说明                                         |
26| ------------------------------- | ------ | -------------------------------------------- |
27| RINGTONE_TYPE_DEFAULT           | 0      | 默认铃声类型。                                 |
28| RINGTONE_TYPE_MULTISIM          | 1      | 多SIM卡铃声类型。                              |
29
30## systemSoundManager.getSystemSoundManager
31
32getSystemSoundManager(): SystemSoundManager
33
34获取系统声音管理器。
35
36**系统接口:** 该接口为系统接口
37
38**系统能力:** SystemCapability.Multimedia.SystemSound.Core
39
40**返回值:**
41
42| 类型                          | 说明         |
43| ----------------------------- | ------------ |
44| [SystemSoundManager](#systemsoundmanager) | 系统声音管理类。 |
45
46**示例:**
47```ts
48let systemSoundManagerInstance: systemSoundManager.SystemSoundManager = systemSoundManager.getSystemSoundManager();
49```
50
51## SystemSoundManager
52
53管理系统声音。在调用SystemSoundManager的接口前,需要先通过[getSystemSoundManager](#systemsoundmanagergetsystemsoundmanager)创建实例。
54
55### setSystemRingtoneUri
56
57setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType, callback: AsyncCallback<void>): void
58
59设置系统铃声uri,使用callback方式异步返回结果。
60
61**系统接口:** 该接口为系统接口
62
63**系统能力:** SystemCapability.Multimedia.SystemSound.Core
64
65**参数:**
66
67| 参数名   | 类型                                      | 必填 | 说明                     |
68| -------- | ---------------------------------------- | ---- | ------------------------ |
69| context  | Context                                  | 是   | 当前应用的上下文。           |
70| uri      | string                                   | 是   | 被设置的系统铃声的uri,资源支持可参考[media.AVPlayer](js-apis-media.md#avplayer9)。 |
71| type     | [RingtoneType](#ringtonetype)            | 是   | 被设置的系统铃声的类型。     |
72| callback | AsyncCallback<void>                | 是   | 回调返回设置成功或失败。     |
73
74**示例:**
75
76```ts
77import { BusinessError } from '@ohos.base';
78
79let context: Context = getContext(this);
80let uri = 'file://data/test.wav'; // 需更改为目标铃声文件的uri
81let type: systemSoundManager.RingtoneType = systemSoundManager.RingtoneType.RINGTONE_TYPE_DEFAULT;
82
83systemSoundManagerInstance.setSystemRingtoneUri(context, uri, type, (err: BusinessError) => {
84  if (err) {
85    console.error(`Failed to set system ringtone uri. ${err}`);
86    return;
87  }
88  console.info(`Callback invoked to indicate a successful setting of the system ringtone uri.`);
89});
90```
91
92### setSystemRingtoneUri
93
94setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType): Promise<void>
95
96设置系统铃声uri,使用Promise方式异步返回结果。
97
98**系统接口:** 该接口为系统接口
99
100**系统能力:** SystemCapability.Multimedia.SystemSound.Core
101
102**参数:**
103
104| 参数名   | 类型                                      | 必填 | 说明                     |
105| -------- | ---------------------------------------- | ---- | ------------------------ |
106| context  | Context                                  | 是   | 当前应用的上下文。         |
107| uri      | string                                   | 是   | 被设置的系统铃声的uri,资源支持可参考[media.AVPlayer](js-apis-media.md#avplayer9)。 |
108| type     | [RingtoneType](#ringtonetype)            | 是   | 被设置的系统铃声的类型。   |
109
110**返回值:**
111
112| 类型                | 说明                            |
113| ------------------- | ------------------------------- |
114| Promise<void> | Promise回调返回设置成功或失败。   |
115
116**示例:**
117
118```ts
119import { BusinessError } from '@ohos.base';
120
121let context: Context = getContext(this);
122let uri = 'file://data/test.wav'; // 需更改为目标铃声文件的uri
123let type: systemSoundManager.RingtoneType = systemSoundManager.RingtoneType.RINGTONE_TYPE_DEFAULT;
124
125systemSoundManagerInstance.setSystemRingtoneUri(context, uri, type).then(() => {
126  console.info(`Promise returned to indicate a successful setting of the system ringtone uri.`);
127}).catch ((err: BusinessError) => {
128  console.error(`Failed to set the system ringtone uri ${err}`);
129});
130```
131
132### getSystemRingtoneUri
133
134getSystemRingtoneUri(context: Context, type: RingtoneType, callback: AsyncCallback<string>): void
135
136获取系统铃声uri,使用callback方式异步返回结果。
137
138**系统接口:** 该接口为系统接口
139
140**系统能力:** SystemCapability.Multimedia.SystemSound.Core
141
142**参数:**
143
144| 参数名   | 类型                                      | 必填 | 说明                     |
145| -------- | ---------------------------------------- | ---- | ------------------------ |
146| context  | Context                                  | 是   | 当前应用的上下文。         |
147| type     | [RingtoneType](#ringtonetype)            | 是   | 待获取的系统铃声的类型。    |
148| callback | AsyncCallback<string>              | 是   | 回调返回获取的系统铃声uri。 |
149
150**示例:**
151
152```ts
153import { BusinessError } from '@ohos.base';
154
155let context: Context = getContext(this);
156let type: systemSoundManager.RingtoneType = systemSoundManager.RingtoneType.RINGTONE_TYPE_DEFAULT;
157
158systemSoundManagerInstance.getSystemRingtoneUri(context, type, (err: BusinessError, value: string) => {
159  if (err) {
160    console.error(`Failed to get system ringtone uri. ${err}`);
161    return;
162  }
163  console.info(`Callback invoked to indicate the value of the system ringtone uri is obtained ${value}.`);
164});
165```
166
167### getSystemRingtoneUri
168
169getSystemRingtoneUri(context: Context, type: RingtoneType): Promise<string>
170
171获取系统铃声uri,使用Promise方式异步返回结果。
172
173**系统接口:** 该接口为系统接口
174
175**系统能力:** SystemCapability.Multimedia.SystemSound.Core
176
177**参数:**
178
179| 参数名   | 类型                                      | 必填 | 说明                     |
180| -------- | ---------------------------------------- | ---- | ------------------------ |
181| context  | Context                                  | 是   | 当前应用的上下文。         |
182| type     | [RingtoneType](#ringtonetype)            | 是   | 被设置的系统铃声的类型。   |
183
184**返回值:**
185
186| 类型                | 说明                                |
187| ------------------- | ---------------------------------- |
188| Promise<string> | Promise回调返回获取的系统铃声uri。 |
189
190**示例:**
191
192```ts
193import { BusinessError } from '@ohos.base';
194
195let context: Context = getContext(this);
196let type: systemSoundManager.RingtoneType = systemSoundManager.RingtoneType.RINGTONE_TYPE_DEFAULT;
197
198systemSoundManagerInstance.getSystemRingtoneUri(context, type).then((value: string) => {
199  console.info(`Promise returned to indicate that the value of the system ringtone uri is obtained ${value}.`);
200}).catch ((err: BusinessError) => {
201  console.error(`Failed to get the system ringtone uri ${err}`);
202});
203```
204
205### getSystemRingtonePlayer
206
207getSystemRingtonePlayer(context: Context, type: RingtoneType, callback: AsyncCallback<RingtonePlayer>): void
208
209获取系统铃声播放器,使用callback方式异步返回结果。
210
211**系统接口:** 该接口为系统接口
212
213**系统能力:** SystemCapability.Multimedia.SystemSound.Core
214
215**参数:**
216
217| 参数名   | 类型                                      | 必填 | 说明                         |
218| -------- | -----------------------------------------| ---- | --------------------------- |
219| context  | Context                                  | 是   | 当前应用的上下文。            |
220| type     | [RingtoneType](#ringtonetype)            | 是   | 待获取播放器的系统铃声的类型。 |
221| callback | AsyncCallback<[RingtonePlayer](js-apis-inner-multimedia-ringtonePlayer.md#ringtoneplayer)> | 是 | 回调返回获取的系统铃声播放器。 |
222
223**示例:**
224
225```ts
226import { BusinessError } from '@ohos.base';
227
228let context: Context = getContext(this);
229let type: systemSoundManager.RingtoneType = systemSoundManager.RingtoneType.RINGTONE_TYPE_DEFAULT;
230let systemRingtonePlayer: systemSoundManager.RingtonePlayer | undefined = undefined;
231
232systemSoundManagerInstance.getSystemRingtonePlayer(context, type, (err: BusinessError, value: systemSoundManager.RingtonePlayer) => {
233  if (err) {
234    console.error(`Failed to get system ringtone player. ${err}`);
235    return;
236  }
237  console.info(`Callback invoked to indicate the value of the system ringtone player is obtained.`);
238  systemRingtonePlayer = value;
239});
240```
241
242### getSystemRingtonePlayer
243
244getSystemRingtonePlayer(context: Context, type: RingtoneType): Promise<RingtonePlayer>
245
246获取系统铃声播放器,使用Promise方式异步返回结果。
247
248**系统接口:** 该接口为系统接口
249
250**系统能力:** SystemCapability.Multimedia.SystemSound.Core
251
252**参数:**
253
254| 参数名   | 类型                                      | 必填 | 说明                         |
255| -------- | -----------------------------------------| ---- | --------------------------- |
256| context  | Context                                  | 是   | 当前应用的上下文。            |
257| type     | [RingtoneType](#ringtonetype)            | 是   | 待获取播放器的系统铃声的类型。 |
258
259**返回值:**
260
261| 类型                | 说明                            |
262| ------------------- | ------------------------------- |
263| Promise<[RingtonePlayer](js-apis-inner-multimedia-ringtonePlayer.md#ringtoneplayer)> | Promise回调返回获取的系统铃声播放器。 |
264
265**示例:**
266
267```ts
268import { BusinessError } from '@ohos.base';
269
270let context: Context = getContext(this);
271let type: systemSoundManager.RingtoneType = systemSoundManager.RingtoneType.RINGTONE_TYPE_DEFAULT;
272let systemRingtonePlayer: systemSoundManager.RingtonePlayer | undefined = undefined;
273
274systemSoundManagerInstance.getSystemRingtonePlayer(context, type).then((value: systemSoundManager.RingtonePlayer) => {
275  console.info(`Promise returned to indicate that the value of the system ringtone player is obtained.`);
276  systemRingtonePlayer = value;
277}).catch ((err: BusinessError) => {
278  console.error(`Failed to get the system ringtone player ${err}`);
279});
280```
281