从头学起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使用组件

版权声明:本文博客原创文章。博客,未经同意,不得转载。

posted @ 2015-08-12 09:40  hrhguanli  阅读(172)  评论(0编辑  收藏  举报