'Code module
#Region Project Attributes
#ApplicationLabel: Control General Audio Volume
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#MinVersion: 7
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Private xTTS As TTS
Private label1 As Label
Private volume_value As Float
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.Title = "Page 1"
Page1.RootPanel.Color = Colors.White
NavControl.ShowPage(Page1)
'Initialize TTS
xTTS.Initialize("")
Audiovolume_hide
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
Page1.RootPanel.RemoveAllViews
label1.Initialize("")
Page1.RootPanel.AddView(label1, 0, 0, Page1.RootPanel.Width, Page1.RootPanel.Height)
label1.Multiline = True
label1.Text = "Tap screen to change volume" & CRLF & "Volume set at: " & volume_value & CRLF & "Volume reported: " & Audiovolume_get
End Sub
Private Sub Page1_Click
volume_value = volume_value - .25
If volume_value < 0 Then volume_value = 1
Audiovolume_set(volume_value)
label1.Text = "Tap screen to change volume" & CRLF & "Volume set at: " & volume_value & CRLF & "Volume reported: " & Audiovolume_get
'Speak away
xTTS.Stop
xTTS.Speak("testing testing 1 2 3 4 5 6 7 8 9 10", True)
End Sub
Private Sub Application_Background
End Sub
Sub Audiovolume_hide
Dim no As NativeObject = Me
no.RunMethod("audiovol_hide", Null)
End Sub
Sub Audiovolume_get As Float
Dim no As NativeObject = Me
Return no.RunMethod("audiovol_get", Null).AsNumber
End Sub
Sub Audiovolume_set(volumevalue As Float)
Dim no As NativeObject = Me
no.RunMethod("audiovol_set:", Array(volumevalue))
End Sub
#If OBJC
@import MediaPlayer;
- (void) audiovol_hide
{
//following hides "general audio volume HUD" - see:
//http://stackoverflow.com/questions/7430985/hide-grey-volume-overlay-when-using-mpvolumeview
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-2000., -2000., 0.f, 0.f)];
NSArray *windows = [UIApplication sharedApplication].windows;
volumeView.alpha = 0.1f;
volumeView.userInteractionEnabled = NO;
if (windows.count > 0) {
[[windows objectAtIndex:0] addSubview:volumeView];
}
}
- (float) audiovol_get
{
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
//find general audio volumeViewSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
// get general audio volume here:
static float volval = .123;
// if comment next line out then this method returns .123 - proving that next
// line actually always returns 0 (at least in iOS 9.3)
volval = [volumeViewSlider value];
return volval;
}
- (void) audiovol_set: (float) volval
{
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
//find general audio volumeViewSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
// set general audio volume here:
[volumeViewSlider setValue:volval animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
}
#End If