Skip to content

Instantly share code, notes, and snippets.

@retrokid
Last active November 27, 2020 08:27
Show Gist options
  • Select an option

  • Save retrokid/68204fb5bc7ff9fa5074f1f5a5f88a46 to your computer and use it in GitHub Desktop.

Select an option

Save retrokid/68204fb5bc7ff9fa5074f1f5a5f88a46 to your computer and use it in GitHub Desktop.
FMOD
//FMOD gist
// mod file test function
-(void)createSoundAndPlay
{
FMOD_SYSTEM *soundSystem;
FMOD_SOUND *soundFile;
NSString *modFilePath = [[NSBundle mainBundle] pathForResource:@"accord" ofType:@"mod"];
FMOD_System_Create(&soundSystem);
FMOD_System_Init(soundSystem, 10, FMOD_INIT_NORMAL, NULL);
FMOD_System_CreateSound(soundSystem, [modFilePath UTF8String], FMOD_CREATESAMPLE, NULL, &soundFile);
FMOD_System_PlaySound(soundSystem, soundFile, NULL, false, NULL);
}
- (void) startPlaying
{
if(numberOfEventInstances < MAX_FMOD_STUDIO_EVENTINSTANCE)
{
FMOD_STUDIO_EVENTINSTANCE *eventInst;
FMOD_STUDIO_EVENTDESCRIPTION *eventDesc;
NSString *guid = @"{086962a2-2377-4280-ae2e-9cd2680b955c}";
FMOD_Studio_System_GetEvent(eventSystem, [guid UTF8String], &eventDesc);
FMOD_Studio_EventDescription_CreateInstance(eventDesc, &eventInst);
FMOD_Studio_EventInstance_Start(eventInst);
FMOD_Studio_System_Update(eventSystem);
numberOfEventInstances++;
}
}
-(void) stopPlaying
{
FMOD_STUDIO_EVENTINSTANCE *eventInst[MAX_FMOD_STUDIO_EVENTINSTANCE];
int count;
FMOD_STUDIO_EVENTDESCRIPTION *eventDesc;
NSString *guid = @"{d93ae074-cf0d-403b-89c4-a977050d94ab}";
FMOD_Studio_System_GetEvent(eventSystem, [guid UTF8String], &eventDesc);
FMOD_Studio_EventDescription_GetInstanceList(eventDesc, eventInst, MAX_FMOD_STUDIO_EVENTINSTANCE,&count);
for (int i = 0; i < count; i++)
{
FMOD_Studio_EventInstance_Stop(eventInst[i], FMOD_STUDIO_STOP_IMMEDIATE);
FMOD_Studio_EventInstance_Release(eventInst[i]);
NSLog(@"adfa -- %d",count);
}
FMOD_Studio_System_Update(eventSystem);
numberOfEventInstances = 0;
}
//
// AppDelegate.swift
// music-player
//
// Created by efe ertugrul on 16/12/2017.
// Copyright © 2017 efe ertugrul. All rights reserved.
//
import UIKit
import AVFoundation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Override point for customization after application launch.
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
/*
!! IMPORTANT !!
If you're using 3rd party libraries to play sound or generate sound you should
set sample rate manually here.
Otherwise you wont be able to hear any sound when app goes background.
*/
//try AVAudioSession.sharedInstance().setPreferredSampleRate(4096)
}
catch
{
print(error)
}
// This will enable to show nowplaying controls on lock screen
application.beginReceivingRemoteControlEvents()
return true
}
}
//
// ViewController.swift
// music-player
//
// Created by efe ertugrul on 16/12/2017.
// Copyright © 2017 efe ertugrul. All rights reserved.
//
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController
{
var player : AVPlayer = AVPlayer()
override func viewDidLoad()
{
super.viewDidLoad()
let path = Bundle.main.path(forResource: "music", ofType: "mp3")
let url = URL(fileURLWithPath: path!)
// !! IMPORTANT !!
/*
If you are using 3rd party libraries to play sound or generate sound you should always setNowPlayingInfo before you create your player object.
like:
self.setNowPlayingInfo()
self.player = AVPlayer(url: url)
if you set now playing after you create your system/player etc. like :
self.player = AVPlayer(url: url)
self.setNowPlayingInfo()
You won't be able to see any controls on lock screen.
*/
self.setNowPlayingInfo()
self.player = AVPlayer(url: url)
}
func setNowPlayingInfo()
{
let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
var nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo ?? [String: Any]()
let title = "title"
let album = "album"
let artworkData = Data()
let image = UIImage(data: artworkData) ?? UIImage()
let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: { (_) -> UIImage in
return image
})
nowPlayingInfo[MPMediaItemPropertyTitle] = title
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo
}
@IBAction func startPlayingButtonPressed(_ sender: Any)
{
self.player.play()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment