音频流是指音频系统中一个具备音频格式和音频使用场景信息的独立音频数据处理单元。可以表示播放,也可以表示录制,并且具备独立音量调节和音频设备路由切换能力。对于播放流,其类型由StreamUsage确定;对于录制流,则由SourceType决定。
查看日志,搜索关键字StreamUsage,查看应用使用的音频流类型。如下面日志所示,应用设置的StreamUsage值为4,对应音频流类型为闹钟。当StreamUsage的值不为6(铃声)或7(通知音)时,在设备处于静音模式下,系统仍会发出提示音。仅当StreamUsage为6或7时,在静音模式下不会播放提示音。
03-06 16:38:15.457 782-2437 C02B85/audio_server/AudioEffectChain audio_server I [SetEffectParamToHandle]set param to handle, sceneType: 5, effectMode: 1, rotation: 0, volume: 700, extraSceneType: 0, spatialDeviceType: 5, spatializationSceneType: 1, spatializationEnabled: 0, streamUsage: 4,absVolumeState = 1
根据业务场景使用合适的音频流。设置音频流类型为STREAM_USAGE_RINGTONE(铃声类型)或STREAM_USAGE_NOTIFICATION(通知音),静音模式下应用将不会发出提示音。
import { media } from '@kit.MediaKit';
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct NotificationSoundPlayer {
private soundPool: media.SoundPool | undefined = undefined;
private soundId: number = -1;
// 创建SoundPool实例并加载音频
async initSoundPool() {
try {
// 设置音频流类型为铃声或通知音,仅当usage设置为STREAM_USAGE_RINGTONE和STREAM_USAGE_NOTIFICATION时可以实现静音
const audioRendererInfo: audio.AudioRendererInfo = {
usage: audio.StreamUsage.STREAM_USAGE_RINGTONE, // 设置为铃声类型
rendererFlags: 0
};
// 创建SoundPool实例(最大并发数8)
this.soundPool = await media.createSoundPool(8, audioRendererInfo);
// 注册音频加载完成回调
this.soundPool.on('loadComplete', (soundId: number) => {
this.soundId = soundId;
console.info(`Sound loaded ID: ${soundId}`);
});
// 从资源文件加载音频
const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const fileDescriptor =
await context.resourceManager.getRawFd('notification.mp3'); // 此处'notification'仅作示例,请开发者自行替换
this.soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length);
} catch (error) {
console.error(`Init failed: ${(error as BusinessError).message}`);
}
}
// 播放提示音
playNotification() {
if (!this.soundPool || this.soundId === -1) {
return;
}
const playParams: media.PlayParameters = {
loop: 0, // 不循环
rate: audio.AudioRendererRate.RENDER_RATE_NORMAL,
leftVolume: 1.0, // 左声道音量
rightVolume: 1.0, // 右声道音量
};
try {
this.soundPool.play(this.soundId, playParams);
} catch (error) {
console.error(`Play failed: ${(error as BusinessError).message}`);
}
}
// 组件初始化时加载音频
aboutToAppear() {
this.initSoundPool();
}
// 组件销毁时释放资源
aboutToDisappear() {
this.soundPool?.release();
}
build() {
Column() {
Button('播放提示音')
.onClick(() => this.playNotification())
.margin(20)
}
}
}