目录

系统音量

接口声明

在manifest.json文件的 features属性中增加如下配置。

{"name": "system.volume"}

导入模块

在调用接口页面的<script>部分增加如下配置。

import volume from '@system.volume'

或

var volume = require("@system.volume")

使用限制

限制条件

说明

适用终端

手机、平板、智慧屏、车机

适用区域

全球

接口定义

接口

描述

volume.getMediaValue(OBJECT)

获取当前多媒体音量。

volume.setMediaValue(OBJECT)

设置当前多媒体音量。

[h2]volume.getMediaValue(OBJECT)

描述

获取当前多媒体音量。

OBJECT参数

参数

类型

是否必填

说明

success

function

否

成功回调。

fail

function

否

失败回调。

complete

function

否

执行结束后的回调。

success返回值:

参数名

类型

说明

value

number

系统媒体当前音量,0.0 ~ 1.0之间。

[h2]volume.setMediaValue(OBJECT)

描述

设置当前多媒体音量。

OBJECT参数

参数

类型

是否必填

说明

value(1077+)

number

是

设置的音量,0.0 ~ 1.0之间。

success

function

否

成功回调。

fail

function

否

失败回调。

complete

function

否

执行结束后的回调。

fail返回错误码:

错误码

说明

200

一般性错误。

202

参数非法。

Demo

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <div class="case-title">
      <text class="title">setMediaVolume:{{volumeNum}}</text>
    </div>
    <div class="mlr-container">
      <slider class="slider" id="slider" min="0" value="{{volumeNum}}" max="100" step="1" onchange="updateVolume"></slider>
    </div>

     <div class="case-title ">
      <text class="title">getMediaVolume:{{getvolumeNum}}</text>
     </div>


     <div class="item-container">
      <input onclick="getMediaVolume" type="button" value="Get Media Volume" class="btn-blue" />
    </div>

  </div>
</template>

<style lang="sass">
.container {
   flex: 1;
  flex-direction: column;
}
.case-title {
  margin-left: 32px;
  margin-right: 32px;
  margin-bottom: 20px;
  flex-direction: column;
  margin-top: 40px;
}
.title {
  color: rgba(0, 0, 0, 0.6);
  font-size: 28px;
  font-weight: 500;
}
.slider {
  color: rgba(0, 0, 0, 0.1);
  selected-color: #0faeff;
  block-color: #0faeff;
  padding-left: 0;
  padding-right: 0;
}
.mlr-container {
  flex-direction: column;
  margin-right: 32px;
  margin-left: 32px;
}
.btn-blue {
    background-color: #0faeff;
    color: #ffffff; 
    width:400px;
    height: 70px;
    font-size: 40px;
    border-radius:15px;
    align-items: center;
    margin-right: 60px;
    margin-left: 60px;
  }
  .item-container {
    margin-bottom: 50px;
    margin-right: 60px;
    margin-left: 60px;
    flex-direction: column;
    align-items: center;
  }
</style>

<script>
  import volume from '@system.volume';
  import prompt from '@system.prompt';
  module.exports = {
    public: {
      volumeNum: 0,
      volumeInted:false,
      getvolumeNum:''
    },
    onInit: function () {
      this.$page.setTitleBar({ text: 'system volume' });

      let that = this;
      volume.getMediaValue({
        success: function (ret) {
          that.volumeNum = ret.value * 100;
          setTimeout(() => {
            that.volumeInted = true;
          },500);
        }
      })
    },
    updateVolume({ progress }) {
      if(!this.volumeInted)
      {
        return;
      }
      this.volumeNum = progress;
      volume.setMediaValue({
        volume: progress / 100,
        success: function (data) {
        },
        fail: function (data, code) {
          prompt.showToast({
            message: 'fail',
            duration: 2000,
            gravity: 'bottom'
          })
        }
      })
    },
    getMediaVolume:function() {
      let that = this;
      volume.getMediaValue({
        success: function (ret) {
          that.getvolumeNum = ret.value * 100;
          setTimeout(() => {
            that.volumeInted = true;
          },500);
        }
      })
    }
  }
</script>

效果图如下: