Use IBOutlet in Function in Header File - objective-c

could you please tell me how i can use IBOutlets in a function stored in the header file:
.h file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface MainView : UIView <AVAudioPlayerDelegate> {
IBOutlet UILabel *SoundDescriptionLabel;
IBOutlet UIBarButtonItem *StopSoundButton;
}
- (IBAction)PlayFatGuyWalking:(id)sender;
- (IBAction)PlayRoadRunner:(id)sender;
- (IBAction)ShowInfoPopup:(id)sender;
- (IBAction)PlayChaosRunning:(id)sender;
- (IBAction)PlaySadTrombone:(id)sender;
- (IBAction)PlayBadJokeSound:(id)sender;
- (IBAction)PlayHorrorSynth:(id)sender;
- (IBAction)PlayLKWPeep:(id)sender;
- (IBAction)PlayUiToll:(id)sender;
- (IBAction)StopSound:(id)sender;
AVAudioPlayer *theAudio;
#end
//PlaySound Function
void playSound(NSString *filename,NSString *type){
[theAudio stop];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:type];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//HERE I WOULD LIKE TO DO THIS:
StopSoundButton.enabled=1;
//END OF WHAT I'D LIKE TO DO
[theAudio play];
}
Please help me
thx in advance

An IBOutlet has no impact or effect on your code - it's #defined out!
It is used as a suggestion to Interface Builder, which can parse the header, so that it knows that that variable can become linked to some graphical control in some way.

Related

How do i play many sounds selected by picker?

