Android Audiomanager Solve MediaPlayer audiotrack Adjust volume q

Source: Internet
Author: User

In handset mode


Am.setspeakerphoneon (FALSE);
Setvolumecontrolstream (Audiomanager.stream_voice_call);
Am.setmode (Audiomanager.mode_in_call);
I use MediaPlayer audiotrack to adjust the volume always fails


At.setstereovolume (vol, vol);
Player.setvolume (Vol,vol);
Then decided to use Audiomanager to adjust the volume.


Audiomanager can modify the volume of the system's Android system


Here are some ways to adjust the volume of several audiomanager.


The first is to get Audiomanager instances:


Audiomanager am= (Audiomanager) Getsystemservice (Context.audio_service);


There are two ways to adjust the volume method, one is progressive, that is, like manually pressing the volume key, step by step increase or decrease, the other is to set the volume value directly.


1. Progressive type


public void Adjuststreamvolume (int streamtype, int direction, int flags)

Am.adjuststreamvolume (Audiomanager.stream_music, Audiomanager.adjust_raise, audiomanager.flag_show_ui);
Explain three parameters


The first streamtype is the type of volume you want to adjust, which is set to the media volume, which can be:
Stream_alarm Alarm
Stream_music Music playback is media volume
Stream_notification window at the top of the status bar NOTIFICATION,
Stream_ring Ringtones
Stream_system system
Stream_voice_call Call
STREAM_DTMF dual tone Multi-frequency, not very understand what things

The second direction, which is to adjust the direction, increase or decrease, can be:
Adjust_lower lowering the volume
Adjust_raise Raise the Volume
The adjust_same remains the same, which is used primarily to show the current volume to the user

The third flags are additional parameters that describe only two common
Flag_play_sound playing sound when you adjust the volume
FLAG_SHOW_UI Display the volume bar when adjusting, the one that appears when you press the volume key
2, directly set the volume value method:


public void Setstreamvolume (int streamtype, int index, int flags)

Am.setstreamvolume (Audiomanager.stream_music, Am.getstreammaxvolume (Audiomanager.stream_music), Audiomanager.flag_play_sound);
Am.getstreammaxvolume (Audiomanager.stream_voice_call);//Get the maximum value of the handset mode
Am.getstreamvolume (Audiomanager.stream_voice_call);//Get the current value of the handset mode
The first and third arguments are the same as the previous one


The second parameter is an int value of volume, getstreammaxvolume (int streamtype) is the maximum value of the volume of that type, you can calculate the volume you need based on this value, and I'm going to get to the maximum.


The last of My Code:


Package COM.LP;


Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;

Import android.app.Activity;
Import Android.content.Context;
Import Android.media.AudioFormat;
Import Android.media.AudioManager;
Import Android.media.AudioTrack;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.SeekBar;
/**
* Audiotrack play audio as WAV format
* and allow volume adjustment
* @author Administrator
*
*/
public class MainActivity5 extends Activity {
Private Button play;
Private Button stop;
Private SeekBar Soundvalue;

Private Audiotrack at;
Private Audiomanager AM;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.MAIN_SK);
am = (Audiomanager) getsystemservice (Context.audio_service);
Play = (Button) Findviewbyid (R.id.main_sk_play);
Stop = (Button) Findviewbyid (r.id.main_sk_stop);
Soundvalue = (SeekBar) Findviewbyid (R.id.skbvolume);
Setvolumecontrolstream (Audiomanager.stream_voice_call);
Play.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {

if (Am.isspeakerphoneon ()) {
Am.setspeakerphoneon (FALSE);
}
Setvolumecontrolstream (Audiomanager.stream_voice_call);
Am.setmode (Audiomanager.mode_in_call);
System.out.println (Am.getstreammaxvolume (Audiomanager.stream_voice_call));
System.out.println ("&&&&&&&&&&&&&");
System.out.println (Am.getstreamvolume (Audiomanager.stream_voice_call));
Am.setstreamvolume (streamtype, index, flags)

int buffersizeinbytes = Audiotrack.getminbuffersize (44100, Audioformat.channel_out_mono, AudioFormat.ENCODING_PCM_ 16BIT);

if (at==null) {
at = new Audiotrack (Audiomanager.stream_voice_call, 44100, Audioformat.channel_out_mono, audioformat.encoding_pcm_ 16BIT, Buffersizeinbytes, Audiotrack.mode_stream);
System.out.println ("22222");
At.setstereovolume (100f, 100f);
At.setstereovolume (0.7f, 0.7f);//Set Current volume size
New Audiotrackthread (). Start ();
}else{
if (At.getplaystate () ==audiotrack.playstate_playing) {
System.out.println ("111111111");
}else{
System.out.println ("33333");
at = new Audiotrack (Audiomanager.stream_voice_call, 44100, Audioformat.channel_out_mono, audioformat.encoding_pcm_ 16BIT, Buffersizeinbytes, Audiotrack.mode_stream);
New Audiotrackthread (). Start ();
}
}

}
});

Stop.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
if (At.getplaystate () ==audiotrack.playstate_playing) {
try{
At.stop ();
}catch (IllegalStateException e)
{
E.printstacktrace ();
}
At.release ();
Am.setmode (Audiomanager.mode_normal);
}
}
});

Soundvalue.setmax (100);//volume adjustment limit
Soundvalue.setprogress (70);//Set the position value of the Seekbar
Soundvalue.setmax (Am.getstreammaxvolume (Audiomanager.stream_voice_call));
Soundvalue.setprogress (Am.getstreamvolume (Audiomanager.stream_voice_call));

Soundvalue.setonseekbarchangelistener (New Seekbar.onseekbarchangelistener () {

@Override
public void Onstoptrackingtouch (SeekBar SeekBar) {
float vol= (float) (seekbar.getprogress ())/(float) (Seekbar.getmax ());
SYSTEM.OUT.PRINTLN (vol);
At.setstereovolume (vol, vol);//Set volume
Am.setstreamvolume (Audiomanager.stream_voice_call, Seekbar.getprogress (), audiomanager.flag_play_sound);
}

@Override
public void Onstarttrackingtouch (SeekBar SeekBar) {
TODO auto-generated Method Stub
}

@Override
public void onprogresschanged (SeekBar SeekBar, int progress,
Boolean Fromuser) {
TODO auto-generated Method Stub
}
});
}

Class Audiotrackthread extends thread{

@Override
public void Run () {
byte[] out_bytes = new byte[44100];

InputStream is = Getresources (). Openrawresource (R.raw.start);
int length;
try{
At.play ();
}catch (IllegalStateException e)
{
E.printstacktrace ();
}
try {
while (length = Is.read (out_bytes))!=-1) {
System.out.println (length);
At.write (out_bytes, 0, length);
}
} catch (IOException e) {
E.printstacktrace ();
}
if (At.getplaystate () ==audiotrack.playstate_playing) {
try{
At.stop ();
}catch (IllegalStateException e)
{
E.printstacktrace ();
}
At.release ();
Am.setmode (Audiomanager.mode_normal);
}
}

}

}
Of course Permissions


<uses-permission android:name= "Android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>


This article is from the Linux commune website (www.linuxidc.com) Source Link: http://www.linuxidc.com/Linux/2011-10/44660.htm

Android Audiomanager Solve MediaPlayer audiotrack Adjust volume q

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.