标签:风飞雪未扬 从零开始学android audiomanager android声音服务
我们在使用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); // 一下为各个按钮设置的监听事件 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>
点击屏幕上的几个按钮就能实现相应的操作,大家可以根据这几节学到的东西自己实现一个简单的音乐播放器。
下节预报:Viewpaper组件的使用
从零开始学android<AudioManager 声音编辑器.五十.>
标签:风飞雪未扬 从零开始学android audiomanager android声音服务
原文地址:http://blog.csdn.net/u013616976/article/details/40150237