I want to make an app playing sound selected by picker.
When i select a file by picker, if i touch button, the sound will play.
But, it cannot.
I cannot point the miss of the code.
Anyone can help me to troubleshoot my code?
Here is my code for troubleshooting.
.H file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController <
UIPickerViewDelegate,UIPickerViewDataSource,AVAudioPlayerDelegate> {
NSArray *ap;
NSString *si;
UIButton * utab;
AVAudioPlayer *apr;
}
#property (weak, nonatomic) IBOutlet UILabel *ll;
#property (weak, nonatomic) IBOutlet UIImageView *igv;
#property (weak, nonatomic) IBOutlet UIPickerView *pv;
#property (weak, nonatomic) NSArray *msa;
#end
.M file
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property AVAudioPlayer *apr;
#end
#implementation ViewController
#synthesize ll,igv,pv;
- (void)viewDidLoad
{
[pv setDelegate:self];
[pv setDataSource:self];
[ll setText:[ap objectAtIndex:0]];
ap = [NSArray arrayWithObjects:#"AAA",#"BBB",#"CCC",#"DDD",#"EEE", nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#.m4a",[[NSBundle mainBundle] resourcePath]]];
NSError *error;
self.apr = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
apr.numberOfLoops = 2;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [ap count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component {
return [ap objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component {
si = [ap objectAtIndex:row];
[ll setText:si];
NSString *in = [NSString stringWithFormat:#"%#.jpg",si];
UIImage *tig = [UIImage imageNamed:in ];
[igv setImage:tig];
}
- (IBAction)utabu {
[self.apr play];
}
#end
Most likely the sound file could not be loaded. You should check the error variable after initializing the AVAudioPlayer.
The code for creating the URL objects is also wrong. You should use:
[[NSBundle mainBundle] URLForRessource:<filename> withExtension:"m4a"]
Also you would have to create a new AudioPlayer each time a sound file is selected.
Next time please use better variable names and not just two or three letter garbage.

Sound not playing in iOS Simulator. No errors

I have constructed some code that is supposed to play audio when a button is clicked. There are no errors in the code, but when I open the iOS Simulator and press the button, no sound plays. The button does the tap animation, but nothing happens.
I have both the AVFoundation and AudioToolbox frameworks.
I also have my audio file in resources.
I am not using breakpoints.
The button is set to use the 'playAudio:' first responder upon Touch Up Inside.
Here is my 'ViewController.h' file:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
And here is my 'ViewController.m' file:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
sleep(5);
};
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The audio segment is about 3 seconds.
Any help is much appreciated!
Thanks!
I am quite new to ios but I had the same problem and was able to fix it.
I simply declared my AVAudioPlayer variable as a global variable and called
[audioPlayer prepareToPlay];
before
[audioPlayer play];
Just calling prepareToPlay didnt fix it, I had to declare the global variable to for it to work.
Not sure why that makes a difference to the Simulator but it works now.
Play sound/music file in objective-c,
I had the same problem and was able to fix it with below code.
.h file
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)actionOnPlayButton:(id)sender;
- (IBAction)actionOnStopButton:(id)sender;
- (void)playSound ;
#end
.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
AVAudioPlayer *audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Play Music";
}
-(void)playSound{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound10"
ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (IBAction)actionOnPlayButton:(id)sender {
[self playSound];
}
****Used above lines of code, it will work fine...
Note:- Don't forget to import following framework:-**
#import
#import

How can i make a random sound array with objective c?

So i've done the tutorial where you code a button so that when you press it a sound plays. I'm trying to modify it so that when the button is pressed, a random sound plays.
here is the code:
viewcontroller.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface STViewController : UIViewController
- (IBAction)playAudio:(id)sender;
#property (nonatomic, strong) NSArray *sounds;
#end
viewcontroller.m
#import <AVFoundation/AVFoundation.h>
#import "STViewController.h"
#interface STViewController ()
#property (weak, nonatomic) IBOutlet UIButton *playAudio;
#end
#implementation STViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"Woof" ofType:#"mp3"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError];
if (!audioError) {
[audioPlayer play];
NSLog(#"Woof!");
}
else {
NSLog(#"Error!");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I've been dabbling with something along these lines
- (NSArray *)sounds
{
NSArray *sounds = [NSArray arrayWithObjects:
#"Woof.mp3",
#"Meow.mp3",
#"tweet.mp3",
#"Squeak.mp3",
#"Moo.mp3",
#"Croak.mp3",
#"Toot.mp3",
#"Quack.mp3",
#"Blub.mp3",
#"OWOwOw.mp3",
#"Fox.mp3",
nil];
return sounds;
}
but i'm not really sure how to make it random or even implement in in the code that I have going right now. Anyone have any ideas?
Just make it random try below:-
NSMutableArray *array=[NSMutableArray
arrayWithObjects:
#"Woof.mp3",
#"Meow.mp3",
#"tweet.mp3",
#"Squeak.mp3",
#"Moo.mp3",
#"Croak.mp3",
#"Toot.mp3",
#"Quack.mp3",
#"Blub.mp3",
#"OWOwOw.mp3",
#"Fox.mp3",
nil];
// now use exchangeobject with index api
int i=0;
for(i=0;i<=[array count]; i++)
{
NSInteger rand=(arc4random() %10);
[array exchangeObjectAtIndex:i
withObjectAtIndex:rand];
}

Why I get this error? At Xcode [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
I have two error : No visible #interface for ‘UIWebview’
Why I get this error at Xcode. Error is that:No visible #interface for 'UIWebView' declares the selector 'highlightAllOccurencesOfString:' and No visible #interface for 'UIWebView' declares the selector 'removeAllHighlights'. Where are wrong?
WBSecondViewController.h
#import <UIKit/UIKit.h>
#interface WBSecondViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>{}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIToolbar *webToolBar;
- (IBAction)searchButtonPressed:(id)sender;
- (IBAction)clearHighlights:(id)sender;
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
#end
WBSecondViewController.m
#import "WBSecondViewController.h"
#interface WBSecondViewController ()
#end
#implementation WBSecondViewController
-(IBAction)searchButtonPressed:(id)sender{
NSLog(#"highlighttes");
[_webView highlightAllOccurencesOfString:#"不明"];
}
-(IBAction)clearHighlights:(id)sender{
[_webView removeAllHighlights];
}
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"UIWebViewSearch" ofType:#"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:jsCode];
NSString *startSearch = [NSString stringWithFormat:#"uiWebview_HighlightAllOccurencesOfString('%#')",str];
[_webView stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [_webView stringByEvaluatingJavaScriptFromString:#"uiWebview_SearchResultCount"];
return [result integerValue];
}
- (void)removeAllHighlights
{
[_webView stringByEvaluatingJavaScriptFromString:#"uiWebview_RemoveAllHighlights()"];
}
#end
These two lines are wrong,
[_webView highlightAllOccurencesOfString:#"不明"];
[_webView removeAllHighlights];
It should be,
[self highlightAllOccurencesOfString:#"不明"];
[self removeAllHighlights];
You are trying to call highlightAllOccurencesOfString and removeAllHighlights which are defined in WBSecondViewController's #interface but on UIWebview objects. Compiler is not able to find it in UIWebView class #interface and hence the error message as No visible #interface for 'UIWebView' declares the selector ...
highlightAllOccurencesOfString and removeAllHighlights are method defined in your WBSecondViewController, while you are attempting to call them on a UIWebView object. Try with this:
-(IBAction)searchButtonPressed:(id)sender{
NSLog(#"highlighttes");
[self highlightAllOccurencesOfString:#"不明"];
}
-(IBAction)clearHighlights:(id)sender{
[self removeAllHighlights];
}
This will at least compile.

AudioPlayer not playing on a second view controller

I have a sound playing on buttons pushed from the main view controller which works fine. On the next view controller I also want a sound to play on buttons pushed but I'm not getting any sound. I set the two .h and .m files the same. What could my problem be? Thank you for any help.
my .m file:
#import "AboutView.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#interface AboutView ()
#end
#implementation AboutView
#synthesize support;
#synthesize facebook;
#synthesize kmbdev;
#synthesize back;
#synthesize player2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNi{
if (self) {
// Custom initialization
}
return self;
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: #"sound1" ofType: #"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.player2 = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: NULL];
player2.volume = .5;
[player2 prepareToPlay];
}
My .h file:
#import "ViewController.h"
#import <MessageUI/MessageUI.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#class ViewController;
#interface AboutView : UIViewController <MFMailComposeViewControllerDelegate, AVAudioPlayerDelegate>{
AVAudioPlayer *player2;
}
#property (strong, nonatomic) IBOutlet UIButton *back;
- (IBAction)back:(id)sender;
#property (strong, nonatomic) IBOutlet UIButton *support;
#property (strong, nonatomic) IBOutlet UIButton *facebook;
#property (strong, nonatomic) IBOutlet UIButton *kmbdev;
- (IBAction)email:(id)sender;
#property (strong, nonatomic) AVAudioPlayer *player2;
#end
I have [player2 play]; at my prepare for segue and IBaction as I do on the main view controller.
Add this code in ViewDidLoad method of 2nd controller:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: #"sound1" ofType: #"wav"];
if([[NSFileManager defaultManager] fileExistsAtPath:soundFilePath)
{
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
if(fileURL)
{
self.player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
self.player2.delegate = self;
self.player2.volume = 0.5f;
[self.player2. play];
}
}
else {
NSLog(#"File DoesNot Exists");
}