Home>

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <button android:id="@ + id/btnplay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="play music" />
  <linearlayout android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">
    <togglebutton android:id="@ + id/tbmute"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:texton="mute"
      android:textoff="normal"
      android:checked="true"
      android:layout_gravity="center_vertical" />
    <button android:id="@ + id/btnupper"
      android:text="Increase volume"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <button android:id="@ + id/btnlower"
      android:text="decrease volume"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </linearlayout>
</linearlayout>

audioactivity class:

package com.ljq.activity;
import android.app.activity;
import android.app.service;
import android.media.audiomanager;
import android.media.mediaplayer;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.compoundbutton;
import android.widget.togglebutton;
import android.widget.compoundbutton.oncheckedchangelistener;
public class audioactivity extends activity {
  private button btnplay=null, btnupper=null, btnlower=null;
  private togglebutton tbmute=null;
  private mediaplayer mediaplayer=null;//audio
  private audiomanager audiomanager=null;//audio
  @override
  public void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
    audiomanager=(audiomanager) getsystemservice (service.audio_service);
    btnplay=(button) findviewbyid (r.id.btnplay);
    btnupper=(button) findviewbyid (r.id.btnupper);
    btnlower=(button) findviewbyid (r.id.btnlower);
    btnplay.setonclicklistener (listener);
    btnupper.setonclicklistener (listener);
    btnlower.setonclicklistener (listener);
    tbmute=(togglebutton) findviewbyid (r.id.tbmute);
    tbmute.setoncheckedchangelistener (new oncheckedchangelistener () {
      public void oncheckedchanged (compoundbutton buttonview, boolean ischecked) {
        audiomanager.setstreammute (audiomanager.stream_music,! ischecked);//Set whether to mute
      }
    });
  }
  view.onclicklistener listener=new view.onclicklistener () {
    public void onclick (view v) {
      @suppresswarnings ("unused")
      button btn=(button) v;
      switch (v.getid ()) {
      case r.id.btnplay://play music
        mediaplayer=mediaplayer.create (audioactivity.this, r.raw.music);
        mediaplayer.setlooping (true);//Set loop playback
        mediaplayer.start ();//Play sound
        break;
      case r.id.btnupper://Increase volume
        //adjuststreamvolume:adjust the volume of the specified sound type
        audiomanager.adjuststreamvolume (audiomanager.stream_music,            audiomanager.adjust_raise,            audiomanager.flag_show_ui);//Turn up the sound
        break;
      case r.id.btnlower://reduce volume
        //First parameter:sound type
        //The second parameter:adjust the direction of the volume
        //The third parameter:optional flag bit
        audiomanager.adjuststreamvolume (audiomanager.stream_music,            audiomanager.adjust_lower,            audiomanager.flag_show_ui);//turn down the sound
        break;
      }
    }
  };
}

operation result:

  • Previous Three modules of angularjs study notes (modal, controller, view)
  • Next Labelauty–jQuery radio/checkbox landscaping plugin sharing