Trying to play a Random Sound using SimpleAudioEngine, no sound - iphone

So initially the app I was going to produce only made one sound upon swiping up. The code for that was
[[SimpleAudioEngine sharedEngine] playEffect:#"Swoosh.mp3"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:#selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;
I decided I want there to be three sounds that randomly play upon each swipe rather than just one. So here's what I did.
NSString *Soundname;
int randSound = arc4random() % 3;
switch (randSound) {
case 0:
Soundname = #"Swoosh1.mp3";
break;
case 1:
Soundname = #"Swoosh2.mp3";
break;
case 2:
Soundname = #"Swoosh3.mp3";
break;
}
[[SimpleAudioEngine sharedEngine] playEffect:#"Soundname"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:#selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;
I made the string, then replaced playEffect:#"Swoosh.mp3"]; with playEffect:#"Soundname"];
I've tried multiple variations of Soundname, none seem to me working. I'm fairy new at coding, I know this is something silly that i'm missing. Does anybody have any ideas ?

I'm pretty sure the problem is because you are still using a literal string for the playEffect parameter but your intent was to use the newly defined string variable.
Try changing:
[[SimpleAudioEngine sharedEngine] playEffect:#"Soundname"];
to this (note the lack of quotes):
[[SimpleAudioEngine sharedEngine] playEffect:Soundname];

Related

Morse Code via iPhone LED

I have coded an App that can convert normal Text like : "Hello my Name is XY"
into points and strokes ( ..-. ; --.- ; . ; - ; etc etc)
Now I want to Convert these points and stroke into light flashes with the lenght of 0.3 sec for points an 0.6 seconds for strokes. also there is a pause with the length of a point after each point or stroke, a double pause after each Word, and a tripple pause/break after each sentences.
The breaks are also implied into my code.
The problem is now that the light strokes aren't different enough.
Because the Idea behind it is to convert the Light flashes via an Arduino Duo and a fototransistor back to Text.
Here is the Code Passage for the Light converting Process:
- (IBAction)send:(id)sender{
// Converting Text to morsecode etc
float needTime;
NSString *string = plotter;
for (int d = 0;d < [string length]; d++) {
NSString *punktoderstrich = [string substringWithRange:NSMakeRange(d, 1)];
if ([punktoderstrich isEqualToString:#"."]) {
needTime = needTime + 0.4f;
[self performSelector:#selector(playpunkt) withObject:nil afterDelay:needTime];
}
if ([punktoderstrich isEqualToString:#"-"]) {
needTime = needTime + 1.0f;
[self performSelector:#selector(playstrich) withObject:nil afterDelay:needTime];
}
if ([punktoderstrich isEqualToString:#" "]) {
needTime = needTime + 0.4f;
[self performSelector:#selector(playpause) withObject:nil afterDelay:needTime];
}
if ([punktoderstrich isEqualToString:#"/"]) {
needTime = needTime + 0.3f;
[self performSelector:#selector(playpause) withObject:nil afterDelay:needTime];
}
}
- (void)torchAn {
[captureDevice lockForConfiguration:nil];
[captureDevice setTorchMode:AVCaptureTorchModeOn];
[captureDevice setFlashMode:AVCaptureFlashModeOn];
[captureDevice unlockForConfiguration];
}
- (void)torchAus {
[captureDevice lockForConfiguration:nil];
[captureDevice setTorchMode:AVCaptureTorchModeOff];
[captureDevice setFlashMode:AVCaptureFlashModeOff];
[captureDevice unlockForConfiguration];
}
-(void)playstrich{
// AudioServicesPlaySystemSound (outSystemSoundID2);
[self torchAn];
//[self performSelector:#selector(torchAus) withObject:nil afterDelay:0.8f];
}
-(void)playpunkt{
//AudioServicesPlaySystemSound (outSystemSoundID1);
[self torchAn];
//[self performSelector:#selector(torchAus) withObject:nil afterDelay:0.4f];
}
- (void)playpause{
// AudioServicesPlaySystemSound (outSystemSoundID3);
[self performSelector:#selector(torchAus) /*withObject:nil afterDelay:0.4f*/];
}
Like You see I also imported sound files (short and Long) but the Main target is to give a right light signal out.
My Problems:
Short lights are mostly okay, exept the first when the LED is firstly flashing.
Long light signals aren't really longer. Sometimes I get equal results when I'm recording them.
And after a Long light should light up the following short ones aren't short as normal.. hm..
After I had commented the Part with the sounds out, the whole process became more stable.
I also moved the Part (turning LED OFF) from the sign it self to the breaks.
I hope somebody can give me some tipps or so :)
Greets from Germany!
P.S.: My divice is an iPhone 4s (with torch ^^)
Like Brad said the iPhone flash needs a few ms to turn on. If you still want to use it the only possibility would be (in my eyes) to change your protocol and give the single signals more time.
This slows down your transmission but on the receiver-side you could define some confidence intervals (like plus-minus 2 seconds). Maybe a human beeing would not understand your morse code because it could be too slow (but maybe they will), a machine like the iPhone as a receiver would be perfectly able to understand it.
You have to play around with the time intervals to find the shortest ones that are working.
Greetings from GER.

Playing video when new view is loaded

I'm in the process of developing an iPhone app, and I'm running into a very minor issue. Essentially, when I load a certain view, I want the video to play. When the video is done playing it'll return to the view with a menu and some options. One of the options is to replay the video. Currently I have the replay video option working, but I'm unable to play the video when the view is loaded.
I've implemented a playMovie and moviePlayBackDidFinish method. I then placed [self playMovie] in the viewDidLoad method thinking it would call the playMovie method initially and thus play the movie when the view got loaded, but it doesn't seem to work.
If anyone could explain why this method of thinking doesn't work and also a proper way of doing this, it'd be greatly appreciated.
I would try viewDidAppear instead of viewDidLoad depending on your viewController. A view can often only load once, and you may be wasting that as it's loading in the background.
Also, Here's a sample movie Did Finish method, I'm wondering if you're releasing something you shouldn't be, or never telling the movie to stop properly:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason integerValue])
{
/* The end of the movie was reached. */
case MPMovieFinishReasonPlaybackEnded:
/*
Add your code here to handle MPMovieFinishReasonPlaybackEnded.
*/
break;
/* An error was encountered during playback. */
case MPMovieFinishReasonPlaybackError:
NSLog(#"An error was encountered during playback");
[self performSelectorOnMainThread:#selector(displayError:) withObject:[[notification userInfo] objectForKey:#"error"]
waitUntilDone:NO];
[self removeMovieViewFromViewHierarchy];
[self removeOverlayView];
[self.backgroundView removeFromSuperview];
break;
/* The user stopped playback. */
case MPMovieFinishReasonUserExited:
[self removeMovieViewFromViewHierarchy];
[self removeOverlayView];
[self.backgroundView removeFromSuperview];
break;
default:
break;
}
}

Play sound in Sequence in cocos2d

how to play sound in cocos2d in sequence does any body have any idea for example i have three sound like
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderOneSound]];
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.WithSound]];
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderTwoSound]];
this is the code of sound effect play how can i play when one is stop and then another play after that
CCSequence* sequence = [CCSequence actions:
[CCCallBlock actionWithBlock:^(void)
{
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderOneSound]];
}],
[CCDelayTime actionWithDuration:3],
[CCCallBlock actionWithBlock:^(void)
{
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.WithSound]];
}],
[CCDelayTime actionWithDuration:3],
[CCCallBlock actionWithBlock:^(void)
{
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderTwoSound]];
}],
nil];
[self runAction:sequence];
this way can solve your problem, but only drawback is you have to insert delay time by yourself. hope it work for your situation.

