Audio on TouchesEnded - xcode

On TouchesEnded I want to do two things:
1) return the view to its original position - this works fine.
2) I want to play an audio sound if the touch ended on the YES vies and a different sound for the NO views.
How can I do this?
At this moment my sound for NO will play for every touch ended - and this is not good.
I am new to this so please explain the basics :) my current code below.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
yesletter1.frame = keepYesLetter1Place;
yesletter2.frame = keepYesLetter2Place;
yesletter3.frame = keepYesLetter3Place;
yesletter4.frame = keepYesLetter4Place;
notletter1.frame = keepNoLetter1Place;
notletter2.frame = keepNoLetter2Place;
notletter3.frame = keepNoLetter3Place;
notletter4.frame = keepNoLetter4Place;
NSString *path = [[NSBundle mainBundle]
pathForResource:#"no"ofType:#"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}

Solved this issue not the way i wanted but thing work now.
I have 2 different sounds for some objects one sound and the other another sound.
What i did is added a flag for each object and did if(flag1 == 0 || flag2 == 0 ect...) play sound 1 else play sound 2.
in another method which does something else i played with the flags.
I am sure there is a better solution for this :) but things work now.

Related

How do I add background music to my spritekit file

Could someone give me a quick easy step by step to adding background m4a music once my app has loaded. It is a sprite kit Xcode file, and the music is in m4a format. Thanks
Try with this:
#import AVFoundation;
...
AVAudioPlayer * backgroundMusicPlayer;
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:#"song" withExtension:#"m4a"];
backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
backgroundMusicPlayer.numberOfLoops = -1; //-1 = infinite loop
[backgroundMusicPlayer prepareToPlay];
[backgroundMusicPlayer play];
and to stop simply
[backgroundMusicPlayer stop];
note: I don't use SKAction to play background music because you can't stop it when you want
You can use AVAudioPlayer for this purpose:
In your .h:
#import <AVFoundation/AVFoundation.h>
and add the following to interface
AVAudioPlayer *player;
In .m, initialize player with audio oath url:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"bg_music"
ofType:#"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
and when you need to play the audio, you can call:
[player play];
Note:
"numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end.
A value of zero means to play the sound just once.
A value of one will result in playing the sound twice, and so on...
Any negative number will loop indefinitely until stopped.
Keep Coding................ :)

Looping Through NSArray

This is suppose to play all the songs in the NSArray but it only plays one and stops. What am I doing wrong? The array contains a whole bunch of links to different .mp3 formatted songs.
NSArray *array = [myString componentsSeparatedByString:#"\n"];
int nextTag = 1;
for (soundPath in array) {
NSURL *url = [NSURL URLWithString:soundPath];
asset = [AVURLAsset URLAssetWithURL:url options:nil];
yourMediaPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
yourMediaPlayer.controlStyle = MPMovieControlStyleNone;
yourMediaPlayer.currentPlaybackTime = 0;
yourMediaPlayer.shouldAutoplay = FALSE;
[yourMediaPlayer play];
nextTag++;
}
Instead of trying to get MPMoviePlayerController play a list of items use AVQueuePlayer instead. It has a convenient advanceToNextItem method.
You can create a player already loaded with items (class AVPlayerItem) either with initWithItems or queuePlayerWithItems. Then just call play on it (inherited from AVPlayer) and it should play the items one after the other.
See:
AVQueuePlayer docs
AVPlayer docs
AV Foundation Programming Guide
AVPlayerItem docs

how do I get multiple coordinates for multi touch from a UIGesture

I have three gestures: 2-finger tap, 3-finger tap, and 4-finger tap. I need to get coordinates accordingly.
I have tried the following to get the coordinated of 2 fingers tap but app keeps crashing:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *twoTouch = [touches allObjects];
UITouch *tOne = [twoTouch objectAtIndex:0];
UITouch *tTwo = [twoTouch objectAtIndex:1];
CGPoint firstTouch = [tOne locationInView:[tOne view]];
CGPoint secondTouch = [tTwo locationInView:[tTwo view]];
NSLog(#"point one: %#", firstTouch);
NSLog(#"point two: %#", secondTouch);
[twoTouch release];
}
First of all, your application is not checking if there actually are two touches.
If you tap the screen with one finger you will get one touch in the "touches".
Try something like this.
if(touches.count > 1 && touches.count < 3)
{
// Your code for two touches.
}
Otherwise, the part where your program crashes is the [twoTouch objectAtIndex:1] because objectAtIndex:1 doesn't exist.
(I know this is a really old question but I answered it anyways.)

Enabling multi-touch in game?

I am creating a simple pong game in Xcode. I have a 2 player version and both paddles can be moved and function perfectly. The game is flawless except for the fact that you cannot move both paddles at the same time, rendering the 2 player mode useless.
How can I enable both paddles to be moved at the same time?
I've already tried selecting the "Multiple Touch" button in Interface Builder but it does nothing and im not quite sure it is even the correct route into enabling multi touch as I want it.
Also, my game is a View-Based Application if that matters.
Thanks!
EDIT: I mis-read the question. Added code snippet.
Here's the code I use to extract all touches when I get a touchesBegan event:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *touchesArray = [touches allObjects];
NSUInteger nNumTouches = [touchesArray count];
UITouch *touch;
CGPoint ptTouch;
for (int nTouch = 0; nTouch < nNumTouches; nTouch++)
{
touch = [touchesArray objectAtIndex:nTouch];
ptTouch = [touch locationInView:self.view];
// Do stuff with the touch
}
}
and similarly for touchesEnded and touchesMoved
From my experience (which isn't that much). You can create 2 UIViews for the 2 paddles, touching in one view will move one paddle, while touching in the other will move the other paddle. I hope this helps.
If you don't know how to split the views, you can simply make it identify 2 touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [[event touchesForView:zone1] anyObject];
UITouch *touch2 = [[event touchesForView:zone2] anyObject];
if (touch1)
touchOffset1 = paddle1.center.x - [touch1 locationInView:touch1.view].x;
if (touch2)
touchOffset2 = paddle2.center.x - [touch2 locationInView:touch2.view].x;
}
This you can use, it probably isn't the most productive, but it does work if you can't figure out how to split the touches.
self.view.multipleTouchEnabled = YES;
by default it is No

iTunes Scripting Bridge reveal does not work

The following code should show a certain track in iTunes:
NSString* iTunesPath = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:#"com.apple.iTunes"];
iTunesApplication *iTunes = nil;
if ( iTunesPath ) {
iTunes = [[SBApplication alloc] initWithURL:[NSURL fileURLWithPath:iTunesPath]];
[iTunes setDelegate:self];
}
iTunesSource *librarySource = nil;
NSArray *sources = [iTunes sources];
for (iTunesSource *source in sources) {
if ([source kind] == iTunesESrcLibrary) {
librarySource = source;
break;
}
}
SBElementArray *libraryPlaylist = [librarySource libraryPlaylists];
iTunesLibraryPlaylist *iTLibPlaylist = nil;
if ([libraryPlaylist count] > 0) {
iTLibPlaylist = [libraryPlaylist objectAtIndex:0];
}
SBElementArray *fileTracks = [iTLibPlaylist fileTracks];
iTunesFileTrack *track = [fileTracks objectAtIndex:4];
NSLog(#"Try to reveal track: %# at path :%#",[track description],[[track location] path]);
[track reveal];
Output:
Try to reveal track: <ITunesFileTrack #0x1364ed20: ITunesFileTrack 4 of ITunesLibraryPlaylist 0 of ITunesSource 0 of application "iTunes" (2474)> at path :/Users/...
But absolutely noting happens. What am I doing wrong?
(iTunes Version: 9.0.3)
The Library playlist doesn't exist anymore in the UI; it's there in the model, so it shows up in AppleScript, but trying to reveal it or anything in it won't do anything in the UI, as you saw. You can reproduce this in AppleScript as well (reveal track 5 of library playlist 1 of source 1).
The solution is to talk to the Music playlist, not the Library playlist. “Music” is the second playlist—playlist 2 in AppleScript, [[librarySource playlists] objectAtIndex:1] in Cocoa.
If you want to reveal a playing item in whatever playlist it's playing in, use reveal current track (which should be [[iTunes currentTrack] reveal], although I haven't tested that).
This helped me solve a related issue. Thanks.
I would recommend against using [[librarySource playlists] objectAtIndex:1] to get the playlist. It feels too much like guessing. You should also avoid iterating through the arrays from Scripting Bridge with a for loop.
This example solves both problems:
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
iTunesSource *library = [[[[iTunes sources] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"kind == %i", iTunesESrcLibrary]] objectAtIndex:0];
iTunesLibraryPlaylist *lp = [[[[library playlists] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"specialKind == %i", iTunesESpKMusic]] objectAtIndex:0];
[[library playlists] objectWithName:#"Music"] also works, but I’m not sure if that’s locale dependent (and the name could change in a future update).
Just to add to the answer of Rob McBroom, it would be even better to use firstObject instead of objectAtIndex:0. It will prevent an exception in case your query fails and returns an empty array. This will happen when you search for the Internet radio source (kind == iTunesESrcRadioTuner) and the Internet Radio library is disabled in the preferences.
iTunesApplication* iTunesApp = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
iTunesSource* radioTunerSource = [[[[iTunesApp sources] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"kind == %i", iTunesESrcRadioTuner]] firstObject];

Resources