How would I play songs or make a splash screen for my cocoa app? - objective-c

How would I play songs or make a splash screen for my cocoa app? I know it's a simple question but I am a complete noob when it comes to cocoa.

You want to create an application delegate class and implement the -applicationWillFinishLaunching and -applicationDidFinishLaunching methods to display/hide your splash screen or start/stop your audio. You can connect an instance of this class as the application's delegate in Interface Builder in the project's MainMenu.xib.
Keep in mind that it's generally considered bad form to have to display a splash/load screen in Mac apps. If your app can start instantly and lazily load resources or load them in a background thread, it provides a much nicer experience for your users.

Barry is right, you have to get rid of this idea to use splash screens.. it's mainly a M$ Windows concept and it is kind of frustrating for the user to wait for the app to load itself, and I bet you won't load a thing and you just want to show a splash screen so you can feel important, but I'm telling you: apps with "marketing-only" splash screens are trash right from the beginning, because the user waits for absolutely nothing to load, and he/she will immediatelly get sick of seeing it every single time the app starts..
Now, about the songs.. I'll help you on this one, but I'm telling you again: it's useless, can't see the usefullness in having a sound play on every startup... so putting sound and a splash screen to your app startup will scare most users away, and your app won't made it!
So, to load a song you use the NSSound class like this:
NSSound *s = [[NSSound alloc] initWithContentsOfFile:songPath byReference:YES];
and then you can control it with the following methods:
[s play]
[s pause]
[s resume]
[s stop]

Related

How should I go about stopping/resuming all sounds within my iOS game?

I currently have background music that starts up when the app opens and runs an infinite loop until the user goes into the settings and turns the music off using this
-(IBAction)stopMusicButton{
[(AppDelegate*)[UIApplication sharedApplication].delegate stopMusic];
}
They can also resume the music in the settings page. What I'm a confused about is how to stop the other sounds in my app from playing if the user doesn't want the noise. For example, when the user navigates the menu of my app there are various sounds that play when certain things are pressed. How can I allow the user to be able to toggle these sounds on and off? There are also a few different views with their respective .h/.m files, all of which play sounds. If you could point me in the right direction I'd really appreciate it! Thanks in advance!
I use a singleton class to store settings information - you could probably use your app delegate as well. All you need is a boolean property called isSoundMuted or something like that. Set that whenever the sound is muted or unmuted, then check the variable's value before playing any sounds in an if block.
if (MySingletonClass.isSoundMuted == NO)
{
[self playSound]
}

After exiting app, how to go back to the exact same screen where I left off?

Is there a quick and easy way to have the app go back to the same screen after I put it in background mode?
I know there are frameworks just for this stuff, but has anyone done it without much sweat?
Thanks
In your app delegate, add a line to the applicationDidenterBackground: method that stores the current page via NSUserDefaults.
Then in the applicationWillEnterForeground: method, load up that saved value and restore the page.
Multitasking should enable this, while a device has enough memory to keep your app open. Otherwise how about using NSUserDefaults to store a string reference to your view?
As of iOS 5.1, there is no quick and easy way.

Receive remote control events without audio

