Author: h.mohri
Use C++Builder 10.2 to control audio volume of iOS.
I used the MPVolumeView class of iOS.
The MPVolumeView class is declared in “iOSapi.MediaPlayer.hpp”.
Table of Contents
Include two files.
1 2 |
<span class="synPreProc" style="color: #9355e6;">#include </span><span class="synConstant" style="color: #ff6666;"><iOSapi.MediaPlayer.hpp></span> <span class="synPreProc" style="color: #9355e6;">#include </span><span class="synConstant" style="color: #ff6666;"><iOSapi.UIKit.hpp></span> |
Form design
Arrange “Up” and “Down” two TButton. Use this button to control the volume.
Add MPVolumeView to the main form Class
Add “_di_MPVolumeView” and “_di_UISlider” to the form class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="synType" style="color: #3ec63e;">class</span> TForm1 : <span class="synStatement" style="color: #d88a17;">public</span> TForm { <span class="synStatement" style="color: #d88a17;">__published</span>: <span class="synComment" style="color: #4f80e5;">// IDE-managed Components</span> TButton *Button1; TLayout *Layout1; TButton *Button2; <span class="synType" style="color: #3ec63e;">void</span> __fastcall Button1Click(TObject *Sender); <span class="synType" style="color: #3ec63e;">void</span> __fastcall Button2Click(TObject *Sender); <span class="synStatement" style="color: #d88a17;">private</span>: <span class="synComment" style="color: #4f80e5;">// User declarations</span> _di_MPVolumeView MPVolumeView1; _di_UISlider slider1; <span class="synStatement" style="color: #d88a17;">public</span>: <span class="synComment" style="color: #4f80e5;">// User declarations</span> __fastcall TForm1(TComponent* Owner); }; |
Main Form constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { <span class="synComment" style="color: #4f80e5;">//Use "TMPVolumeView" to create "_di_MPVolumeView".</span> MPVolumeView1 = TMPVolumeView::Create(); <span class="synStatement" style="color: #d88a17;">for</span> (<span class="synType" style="color: #3ec63e;">int</span> i = <span class="synConstant" style="color: #ff6666;">0</span>; i < MPVolumeView1->subviews()->count(); i++) { <span class="synStatement" style="color: #d88a17;">switch</span> (i) { <span class="synStatement" style="color: #d88a17;">case</span> <span class="synConstant" style="color: #ff6666;">0</span>: <span class="synComment" style="color: #4f80e5;">//Setting the _di_UISlider in the "MPVolumeView1".</span> slider1 = TUISlider::Wrap(MPVolumeView1->subviews()->objectAtIndex(i)); <span class="synComment" style="color: #4f80e5;">//Set the slider to 0.0.</span> slider1->setValue(<span class="synConstant" style="color: #ff6666;">0.0</span>); } } } |
Code of volume down button
1 2 3 4 5 |
<span class="synType" style="color: #3ec63e;">void</span> __fastcall TForm1::Button1Click(TObject *Sender) { <span class="synType" style="color: #3ec63e;">float</span> i = slider1->value(); slider1->setValue(i - <span class="synConstant" style="color: #ff6666;">0.1</span>); } |
Code of volume up button
1 2 3 4 5 |
<span class="synType" style="color: #3ec63e;">void</span> __fastcall TForm1::Button2Click(TObject *Sender) { <span class="synType" style="color: #3ec63e;">float</span> i = slider1->value(); slider1->setValue(i + <span class="synConstant" style="color: #ff6666;">0.1</span>); } |
Execute the program.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition