How to call a method defined in another class - objective-c

I have a class named as Backgroundmusic.h file is
#import "UIKit/UIKit.h"
#import "AVFoundation/AVFoundation.h"
#import "AudioToolbox/AudioToolbox.h"
#interface Backgroundmusic : NSObject{
}
-(void)playBgmusic:(BOOL) issoundon;
#end
Backgroundmusic.m is
-(void)playBgmusic:(BOOL)issoundon {
//AVAudioPlayer *myExampleSound; //this variable can be named differently
if ( issoundon==TRUE) {
NSString *path =[[NSBundle mainBundle] pathForResource:"bg" ofType:#"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path]
,&soundID);
AudioServicesPlaySystemSound(soundID);
}
else {
NSString *path =[[NSBundle mainBundle] pathForResource:nil ofType:#"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path]
,&soundID);
AudioServicesPlaySystemSound(soundID);
}
}
Basically what i am trying to do is setting a background music to my app at the time of beginning and allow the user to stop it in the middle
so in my xAppdelegate.m i have called this function as
- (void)applicationDidFinishLaunching:(UIApplication *)application {
Backgroundmusic *bgm=[[Backgroundmusic alloc] init];
[bgm playBgmusic:TRUE];
}
but at this time my app terminates can someone tell me whats the issue here???

what the error message you are getting ? When application terminate you can check that in gdb window. It will show the proper error message. Post that error message in this thread.

Related

pointer loop to integer conversion?