iOS: How to get my audio app to register in the multitask controls?

When a user double clicks the home button and swipe right, some audio controls shows.
How do I get use them?
I've searched but havn't found anything that helps me.
Try this
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playTapped];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self previousTapped];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextTapped];
break;
default:
break;
}
PlayTapped is the method for playing the music.
nextTapped is the method for the next song to be played.
previousTapped is for playing the previous track.
All the best

Remove sprite from screen cocos2d iphone?

I have a game that I wrote. I am about ready to call it finished but I found a bug. Basically the game gets slower as the longer you play. My guess is this is due to sprites that are still being drawn off screen. I will paste the code below but basically the sprite is created in the "addNewBall" method. In this method it is added to an array which calculates its motion. After the ball reaches a position where it is off the screen it is removed from the array which causes it to stop moving but it is still being "drawn" off screen. How do I remove the sprite so the processor no longer calculates it. Thanks in advance for your help!
Tanner
Code:
-(void) addNewBall {
NumberOfBalls = NumberOfBalls + 1;
int RandomXPosition = (arc4random() % 240) + 40;
NSString *BallFileString = #"OrangeBall.png";
switch (arc4random() % 5) {
case 1:
BallFileString = #"OrangeBall.png";
break;
case 2:
BallFileString = #"GreenBall.png";
break;
case 3:
BallFileString = #"YellowBall.png";
break;
case 4:
BallFileString = #"PinkBall.png";
break;
case 0:
BallFileString = #"BlueBall.png";
break;
}
Ball = [CCSprite spriteWithFile:BallFileString];
Ball.position = ccp(RandomXPosition, 520);
BallIsMoving = YES;
[self addChild:Ball z:10];
[AllObjectsArray_ addObject:Ball];
[BallArray_ addObject:Ball];
}
//And here is where it is removed...
if (Ball.position.y <= -100) {
[BallArray_ removeObject: Ball];
}
You seem to be missing some conditions in your removal method. Don't you also want to remove the ball if its y position is greater than the screen height, or if its x position is off-screen? At any rate, in the same place that you're removing the ball from the array, you should add:
[self removeChild:Ball cleanup: YES]
I should also point out that your BallArray is probably redundant, since you're adding all the balls to another node anyway. If the only children of that node are Balls, you can get the array of balls using its children property. In this case, the child array would be: self.children (See http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa for more info.)
If you have non-Ball children on the same node, you might want to add an intermediate node to simplify the design so that you can use one less array.
You said you are removing the objects from the arrays, but you didn't mention that you are also removing the sprite from the parent CCNode.
Check the methods from CCNode to remove childs: http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a0d4e615f688458c74001acf10f0ae011
You could use:
[Ball removeFromParentAndCleanup:YES];
This will remove the ball from it's parent CCNode and will remove all actions and callbacks.
You need to specify your sprite,and you can use this following line..
[self removeChild:Ball cleanup: YES]