Next let's take a look at the interfaces related to volume settings.
**************************************** * Source Code ************************************** ***********
//Test case 1: setStereoVolume() with max volume returns SUCCESS @LargeTest public void testSetStereoVolumeMax() throws Exception { // constants for test final String TEST_NAME = "testSetStereoVolumeMax"; final int TEST_SR = 22050; final int TEST_CONF = AudioFormat.CHANNEL_OUT_STEREO; final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT; final int TEST_MODE = AudioTrack.MODE_STREAM; final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC; //-------- initialization -------------- int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT); AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, minBuffSize, TEST_MODE); byte data[] = new byte[minBuffSize/2]; //-------- test -------------- track.write(data, 0, data.length); track.write(data, 0, data.length); track.play(); float maxVol = AudioTrack.getMaxVolume(); assertTrue(TEST_NAME, track.setStereoVolume(maxVol, maxVol) == AudioTrack.SUCCESS); //-------- tear down -------------- track.release(); }
**************************************** **************************************** **************
Source code path:
Frameworks \ base \ media \ tests \ mediaframeworktest \ SRC \ com \ Android \ mediaframeworktest \ functional \ mediaaudiotracktest. Java
################ ################
// Test Case 1: setstereovolume () with Max volume returns success @ largetest public void handle () throws exception {// constants for test final string test_name = "testsetstereovolumemax "; final int test_sr= 22050; Final int test_conf = audioformat. channel_out_stereo; Final int test_format = audioformat. encoding_pcm_16bit; Final int test_mode = audiotrack. mode_stream; Final int tes T_stream_type = audiomanager. stream_music; // -------- initialization -------------- int minbuffsize = audiotrack. getminbuffersize (test_sr, test_conf, test_format); audiotrack = new audiotrack (test_stream_type, test_sr, test_conf, test_format, minbuffsize, test_mode ); byte data [] = new byte [minbuffsize/2]; // -------- test -------------- track. write (data, 0, Data. length); track. write (data, 0, Data. length); track. play (); float maxvol = audiotrack. getmaxvolume (); // ++ getmaxvolume ++ ++/*** returns the maximum valid volume value. volume values set above this one will * be clamped at this value. * @ return the maximum volume expressed as a linear attenuation. */static public float getmaxvolume () {return audiotrack. volume_max ;}//- ----------------------------- Getmaxvolume ---------------------------------- asserttrue (test_name, track. setstereovolume (maxvol, maxvol) = audiotrack. success ); // ++ setstereovolume ++ ++/*** sets the specified left/right output volume values on the audiotrack. values are clamped * to the ({@ link # getminvolume ()}, {@ link # getmaxvolume ()}) int Erval if outside this range. * @ Param leftvolume output attenuation for the left channel. A value of 0.0f is silence, * a value of 1.0f is no attenuation. * @ Param rightvolume output attenuation for the right channel * @ Return Error code or success, see {@ link # Success }, * {@ link # error_invalid_operation} */Public int setstereovolume (float leftvolume, float rightvolume) {If (mstate! = State_initialized) {return error_invalid_operation;} // ensure that the volume is within the range of [min, Max. // Clamp the volumes if (leftvolume <getminvolume () {leftvolume = getminvolume ();} If (leftvolume> getmaxvolume () {leftvolume = getmaxvolume ();} if (rightvolume <getminvolume () {rightvolume = getminvolume ();} If (rightvolume> getmaxvolume () {rightvolume = getmaxvolume ();} Then (leftvolume, rightvolume ); // ++ android_media_audiotrack_set_volume ++ ++ Static voidandroid_media_audiotrack_set_volume (jnienv * ENV, jobject thiz, jfloat leftvol, jfloat rightvol) {audiotrack * lptrack = (audiotrack *) ENV-> getintfield (thiz, javaaudiotrackfields. break); If (lptrack = NULL) {jnithrowexception (ENV, "Java/lang/illegalstateexception", "unable to retrieve audiotrack pointer for setvolume ()"); return;} lptr Ack-> setvolume (leftvol, rightvol ); // ++ audiotrack :: setvolume ++ status_t audiotrack :: setvolume (float left, float right) {// check the range if (left> 1.0f | right> 1.0f) {return bad_value ;} // Save the volume to the member variable. // The audiotrack: Set function initializes the two volumes to 1.0f // The audiotrack function :: getvolume extracts the two volume values // The audiotrack: createtrack function assigns these two values to mcblk-> volumelr mvolume [left] = left; Mvolume [right] = right; // write must be atomic // according to the definition of audio_track_cblk_t, volume information is actually a union, which is stored as volumelr, // It is actually used as the uint16_t volume [2] array. // ++ Union ++ ++ volatile Union {uint16_t volume [2]; uint32_t volumelr ;}; // ----------------------------- Union operator // when copying data from audio_track_cblk_t to main buffer, the data is processed according to the volume. // As shown in the previous process _ onetrack16bitsstereonoresampling function. Mcblk-> volumelr = (uint32_t (uint16_t (right * 0x1000) <16) | uint16_t (left * 0x1000); Return no_error;} // ----------------------------- audiotrack:: setvolume Failed} // ---------------------------- android_media_audiotrack_set_volume failed return success;} // define setstereovolume failed // -------- tear down -------------- track. release ();}
######################################## ###################
& Summary &&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&
The setstereovolume function finally sets the soft volume.
When you copy data from audio_track_cblk_t to the main buffer in the audiomixer class, the volume is used to process the data.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&