iOS Question Background playback problem display image and title

Ali Alsaedi

Member
Licensed User
Longtime User
Hi ... It does not display the title and image to controll center music player in debug and release mode!


B4X:
Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    NativeMe = Me
    NativeMe.RunMethod("setAudioSession", Null)
    NativeMe.RunMethod("register", Null)
    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
    vp1.LoadVideoUrl("https://stream-dc1.radioparadise.com/mp3-32")
    vp1.Play
    Wait For VP1_Ready (Success As Boolean)
    Sleep(500)
    artwork = NativeMe.RunMethod("createArtwork:", Array(LoadBitmapResize(File.DirAssets, "icon.png", 64, 64, True)))
    SetPlayingInfo(CreateMap("title": "Test 1", "artwork": artwork))
End Sub

Sub Test2_Click
    vp1.LoadVideo(File.DirAssets,"37470.mp3")
    vp1.Play
    Wait For VP1_Ready (Success As Boolean)
    Sleep(500)
    artwork = NativeMe.RunMethod("createArtwork:", Array(LoadBitmapResize(File.DirAssets, "icon.png", 64, 64, True)))
    SetPlayingInfo(CreateMap("title": "Radio Paradise", "artwork": artwork))
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


#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];
    NSLog(@"Error setting category Audio Session: %@", [err localizedDescription]);
  }
}
- (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)];
}
- (MPMediaItemArtwork*)createArtwork:(UIImage*) image {
   return [[MPMediaItemArtwork alloc] initWithBoundsSize:image.size requestHandler:^UIImage* _Nonnull(CGSize aSize) { return image; }];
}
- (MPRemoteCommandHandlerStatus ) play {
   [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"play"]];
   return MPRemoteCommandHandlerStatusSuccess;
 
}
- (MPRemoteCommandHandlerStatus ) pause {
   [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"pause"]];
   return MPRemoteCommandHandlerStatusSuccess;
 
}
#End If
 
Last edited:

Ali Alsaedi

Member
Licensed User
Longtime User
I am sorry ... Why did you think so?

I run this code, but the file information is not displayed on the lock screen
 

Attachments

  • photo_2023-10-25_14-02-56.jpg
    photo_2023-10-25_14-02-56.jpg
    40.1 KB · Views: 54
Upvote 0
Top