Skip to content

Commit

Permalink
- added more music and audio os related api's for ios initially
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Jan 16, 2024
1 parent 64c5d31 commit f8f6983
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/pen/include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
39 changes: 39 additions & 0 deletions core/pen/source/ios/os.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import <AVFoundation/AVAudioFile.h>
#import <AVFoundation/AVAudioSession.h>
#import <MediaPlayer/MPMediaQuery.h>
#import <MediaPlayer/MPNowPlayingInfoCenter.h>

// the last 2 global externs \o/
pen::user_info pen_user_info;
Expand Down Expand Up @@ -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;
Expand All @@ -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];
}
}
}

0 comments on commit f8f6983

Please sign in to comment.