From f8f6983f68a49caee65f993fe340e078113a4cd1 Mon Sep 17 00:00:00 2001 From: polymonster Date: Tue, 16 Jan 2024 21:00:50 +0000 Subject: [PATCH] - added more music and audio os related api's for ios initially --- core/pen/include/os.h | 3 +++ core/pen/source/ios/os.mm | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/core/pen/include/os.h b/core/pen/include/os.h index 0725155d..a5039e09 100644 --- a/core/pen/include/os.h +++ b/core/pen/include/os.h @@ -46,6 +46,7 @@ namespace pen void os_create_directory(const Str& dir); void os_open_url(const Str& url); void os_ignore_slient(); + void os_enable_background_audio(); f32 os_get_status_bar_portrait_height(); void os_haptic_selection_feedback(); @@ -70,5 +71,7 @@ namespace pen const music_item* music_get_items(); // returns stretchy buffer use sb_count for num items music_file music_open_file(const music_item& item); void music_close_file(const music_file& file); + void music_enable_remote_control(); + void music_set_now_playing(const Str& artist, const Str& album, const Str& track); } // namespace pen diff --git a/core/pen/source/ios/os.mm b/core/pen/source/ios/os.mm index 5c73f337..d87838f6 100644 --- a/core/pen/source/ios/os.mm +++ b/core/pen/source/ios/os.mm @@ -29,6 +29,7 @@ #import #import #import +#import // the last 2 global externs \o/ pen::user_info pen_user_info; @@ -427,6 +428,22 @@ void os_ignore_slient() } } + void os_enable_background_audio() + { + AVAudioSession *session = [AVAudioSession sharedInstance]; + double rate = 24000.0; // This should match System::setSoftwareFormat 'samplerate' which defaults to 24000 + s32 blockSize = 512; // This should match System::setDSPBufferSize 'bufferlength' which defaults to 512 + + BOOL success = [session setPreferredSampleRate:rate error:nil]; + PEN_ASSERT(success); + + success = [session setPreferredIOBufferDuration:blockSize / rate error:nil]; + PEN_ASSERT(success); + + success = [session setActive:TRUE error:nil]; + PEN_ASSERT(success); + } + f32 os_get_status_bar_portrait_height() { CGFloat h = [[[UIApplication sharedApplication] windows].firstObject windowScene].statusBarManager.statusBarFrame.size.height; @@ -443,4 +460,26 @@ void os_haptic_selection_feedback() [generator release]; } } + + void music_enable_remote_control() + { + [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; + } + + void music_set_now_playing(const Str& artist, const Str& album, const Str& track) + { + @autoreleasepool { + NSString* nsartist = [NSString stringWithUTF8String:artist.c_str()]; + NSString* nsalbum = [NSString stringWithUTF8String:album.c_str()]; + NSString* nstrack = [NSString stringWithUTF8String:track.c_str()]; + + NSMutableDictionary* info = [[NSMutableDictionary alloc] init]; + [info setObject:nstrack forKey:MPMediaItemPropertyTitle]; + [info setObject:nsalbum forKey:MPMediaItemPropertyAlbumTitle]; + [info setObject:nsartist forKey:MPMediaItemPropertyArtist]; + + [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info]; + [info release]; + } + } }