HarmonyOS鸿蒙Next中想实现点击触发音效,但是根据指南写出来的代码,一直没声音无法实现
HarmonyOS鸿蒙Next中想实现点击触发音效,但是根据指南写出来的代码,一直没声音无法实现 想实现点击触发音效,但是根据指南写出来的代码,一直没声音无法实现。求助!代码附在下面:
Index.ets
import { MyPreferencesUtil } from '../MyPreferencesUtil'
import { AVPlayerDemo } from '../AVPlayerDemo'
@Entry
@Component
struct Index {
@State Food:string = ''
@State array:Array<string> = MyPreferencesUtil.getInstance().mUserInfo.array //['烤冷面','煎饼果子','粥','包子','芙蓉卷','寿司','兰州拉面','馋嘴鱼','香肉拌饭','馄饨','盅盅面','米村拌饭','米线','清补凉','螺蛳粉'];
@State num:number = Math.floor(Math.random()*this.array.length);
@State flag:Boolean = false
@State currentIndex: number = 0;
@State value_sub:string=''
@State value_de:string=''
@State display_a:string = ''
avp: AVPlayerDemo = new AVPlayerDemo()
private tabsController: TabsController = new TabsController();
@Builder TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
Column() {
Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
.size({ width: 25, height: 25 })
Text(title)
.fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
}
.width('100%')
.height(50)
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.currentIndex = targetIndex;
this.tabsController.changeIndex(this.currentIndex);
})
}
build() {
Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
TabContent() {
Column(){
Button('启动')
.width("180vp")
.height("80vp")
.offset({ x: "0vp", y: "500vp" })
.onClick(()=>{
this.num = Math.floor(Math.random()*this.array.length);
this.display_a = this.array[this.num]
this.avp.avPlayerFdSrcDemo()
})
Text(this.display_a)
.fontSize(70)
.width("500vp")
.height("100vp")
.offset({ x: "0vp", y: "200vp" })
.textAlign(TextAlign.Center)
}.width('100%').height('100%').backgroundColor('#FFFFFF')
}
.tabBar(this.TabBuilder('大转盘', 0, $r('app.media.up'), $r('app.media.down')))
TabContent() {
Column(){
TextInput({ placeholder: '请输入想要添加的菜品' }).margin({top:20,bottom:10})
.onChange((value:string)=>{
this.value_sub = value
//this.array.push(value)
})
.borderRadius(10)
Button('菜品提交')
.onClick(()=>{
this.array.push(this.value_sub)
MyPreferencesUtil.saveData()
})
.margin({top:10,bottom:10})
TextInput({ placeholder: '请输入想要删除的菜品' }).margin({top:20,bottom:10})
.onChange((value:string)=>{
this.value_de = value
})
.borderRadius(10)
Button('菜品删除')
.onClick(()=>{
if(this.array.indexOf(this.value_de) === -1){
}
else{
this.array.splice(this.array.indexOf(this.value_de),1)
MyPreferencesUtil.saveData()
}
})
.margin({top:10,bottom:10})
List({ space: this.array.length }) {
ForEach(this.array, (item: number) =>{
ListItem() {
Text(`${item}`)
.width('50%')
.height(50)
.fontSize(20)
.fontColor(Color.White)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0x007DFF)
.offset({y: '400'})
}
}, item => item)
}.listDirection(Axis.Horizontal).margin({top:-20})
}.width('100%').height('100%').backgroundColor('#FFFFFF')
}
.tabBar(this.TabBuilder('设置', 1, $r('app.media.up'), $r('app.media.down')))
}
.barWidth('100%')
.barHeight(50)
.onChange((index: number) => {
this.currentIndex = index;
})
}
}
AVPlayerDemo.ets
import media from '@ohos.multimedia.media';
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
export class AVPlayerDemo {
private avPlayer;
private isSeek: boolean = true; // 用于区分模式是否支持seek操作
private count: number = 0;
// 注册avplayer回调函数
setAVPlayerCallback() {
// seek操作结果回调函数
this.avPlayer.on('seekDone', (seekDoneTime) => {
console.info(`AVPlayer seek succeeded, seek time is ${seekDoneTime}`);
})
// error回调监听函数,当avPlayer在操作过程中出现错误时调用reset接口触发重置流程
this.avPlayer.on('error', (err) => {
console.error(`Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`);
this.avPlayer.reset(); // 调用reset重置资源,触发idle状态
})
// 状态机变化回调函数
this.avPlayer.on('stateChange', async (state, reason) => {
switch (state) {
case 'idle': // 成功调用reset接口后触发该状态机上报
console.info('AVPlayer state idle called.');
this.avPlayer.release(); // 调用release接口销毁实例对象
break;
case 'initialized': // avplayer 设置播放源后触发该状态上报
console.info('AVPlayerstate initialized called.');
this.avPlayer.prepare().then(() => {
console.info('AVPlayer prepare succeeded.');
}, (err) => {
console.error(`Invoke prepare failed, code is ${err.code}, message is ${err.message}`);
});
break;
case 'prepared': // prepare调用成功后上报该状态机
console.info('AVPlayer state prepared called.');
this.avPlayer.play(); // 调用播放接口开始播放
break;
case 'playing': // play成功调用后触发该状态机上报
console.info('AVPlayer state playing called.');
if (this.count !== 0) {
console.info('AVPlayer start to seek.');
this.avPlayer.seek(this.avPlayer.duration); //seek到音频末尾
} else {
this.avPlayer.pause(); // 调用暂停接口暂停播放
}
this.count++;
break;
case 'paused': // pause成功调用后触发该状态机上报
console.info('AVPlayer state paused called.');
this.avPlayer.play(); // 再次播放接口开始播放
break;
case 'completed': // 播放结束后触发该状态机上报
console.info('AVPlayer state completed called.');
this.avPlayer.stop(); //调用播放结束接口
break;
case 'stopped': // stop接口成功调用后触发该状态机上报
console.info('AVPlayer state stopped called.');
this.avPlayer.reset(); // 调用reset接口初始化avplayer状态
break;
case 'released':
console.info('AVPlayer state released called.');
break;
default:
console.info('AVPlayer state unknown called.');
break;
}
})
}
// 以下demo为使用fs文件系统打开沙箱地址获取媒体文件地址并通过url属性进行播放示例
async avPlayerUrlDemo() {
// 创建avPlayer实例对象
this.avPlayer = await media.createAVPlayer();
// 创建状态机变化回调函数
this.setAVPlayerCallback();
let fdPath = 'fd://';
// 通过UIAbilityContext获取沙箱地址filesDir,以下为Stage模型获方式,如需在FA模型上获取请参考《访问应用沙箱》获取地址
let context = getContext(this) as common.UIAbilityContext;
let pathDir = context.filesDir;
let path = pathDir + '/01.mp3';
// 打开相应的资源文件地址获取fd,并为url赋值触发initialized状态机上报
let file = await fs.open(path);
fdPath = fdPath + '' + file.fd;
this.avPlayer.url = fdPath;
}
// 以下demo为使用资源管理接口获取打包在HAP内的媒体资源文件并通过fdSrc属性进行播放示例
async avPlayerFdSrcDemo() {
// 创建avPlayer实例对象
this.avPlayer = await media.createAVPlayer();
// 创建状态机变化回调函数
this.setAVPlayerCallback();
// 通过UIAbilityContext的resourceManager成员的getRawFd接口获取媒体资源播放地址
// 返回类型为{fd,offset,length},fd为HAP包fd地址,offset为媒体资源偏移量,length为播放长度
let context = getContext(this) as common.UIAbilityContext;
let fileDescriptor = await context.resourceManager.getRawFd('1.mp3');
// 为fdSrc赋值触发initialized状态机上报
this.avPlayer.fdSrc = fileDescriptor;
this.isSeek = false; // 不支持seek操作
}
}
更多关于HarmonyOS鸿蒙Next中想实现点击触发音效,但是根据指南写出来的代码,一直没声音无法实现的实战教程也可以访问 https://www.itying.com/category-93-b0.html
4 回复
这段示例代码有点问题,启动播放以后会直接停止播放。
把红框内的diamagnetic注释掉就能正常播放了

更多关于HarmonyOS鸿蒙Next中想实现点击触发音效,但是根据指南写出来的代码,一直没声音无法实现的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
可以了!
在HarmonyOS鸿蒙Next中实现点击触发音效,首先确保你已经正确导入了音频资源,并在config.json
中声明了音频权限。检查音频文件路径是否正确,确保文件格式支持(如.mp3
、.wav
)。使用AudioPlayer
或SoundPool
播放音效时,确保在点击事件中正确调用了播放方法。如果仍无声音,检查设备音量设置或调试日志,确认是否有错误信息。