我们用android经常使用的时候,手机的声音增大和缩小操作。在设定场景模式,它往往使用静音和振动运行,这通常是AudioManager来控制的。

今天我们就来看一下AudioManager
的使用。

首先要想操作声音必须取得这个服务。在前面我们学过取得系统服务的方法

AudioManager audio = (AudioManager) super.getSystemService(Context.AUDIO_SERVICE);

然后用这个类中的方法来操作声音组件。接下来用样例进行说明

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton4"
android:layout_toLeftOf="@+id/imageButton4"
android:src="@drawable/voiceup" /> <ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageButton2"
android:src="@drawable/music" /> <ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton5"
android:layout_toRightOf="@+id/imageButton1"
android:src="@drawable/novoice" /> <ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:src="@drawable/voicedown" /> <ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageButton3"
android:layout_alignTop="@+id/imageButton3"
android:layout_toLeftOf="@+id/imageButton3"
android:layout_toRightOf="@+id/imageButton2"
android:src="@drawable/vibrate" /> </RelativeLayout>

JAVA文件

<span style="font-size:18px;">package com.example.audiomanager;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast; public class MainActivity extends Activity {
private ImageButton voiceOn, voiceOff, voiceUp, voiceDown, vibate;
private AudioManager audio; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceOn = (ImageButton) this.findViewById(R.id.imageButton1);
voiceOff = (ImageButton) this.findViewById(R.id.imageButton2);
voiceUp = (ImageButton) this.findViewById(R.id.imageButton3);
voiceDown = (ImageButton) this.findViewById(R.id.imageButton4);
vibate = (ImageButton) this.findViewById(R.id.imageButton5);
// 创建Mediaplayer对象,并设置播放资源
MediaPlayer media = MediaPlayer.create(MainActivity.this, R.raw.fukua);
try {
// 、准备播放
media.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 进入软件自己主动播放音乐
media.start(); // 取得AudioManager对象
audio = (AudioManager) super.getSystemService(Context.AUDIO_SERVICE); // 一下为各个button设置的监听事件
voiceOn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.audio
.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(MainActivity.this, "手机音量开启", Toast.LENGTH_SHORT).show();
}
}); voiceOff.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.audio
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(MainActivity.this, "手机静音", Toast.LENGTH_SHORT).show();
}
}); voiceUp.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.audio.adjustVolume(AudioManager.ADJUST_RAISE,
0);
Toast.makeText(MainActivity.this, "手机音量增大", Toast.LENGTH_SHORT).show();
}
});
voiceDown.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.audio.adjustVolume(AudioManager.ADJUST_LOWER,
0);
Toast.makeText(MainActivity.this, "手机音量减小", Toast.LENGTH_SHORT).show();
}
});
vibate.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.audio
.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Toast.makeText(MainActivity.this, "手机为振动模式", Toast.LENGTH_SHORT).show();
}
});
} }
</span>

点击屏幕上的几个button就能实现对应的操作。大家能够依据这几节学到的东西自己实现一个简单的音乐播放器。

根据第预测:Viewpaper使用组件

版权声明:本文博客原创文章。博客,未经同意,不得转载。

从头学起android&lt;AudioManager 声音编辑器.五十.&gt;的更多相关文章

  1. 从头学起android&lt;GridView网格视图.二十八.&gt;

    GridView基于组件的网络格所有的形式的组分的,例如:当制作专辑,所有的图片将在相同的尺寸在不同的显示格在孩子,是否能够依靠这个组件完成.此组件的继承结构参见例如下面: java.lang.Obj ...

  2. Android简易实战教程--第五十话《动画扫描》

    祝新年快乐!2017(一起)前行. 转载博客请注明出处:道龙的博客 本篇简答的小案例,使用动画知识,完成一个类似雷达扫描效果,并且加入自定义进度条.对于自定义进度条前面有很详细的解析和案例了,本篇就结 ...

  3. 从头学起android&lt;android基本的绘图.四十六.&gt;

    在一般的图形渲染用户通常只需要重写onDraw()该方法可以是.但是假设,才能真正完成绘图操作.此外,我们需要掌握的四大核心经营类: android.graphics.Bitmap:主要表示的是一个图 ...

  4. 从头学起android&lt;AutoCompleteTextView文章提示文本框.十九.&gt;

    文章提示可以很好的帮助用户输入信息,以方便.在Android它也设置有类似特征,而要实现这个功能需要依靠android.widget.AutoCompleteTextView完毕,此类的继承结构例如以 ...

  5. Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》

    转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...

  6. Android的资源类型和存储方式简介-android学习之旅(五十二)

    android资源的类型 android资源的存储方式

  7. Intent的Component,Action和Category属性详解-android学习之旅(五十)

    Component属性 代码示例 public class MainActivity extends Activity{ @Override protected void onCreate(Bundl ...

  8. Android屏幕适配-android学习之旅(五十九)

    android屏幕适配

  9. Andriod的国际化-android学习之旅(五十八)

    android资源国际化

随机推荐

  1. Impossible WPF Part 1: Binding Properties

    原文 http://www.11011.net/wpf-binding-properties Ever wanted to write the following? <RichTextBoxDo ...

  2. (step8.2.2)hdu 2509(Be the Winner——简单博弈)

    题目大意:输入一个整数n,表示火柴堆数(原题其实指的是苹果堆数,但是为了尽量与模板保持一致,所以在这里理解为火柴堆数....其实理解为什么都没关系, 重要的是,理解就行....).在接下来的一行中,有 ...

  3. 安装虚拟机时出现The Microsoft Runtime DLL

    参考文档: http://zhidao.baidu.com/link?url=1E4vr6ToPGm_kAZw4voOqzrPtzGaSIqy3kvcGXehs3KJAkirNKOHJbrsxec3f ...

  4. 09-UIKit(UICollectionViewController、UITabBarController)

    目录: 一.UICollectionViewController 二.UITabBarController(标签控制器) 三.视图和试图控制器的生命周期 四.其他控件 回到顶部 一.UICollect ...

  5. Storyboard中使用UIscrollView添加约束的开发总结

    第一次在项目中用storyboard做界面,一般的界面直接添加约束非常爽快 然后有个界面有scrollview,添加了约束还总是出错 刚开始使用了 wCompact,hRegular,滑动出现问题,有 ...

  6. 青云B轮获2000万美元VC的背后逻辑:用技术超越巨头

    http://www.lagou.com/gongsi/31164.html http://capital.chinaventure.com.cn/11/7/1389263145.shtml

  7. 基于visual Studio2013解决C语言竞赛题之0907删除记录

       题目

  8. Android如何监听蓝牙耳机的按键事件

    写在前面: 直接想要代码很简单,你直接把滚动条拉到最底端就可以看到.如果想要十分地了解为什么,那就按照我规划的一步一步来理解.以下测试环境以手头上有的「Bluedio + 红米手机」. 1.蓝牙耳机的 ...

  9. Swift - 使用CoreLocation获取设备方向(真实方向,磁极方向)

    CoreLocation这个定位框架除了可以获取设备的位置数据,还可以获取设备的方向(可以用来实现指南针功能等). 1,CLHeading对象通过一组属性提供航向相关数据: magneticHeadi ...

  10. 免插件打造wordpress投稿页面

    一.新建投稿页面模板 把主题的 page.php 另存为 tougao.php,并且在第一行的 <?php 之后添加模板的标识注释: /* Template Name: tougao */ 紧接 ...