从头学起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资源国际化
随机推荐
- php unset 数组陷阱
我们删除一个array, unset($arr); 想删除某个元素 unsert($arr[i]) 一个陷阱是: unset() 函数允许删除数组中的某个键.但要注意数组将不会重建索引.如果需要删除后 ...
- MYSQ提高L查询效率的策略总结
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值推断,否则将导致引擎放弃使用索 ...
- windows下以指定用户访问SMB服务器进行读写
需求:最近要开发某系统前端界面,但是该系统是部署在linux服务器上,前端是用php开发,实时调试运行需要linux下系统环境支持, 每次修改都需要手动传到服务器上,尤其是debug阶段,每修改一点就 ...
- python下载文件(图片)源码,包含爬网内容(爬url),可保存cookie
#coding=utf-8 ''' Created on 2013-7-17 @author: zinan.zhang ''' import re import time import httplib ...
- Inter IPP的一些基本类型对应的vs中类型
来自为知笔记(Wiz)
- perl 面向对象 use base
1.XXX.pm 文件里面的第一行要是:package XXX: 2.要有构造函数 sub new,实现如下: sub new { my $class = shift; # Get the reque ...
- 开源OCR光学字符识别
纸张在 许多地方已日益失宠,无纸化办公谈论40多年,办公环境正限制纸山的生成.而过去几年,无纸化办公的概念发生了显着的转变.在计算机软件的帮助 下,包含大量重要管理数据和资讯的文档可以更方便的以电子形 ...
- Jquery progressbar通过Ajax请求获取后台进度演示
项目源代码下载:http://download.csdn.net/detail/nuptboyzhb/6262253 1.简介 本文主要演示Jquery progressbar的进度条功能.js通过a ...
- C++里面的取整函数
#include<math.h> double ceil(double x) //向上取整 double floor(double x) //向下取整 也能够用数据类型强制转换,那要看数据 ...
- Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...