从头学起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资源国际化
随机推荐
- 关于 Oracle外键列上是否需要索引问题?
外键列上缺少索引会带来两个问题,限制并发性.影响性能.而这两个问题中的任意一个都可能会造成严重性能问题. 无论是Oracle的官方文档,还是在Tom的书中都说明了两种情况下可以忽略外键上的索引.其 ...
- BZOJ 1196: [HNOI2006]公路修建问题( MST )
水题... 容易发现花费最大最小即是求 MST 将每条边拆成一级 , 二级两条 , 然后跑 MST . 跑 MST 时 , 要先加 k 条一级road , 保证满足题意 , 然后再跑普通的 MST . ...
- BZOJ 3211: 花神游历各国( 线段树 )
线段树...区间开方...明显是要处理到叶节点的 之前在CF做过道区间取模...差不多, 只有开方, 那么每个数开方次数也是有限的(0,1时就会停止), 最大的数10^9开方10+次也就不会动了.那么 ...
- inheritAll 及 ant antfile案例分析
<?xml version="1.0"?> <project name="a" default="targeta"> ...
- IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.htmlJava API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...
- 统一横轴墨卡托投影(UTM)
UTM 坐标系统使用基于网格的方法表示坐标.UTM 系统将地球分为 60 个区,每一个区基于横轴墨卡托投影.画图法中的地图投影方法能够在平面中表示一个两维的曲面,比如一个标准地图.图 1 展示了一个横 ...
- 将DataTable 存到一个集合当中
将DataTable 存到一个集合中 此做法来自:http://www.codeproject.com/Articles/692832/Simple-way-of-using-SQL-DataTabl ...
- WCF技术剖析之二十: 服务在WCF体系中是如何被描述的?
原文:WCF技术剖析之二十: 服务在WCF体系中是如何被描述的? 任何一个程序都需要运行于一个确定的进程中,进程是一个容器,其中包含程序实例运行所需的资源.同理,一个WCF服务的监听与执行同样需要通过 ...
- Python,PIL压缩裁剪图片
自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...
- HTML文档类型DTD与浏览器怪异模式
虽然在兼容IE6时候经常会注意到两个模式的区别,但是系统的理解起来,还没有认真总结过.看了一些网上的资料.结合自己的理解汇总了一下,放在这里备忘并分享给大家. 浏览器从服务端获取网页后会根据文档的DO ...