iOS Question Audio widget disappear from lock screen

Pendrush

Well-Known Member
Licensed User
Longtime User
This is example source code:
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?
 

Attachments

  • AudioTest.zip
    22.9 KB · Views: 341

Pendrush

Well-Known Member
Licensed User
Longtime User
You need to follow exact these steps to replicate problem:
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 click on Test 1 and turn off screen widget is on lock screen, but if you follow exact steps, widget will disappear.

I'm not able to use AVPlayer as I need events from player ("Ready" event), I also need to stop player and also to be able to play hundreds of streams per one app session without crating hundreds of players.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This seems to work fine:
B4X:
Sub Test1_Click
   vp1.LoadVideoUrl("http://www.hosting-serv.com:9090/;")
   vp1.Play
   Wait For VP1_Ready (Success As Boolean)
   Sleep(500)
   SetPlayingInfo(CreateMap("title": "Test 1"))
End Sub

Sub Test2_Click
   vp1.LoadVideoUrl("http://stream-dc1.radioparadise.com/mp3-32")
   vp1.Play
   Wait For VP1_Ready (Success As Boolean)
   Sleep(500)
   SetPlayingInfo(CreateMap("title": "Radio Paradise"))
End Sub

Sub SetPlayingInfo (info As Map)
   Dim NowPlayingInfoCenter As NativeObject
   NowPlayingInfoCenter = NowPlayingInfoCenter.Initialize("MPNowPlayingInfoCenter").RunMethod("defaultCenter", Null)
   Dim no As NativeObject = info
   NowPlayingInfoCenter.SetField("nowPlayingInfo", no.RunMethod("ToDictionary", Null))
End Sub
 
Upvote 0
Top