从头学起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资源国际化
随机推荐
- 由动态库文件dll生成lib库文件(手动生成.def文件,然后使用lib命令编译,非常牛),同理可使用dll生成.a库文件
本文基于OpenBlas的编译和安装,来说明如何从一个dll文件生成lib库文件. 参考OpenBlas的说明“Howto generate import library for MingW”,和Mi ...
- Jetty:配置概览-须要配置什么
上一节讲述了怎么配置Jetty,这节将告诉你使用Jetty你须要配置些什么. 配置Server Server实例是Jetty服务端的中心协调对象,它为全部其它Jetty服务端组件提供服务和生命周期管理 ...
- sql server 深入使用 总结 part1
1 OUTPUT 应用场景 : 1.1.对于INSERT,可以引用inserted表以查询新行的属性. insert into [表名] (a) OUTPUT Inserted. ...
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 撰写. 根据您编写的应用程序,Windows Azure 网站上的基本Python 堆 ...
- Boost 库Program Options--第二篇
程式執行參數處理函式庫:Boost Program Options(2/N) 前一篇已經大致解釋了 Boost Program Options 基本上的使用方法.而這一篇,則來細講一下選項描述(opt ...
- 重探 DFT
感觉上次学习DFT简直是乱来了.不知道误导了多少人,这里深感抱歉. 这次我再看了看<算法导论>,觉得收获很大,终于粗略的知道DFT的原理了! 如何将两个多项式相乘 对于一个n次多项式,\( ...
- win7系统远程连接其它计算机,并且向远程机传输文件
首先,打开开始菜单,在程序自带的 “附件“ 中找到 "远程桌面连接"并打开,出现远程桌面对话框: 其次,在对话框左下角点击“选项”,选择“本地资源对话框”,在本地设备和资源下点击“ ...
- window下svn注册为本地的服务
sc create svnservice binpath= "\"C:\program files\Subversion\bin\svnserve.exe\" --ser ...
- java.lang.NoClassDefFoundError: ognl/PropertyAccessor解决的方法
本来不想为这个专门写一篇文章的,可是发现这么简单的一个问题居然没有人好好回答过.从方便搜索的角度考虑,特意取了这么一个题目. 事实上解决方法就是将ognl的jar包增加就可以. 比方我用的是ognl3 ...
- PHP - 接口 - 多接口
/* * 使用多接口 */ //定义接口1 interface IPerosn_one{ public function eat(); } //定义接口2 interface IPerson_two{ ...