The code is suppose to make a sound either yes or yes 3 play when clicked, but I get an error before I begin to debug that says rand cannot be statically allocated, but I can't put a * there because it's an integer. I am getting a conversion error when I do that. I also tried putting a * before sound, and that solved the error issue but when the button is clicked it wont play anything. So what do I do?
#import "ViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
#interface ViewController ()
#end
#implementation
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}//this is where the error breakpoint is that you show me how to make
- (IBAction)Yes {
NSInteger rand = arc4random_uniform(2);
NSString sound = rand == 0 ? #"yes" : #"yes 3";
NSURL *url = [[NSBundle mainBundle] URLForResource:sound withExtension:#"mp3"];
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
}
#end
I've seen this many times before, and it has to do with the fact that your audio player variable is being released before it has a chance to play the file.
You need to create a property for the audio player in your header:
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
Then in your method access that property instead of the local variable:
- (IBAction)YES {
NSInteger rand = arc4random_uniform(2);
NSString *sound = rand == 0 ? #"yes" : #"yes 3";
NSURL *url = [[NSBundle mainBundle] URLForResource:sound withExtension:#"mp3"];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.audioPlayer play];
}
Note that this has absolutely nothing to do with the sound variable you're messing with. You should be using the * to indicate a pointer when creating that variable, as shown above.

When I run my app in Xcode it crashes when I press a button with sound attached to it

So am making this app where if you press a button, a sound plays but when i pressed the button the app crashed. Heres me code
In the .h file
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController : UIViewController {
}
-(IBAction)buttonPressedWithSound:(id)sender;
#end
nothing wrong there, But in the .m file
-(IBAction)buttonPressedWithSound:(id)sender {
int randomSoundNumber = arc4random() % 4; //random number from 0 to 3
NSLog(#"random NR = %i", randomSoundNumber);
NSString *effectTitle;
switch (randomSoundNumber) {
case 0:
effectTitle = #"Come at me BRO!";
break;
case 1:
effectTitle = #"sound2";
break;
case 2:
effectTitle = #"sound3";
break;
case 3:
effectTitle = #"sound4";
break;
default:
break;
}
SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:effectTitle ofType:#"caf"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((CFURLRef)CFBridgingRetain(soundUrl), &soundID);
AudioServicesPlaySystemSound(soundID);
}
#end
this line here
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
well it said there was something wrong with it, this
2012-08-10 10:12:52.419 Sound +[1177:907] random NR = 1(lldb)
and
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
had a green highlight and said "Thread 1: breakpoint 1.1" to the side of it.
How can i fix it and can it to play the sound without crashing
You should use the NSSound class:
NSSound *sound = [[[NSSound alloc] initWithContentsOfFile: soundPath byReference: NO] autorelease];
[sound play];
Your code logged what you asked it to and then hit a breakpoint that you set in the code. Hit the continue button.

How do I load a sound file using initWithContentsOfURL:error:?

How do I gain access to a crashSound.wav file I've put in my Supporting Files folder of my xCode Project? Because the following code isn't working :
#interface Game()
{
// code...
AVAudioPlayer *crashSound;
// code...
}
#end
#implementation Game
- (id) init
{
// code...
NSURL *crashSoundFile = [[NSURL alloc] initWithString:#"crashSound" ]; // <--PROBLEM
crashSound = [[AVAudioPlayer alloc] initWithContentsOfURL:crashSoundFile error:NULL];
// code...
}
-(void) play // this is my "main" method that will be called once the playButton is pressed
{
[crashSound play];[crashSound play];[crashSound play];
}
#end
#"crashSound" is not a valid URL. You must supply an actual URL.
URLs to images, sounds, etc. (resources) in your project are not predictable at compile-time, and must be generated at run-time. This is easy using NSBundle.
NSBundle *bundle = [NSBundle mainBundle];
NSURL *crashSoundFile = [bundle URLForResource: #"crashSound" withExtension: #"wav"];
Ta da.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/soundfile.extension", [[NSBundle mainBundle] resourcePath]]];

AVAudioPlayer isPlaying crashing app

I'm using an AVAudioPlayer to manage some sounds but the isPlaying method seems to be crashing.
done when I initiate the page:
self.soundClass = [AVAudioPlayer alloc];
how I play the sound:
-(void)playSound:(NSString *)fileName:(NSString *)fileExt {
if ( [self.soundClass isPlaying] ){
[self.soundClass pause];
}
else if (newSoundFile == currentSoundFile) {
[self.soundClass play];
}
else {
NSLog(#"PlaySound with two variable passed function");
[[NSBundle mainBundle] pathForResource: fileName ofType:fileExt]], &systemSoundID);
[self.soundClass initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: fileName ofType:fileExt]] error:nil];
[self.soundClass prepareToPlay];
[self.soundClass play];
}
self.currentSoundFile = fileName;
}
My soundClass is pretty empty right now:
soundclass.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import<AVFoundation/AVFoundation.h>
#interface SoundClass : AVAudioPlayer <AVAudioPlayerDelegate> {
}
#end
SoundClass.m
#import "SoundClass.h"
#implementation SoundClass
-(void)dealloc {
[super dealloc];
}
#end
Do you see anything here I might be doing wrong? It crashes right at if ( isPlaying )
You point to space that is allocated for an instance of AVAudioPlayer in the first line you list, but you don't actually initialize an instance. In Cocoa, "alloc" is 99.9% of the time followed by some form of -init (like -initWithContentsOfURL:error: in the case of AVAudioPlayer).
Also, "soundClass" is an odd name for an instance variable. You should read up on the difference between a class (the blueprint of an object) and an instance of a class (an actual object built from the blueprint). Solid knowledge of this concept is critical to your understanding of Cocoa (and all object-oriented) programming.

AVAudioPlayerdelegate

I receive this error for this code-
warning: class 'BeatMaker' does not implement the 'AVAudioPlayerDelegate' protocol
-(IBAction)playBeat3 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"beat3" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
Can anyone help me with this?
You've told your AVAudioPlayer instance that your BeatMaker class implements the AVAudioPlayerDelegate protocol with this line:
theAudio.delegate = self;
But apparently your BeatMaker class hasn't told the compiler that it is actually an AVAudioPlayerDelegate. You would do that in the header file:
#interface BeatMaker : NSObject <AVAudioPlayerDelegate> { ... }
Then you would have to make sure that you have implemented all the required functions of the AVAudioPlayerDelegate protocol in your BeatMaker class.
In this case, there are no required functions for the protocol, so if you are just copying code and you don't actually want to recieve messages from theAudio, you can just delete the line assigning self to the delegate property of theAudio.