Reputation: 31
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!
Upvotes: 1
Views: 1944
Reputation: 3327
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
Upvotes: 1
Reputation: 687
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.
Upvotes: 1