从头学起android<AudioManager 声音编辑器.五十.>
我们用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<AudioManager 声音编辑器.五十.>的更多相关文章
- 从头学起android<GridView网格视图.二十八.>
GridView基于组件的网络格所有的形式的组分的,例如:当制作专辑,所有的图片将在相同的尺寸在不同的显示格在孩子,是否能够依靠这个组件完成.此组件的继承结构参见例如下面: java.lang.Obj ...
- Android简易实战教程--第五十话《动画扫描》
祝新年快乐!2017(一起)前行. 转载博客请注明出处:道龙的博客 本篇简答的小案例,使用动画知识,完成一个类似雷达扫描效果,并且加入自定义进度条.对于自定义进度条前面有很详细的解析和案例了,本篇就结 ...
- 从头学起android<android基本的绘图.四十六.>
在一般的图形渲染用户通常只需要重写onDraw()该方法可以是.但是假设,才能真正完成绘图操作.此外,我们需要掌握的四大核心经营类: android.graphics.Bitmap:主要表示的是一个图 ...
- 从头学起android<AutoCompleteTextView文章提示文本框.十九.>
文章提示可以很好的帮助用户输入信息,以方便.在Android它也设置有类似特征,而要实现这个功能需要依靠android.widget.AutoCompleteTextView完毕,此类的继承结构例如以 ...
- Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》
转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...
- Android的资源类型和存储方式简介-android学习之旅(五十二)
android资源的类型 android资源的存储方式
- Intent的Component,Action和Category属性详解-android学习之旅(五十)
Component属性 代码示例 public class MainActivity extends Activity{ @Override protected void onCreate(Bundl ...
- Android屏幕适配-android学习之旅(五十九)
android屏幕适配
- Andriod的国际化-android学习之旅(五十八)
android资源国际化
随机推荐
- [置顶] C++ sizeof实例详解
在C++中使用sizeof要比C复杂很多,因为C++类中有static静态变量,virtual虚函数,还有继承.派生等.sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是 ...
- 工具类CTools实现字符编码转换和获取当前路径
class CTools { public: CTools(void); public: ~CTools(void); public: static std::string UNICODE_to_UT ...
- BZOJ 1578: [Usaco2009 Feb]Stock Market 股票市场( 背包dp )
我们假设每天买完第二天就卖掉( 不卖出也可以看作是卖出后再买入 ), 这样就是变成了一个完全背包问题了, 股票价格为体积, 第二天的股票价格 - 今天股票价格为价值.... 然后就一天一天dp... ...
- Android--开发过程中使用到的长度单位
px:表示屏幕实际的像素. in:表示英寸. mm:毫米. pt:表示一个点,是屏幕的物理尺寸. dp:(与密度无关的像素)逻辑长度单位,在160dpi屏幕上,1dp = 1px = 1/160英寸 ...
- 关于 Swift
摘自:http://numbbbbb.gitbooks.io/-the-swift-programming-language-/chapter1/01_swift.html Swift 是一种新的编程 ...
- solr4.x设置默认查询字段
1.如果需要同时在title和content中进行查询,可以添加如下字段: <field name="title_content" type="textComple ...
- 新的 Windows Azure 网络安全性白皮书
下载新的 Windows Azure 网络安全性白皮书. Windows Azure 网络提供了将虚拟机安全连接到其他虚拟机所需的基础结构,以及云和内部部署数据中心之间的网桥. 本白皮书旨在挖掘这些内 ...
- android Context的理解
很多初入Android开发的网友向我们问到Context有什么作用,很多地方都用到它,这里Android123给这些新入门的网友做个简单的解释: Context字面意思上下文,位于framework ...
- css图片上下垂直居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python 读取图片的尺寸、分辨率
#需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...