This is example source code:
To test behavior:
1. Start app
2. Click on Button Test 1, wait for music
3. Click on button Test 2, wait for music
4. Lock phone screen (turn off screen)
5. Click power button to turn screen on
After this procedure there is no audio widget on lock screen. When widget disappear only is matter of time when system will kill app in background.
Any one have solution to prevent this behavior?
B4X:
#Region Project Attributes
#ApplicationLabel: AudioTest
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#ATSEnabled: False
#MinVersion: 8
#PlistExtra: <key>UIBackgroundModes</key><array><string>audio</string></array>
#End Region
Sub Process_Globals
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Private vp1 As VideoPlayer
Private Test1 As Button
Private Test2 As Button
Private NativeMe As NativeObject
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
NativeMe = Me
Page1.Initialize("Page1")
Page1.Title = "Page 1"
Page1.RootPanel.Color = Colors.White
Page1.RootPanel.LoadLayout("layMain")
NavControl.ShowPage(Page1)
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
End Sub
Private Sub Application_Background
End Sub
Public Sub ControlEvent (Command As String)
Select Command
Case "play"
vp1.Play
Case "pause"
vp1.Pause
End Select
End Sub
Sub Test1_Click
NativeMe.RunMethod("setAudioSession", Null)
vp1.LoadVideoUrl("http://www.hosting-serv.com:9090/;")
vp1.Play
NativeMe.RunMethod("register", Null)
End Sub
Sub Test2_Click
NativeMe.RunMethod("setAudioSession", Null)
vp1.LoadVideoUrl("http://stream-dc1.radioparadise.com/mp3-32")
vp1.Play
NativeMe.RunMethod("register", Null)
End Sub
#If OBJC
@import MediaPlayer;
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
- (void)setAudioSession {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];
if (success) {
success = [audioSession setActive:YES error:&err];
success = [audioSession setPreferredSampleRate:4096 error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
if (!success)
[NSException raise:@"" format:@"Error setting audio session: %@", err];
}
- (void)register {
MPRemoteCommandCenter* center = [MPRemoteCommandCenter sharedCommandCenter];
center.playCommand.enabled = true;
center.pauseCommand.enabled = true;
[center.playCommand addTarget:self action:@selector(play)];
[center.pauseCommand addTarget:self action:@selector(pause)];
}
- (void) play {
NSLog(@"test");
[self.bi raiseEvent:nil event:@"controlevent:" params:@[@"play"]];
}
- (void) pause {
[self.bi raiseEvent:nil event:@"controlevent:" params:@[@"pause"]];
}
#end if
To test behavior:
1. Start app
2. Click on Button Test 1, wait for music
3. Click on button Test 2, wait for music
4. Lock phone screen (turn off screen)
5. Click power button to turn screen on
After this procedure there is no audio widget on lock screen. When widget disappear only is matter of time when system will kill app in background.
Any one have solution to prevent this behavior?