nativeScript-volume
NativeScript 插件,允许您通过编程方式更改设备音量。
npm i --save nativescript-volume

NativeScript-Volume

此插件允许您在 NativeScript 应用程序中通过编程方式控制音量。它与 iOS 和 Android 兼容。

安装

在项目的根目录中运行以下命令

tns plugin add nativescript-volume

此命令将自动安装必要的文件,并将 nativescript-volume 作为依赖项存储在项目 package.json 文件中。

使用方法

此插件可以与任何类型的 NativeScript(核心、Angular、Vue、TypeScript 和/或 JavaScript)一起使用。

TypeScript

import { Volume } from 'nativescript-volume';

export SomeClass {
private volume: Volume;

constructor() {
this.volume = new Volume();
}

mute() {
this.volume.mute();
}

unmute() {
this.volume.unmute();
}

volumeUp() {
this.volume.volumeUp();
}

volumeHalf() {
if (app.ios) {
// iOS Volume goes from 0 to 1. With its increments being 1/16.
this.volume.setVolume(0.5);
} else if (app.android) {
// Android Volume I'm unsure of the range, but believe it to be 0 to 15 at least for the music stream.
this.volume.setVolume(7);
}
}

volumeDown() {
this.volume.volumeDown();
}

getVolume() {
// Please see Android TODO in the Api Section
alert('
Volume is' + this.volume.getVolume());
}
}

API

方法:ReturnType 描述 注意
mute(): void 静音设备音量
unmute(): void 取消静音设备音量
volumeDown(): void 降低设备音量。 Android 设备选择活动声音流。
volumeUp(): void 增加设备音量。 Android 设备选择活动声音流。
getVolume(): number 获取设备当前音量。 Android 目前这仅获取音乐流音量。
setVolume(value: number): void 设置设备当前音量。 Android 目前这仅设置音乐流音量。

许可证

MIT 许可证