Android--聲音服務(AudioManager)

來源:互聯網
上載者:User

標籤:android   mediaplayer   

main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageButton        android:id="@+id/voiceon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/voice_on"/>     <ImageButton        android:id="@+id/voiceoff"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/voice_off"/>      <ImageButton        android:id="@+id/voicevibrate"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/voice_vibrate"/>       <ImageButton        android:id="@+id/voicelower"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/voice_lower"/>        <ImageButton        android:id="@+id/voiceraise"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/voice_raise"/></LinearLayout>
Activity程式如下:

package org.lxh.demo;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.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.Toast;public class MyAudioManagerDemo extends Activity {private ImageButton voiceOn = null ;private ImageButton voiceOff = null ;private ImageButton voiceVibrate = null ;private ImageButton voiceLower = null ;private ImageButton voiceRaise = null ;private AudioManager audio = null ;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.voiceOn = (ImageButton) super.findViewById(R.id.voiceon) ;this.voiceOff = (ImageButton) super.findViewById(R.id.voiceoff) ;this.voiceVibrate = (ImageButton) super.findViewById(R.id.voicevibrate) ;this.voiceLower = (ImageButton) super.findViewById(R.id.voicelower) ;this.voiceRaise = (ImageButton) super.findViewById(R.id.voiceraise) ;this.audio = (AudioManager) super.getSystemService(Context.AUDIO_SERVICE);this.voiceOn.setOnClickListener(new VoiceOnOnClickListenerImpl()) ;this.voiceOff.setOnClickListener(new VoiceOffOnClickListenerImpl()) ;this.voiceVibrate.setOnClickListener(new VoiceVibrateOnClickListenerImpl()) ;this.voiceLower.setOnClickListener(new VoiceLowerOnClickListenerImpl()) ;this.voiceRaise.setOnClickListener(new VoiceRaiseOnClickListenerImpl()) ;this.playAudio();}private void playAudio(){MediaPlayer media = MediaPlayer.create(this, R.raw.mldn_java);media.setLooping(true);// 迴圈播放try {media.prepare() ;} catch (Exception e) {}media.start() ;}private class VoiceOnOnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {MyAudioManagerDemo.this.audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL) ;Toast.makeText(MyAudioManagerDemo.this, "手機音量開啟!", Toast.LENGTH_SHORT).show() ;}}private class VoiceOffOnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {MyAudioManagerDemo.this.audio.setRingerMode(AudioManager.RINGER_MODE_SILENT) ;Toast.makeText(MyAudioManagerDemo.this, "手機靜音!", Toast.LENGTH_SHORT).show() ;}}private class VoiceVibrateOnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {MyAudioManagerDemo.this.audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE) ;Toast.makeText(MyAudioManagerDemo.this, "手機為震動模式!", Toast.LENGTH_SHORT).show() ;}}private class VoiceLowerOnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {MyAudioManagerDemo.this.audio.adjustVolume(AudioManager.ADJUST_LOWER, 0) ;Toast.makeText(MyAudioManagerDemo.this, "音量減小!", Toast.LENGTH_SHORT).show() ;}}private class VoiceRaiseOnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {MyAudioManagerDemo.this.audio.adjustVolume(AudioManager.ADJUST_RAISE, 0) ;Toast.makeText(MyAudioManagerDemo.this, "音量增加!", Toast.LENGTH_SHORT).show() ;}}}


運行效果如下:


Android--聲音服務(AudioManager)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.