npm i --save @kefah/nativescript-sound
- 版本: 1.0.1
- GitHub: https://github.com/NativeScript/plugins
- NPM: https://npmjs.net.cn/package/%40kefah%2Fnativescript-sound
- 下载
- 昨天: 2
- 上周: 2
- 上个月: 7
@kefah/nativescript-sound
这是一个用于播放游戏短音效的插件
限制:Android 应用只能播放小于 1MB(5.6 秒)的音效。不要使用此插件播放大型音频,它不是为此目的而制作的
npm install @kefah/nativescript-sound
使用方法
不要伪造 Webpack!
const config = webpack.resolveConfig();
const customCopyInstance = new CopyWebpackPlugin({
patterns: [
...
{ from: 'sounds/*.mp3'}, // put here the path to the s-audio file
],
});
config.plugins.push(customCopyInstance);
return config;
使用它 :-)
import { NativescriptSound, SoundPathInterface } from '@kefah/nativescript-sound';
/**
* Do not forged to configure webpack for your audio file
* */
const appPath: string = knownFolders.currentApp().path;
const soundsPath = path.join(appPath, "sounds");
this.sounds = {
"success": {
path: path.join(soundsPath, "card_palyed_1.mp3")
},
"msg": {
path: path.join(soundsPath, "card_played_2.mp3")
},
"success_1": {
path: path.join(soundsPath, "card_played.mp3")
},
"shuffle": {
path: path.join(soundsPath, "card_shuffle.mp3")
},
"wrong": {
path: path.join(soundsPath, "card_swipe.mp3")
}
};
this.player = new NativescriptSound(10)
// Prepare all the audio to be played later
this.player.prepare({
paths: this.sounds,
onLoadCompleted: (player, sampleId, status) => {
console.log(`${sampleId} with status ${status}`)
}
});
// ...
// Later on the game, play the sound
this.player.play({
name: "success"
});
API
名称 | IOS | ANDROID | 类型 | 描述 |
prepare | X | X | paths: SoundsOptions | 准备一个将在游戏后播放的音频列表 |
play | X | X | playOptions: PlayOptions | 播放特定音频 |
stop | X | X | soundName: 字符串 | 通过音名字符串停止音频 |
pause | X | X | soundName: 字符串 | 暂停音频 |
resume | X | soundName: 字符串 | 恢复音频 | |
setVolume | X | X | soundName: 字符串 | 设置音频的音量 |
setRate | X | X | soundName: 字符串 | 设置音频的速度 |
unload | X | soundName: 字符串 | 卸载音频,调用此方法后需要新实例来重新加载音频 | |
releaseAll | X | 卸载所有音频,调用此方法后需要新实例来重新加载音频 |
许可证
Apache 许可证第 2 版