Here is some background information, otherwise skip ahead to the question in bold. I am building an app and I would like it to have access to the remote control/lock screen events. The tricky part is that this app does not play audio itself, it controls the audio of another device nearby. The communication between devices is not a problem when the app is in the foreground. As I just found out, an app does not assume control of the remote controls until it has played audio with a playback audio session, and was the last do so. This presents a problem because like I said, the app controls ANOTHER device's audio and has no need to play its own.
My first inclination is to have the app play a silent clip every time it is opened in order to assume control of the remote controls. The fact that I have to do this makes me wonder if I am even going to be allowed to do it by Apple or if there is another way to achieve this without fooling the system with fake audio clips.
QUESTION(S): Will Apple approve an app that plays a silent audio clip in order to assume control of the remote/lock screen controls for the purpose of controlling another device's audio? Is there any way of assuming control of the remote controls without an audio session?
P.S. I would prefer to have this functionality on iOS 4.0 and up.
P.P.S I have seen this similar question and it has gotten me brainstorming but the answer provided is not specific to what I need to know.
NOTE: As of iOS 7.1, you should be using MPRemoteCommandCenter instead of the answer below.
You create various system-provided subclasses of MPRemoteCommand and assign them to properties of the [MPRemoteCommandCenter sharedCommandCenter].
I'm keeping the rest of this around for historical reference, but the following is not guaranteed to work on recent iOS versions. In fact, it just might not.
You definitely do need an audio player but not necessarily an explicit session to take control of the remote control events. (AVAudioSession is implicit to any app that plays audio.) I spent a decent amount of time playing with this to confirm this.
I've seen a lot of confusion on the internet about where to set up the removeControlEventRecievedWithEvent: method and various approaches to the responder chain. I know this method works on iOS 6 and iOS 7. Other methods have not. Don't waste your time handling remote control events in the app delegate (where they used to work) or in a view controller which may go away during the lifecycle of your app.
I made a demo project to show how to do this.
Here's a quick rundown of what has to happen:
You need to create a subclass of UIApplication. When the documentation says UIResponder, it means UIApplication, since your application class is a subclass of UIResponder. In this subclass, you're going to implement the remoteControlReceivedWithEvent: and canBecomeFirstResponder methods. You want to return YES from canBecomeFirstResponder. In the remote control method, you'll probably want to notify your audio player that something's changed.
You need to tell iOS to use your custom class to run the app, instead of the default UIApplication. To do so, open main.m and change this:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([RCAppDel`egate class]));
to look like this:
return UIApplicationMain(argc, argv, NSStringFromClass([RCApplication class]), NSStringFromClass([RCAppDelegate class]));
In my case RCApplication is the name of my custom class. Use the name of your subclass instead. Don't forget to #import the appropriate header.
OPTIONAL: You should configure an audio session. It's not required, but if you don't, audio won't play if the phone is muted. I do this in the demo app's delegate, but do so where appropriate.
Play something. Until you do, the remote controls will ignore your app. I just took an AVPlayer and gave it the URL of a streaming site that I expect to be up. If you find that it fails, put your own URL in there and play with it to your heart's content.
This example has a little bit more code in there to log out remote events, but it's not all that complicated. I just define and pass around some string constants.
I bet that a silent looping MP3 file would help work towards your goal.
Moshe's solution worked great for me! However one issue I noticed is when you paused the audio, the media controls would go away and you won't be able to play it again without going back into the app. If you set the Media Info on the lock screen when you play the audio then this won't happen:
NSDictionary *mediaInfo = #{MPMediaItemPropertyTitle: #"My Title",
MPMediaItemPropertyAlbumTitle: #"My Album Name",
MPMediaItemPropertyPlaybackDuration: [NSNumber numberWithFloat:0.30f]};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];

Application crashes while logging out in iPAD

My question is specific to iPAD, and I also aware of the basic memory management of iOS, but I am having a different problem.
As I have build an application where I have several UIViewControllers and UIViews,
I have a loginController thats gets called when I launch the App.
My MainView is a single screen with all the ViewController loaded and placed at their respective places and the app runs fine and smoothly.
Problem:
Problem comes when I logout, most of the time my App crashes by saying EXC_BAD on the
[super dealloc] line of my mainView controller.
As for now on I have added a custom function cleanUP in all my viewControllers that gets called when user logout from the app.
Is this the right approach ?
As I know that we can clean up in our didload etc. function and the dealloc gets called too.
but here i have an iPAD when my all viewControllers are just open in front of me, They will be closed or not visible when I logout from the App.
So how to approach on my crash issue and How to manage memory here in my iPAD?
The best way I know to resolve bad-access problems is to use Instruments with the Zombie tool. As you probably know, when you get a bad access issue, is because you try to access to an object that is deallocated.
Try go to Product -> Profile and choose Zombie. Hit record and reproduce your crash. then inspect the pointer to the object that produced that crash and look for the retain count.

Playing a sound that repeats throughout the program in iOS

In my app, every time a user switches to a new view a sound will be played. Also, every time a button is tapped, a sound will be played.
The sounds to be played are the same for each case, so for example, sound.caf will be used many times and will be the same for every view.
My problem is that I cant think of a clever place to alloc these sounds, thats visible to all views. Is this possible? I dont want to alloc the sound everytime a view is created because I think it's a waste of time.
Is there someplace where I can alloc these sounds, and play them from any view in my program? Thanks!
The simplest solution would probably be to use the app delegate for that purpose because it's a singleton class (there is only one instance of it per application) and it can be accessed by any class easily: [UIApplication sharedApplication].delegate
You could set up the sound(s) in the applicationDidFinishLaunching: method so they're ready to go when your app is.