I am an amateur developer making an ios app which includes a sound file (mp3) being played when a button impressed. However, the sound is not playing (tested on a mobile device, not ios simulator). I already have the sound file in bundle resources and have added AVFoundation to link binary with libraries. I have xcode 4.6 and have mac osx 10.8.4. Here is my code for viewcontroller.h:
#import <UIKit/UIKit.h>
#import <RevMobAds/RevMobAds.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController
- (IBAction)playButton:(id)sender;
- (IBAction)stopButton:(id)sender;
@property (nonatomic, strong) AVAudioPlayer *avPlayer;
- (void)revmob;
@end
Viewcontroller.m:
#import "ViewController.h"
#import <RevMobAds/RevMobAds.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[RevMobAds session] showBanner];
// Do any additional setup after loading the view, typically from a nib.
NSString *stringPath = [[NSBundle mainBundle] pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];
if (stringPath)
{
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
_avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if (!error)
{
[_avPlayer setNumberOfLoops:-1];
}
else
{
NSLog(@"Error occurred: %@", [error localizedDescription]);
}
}
else
{
NSLog(@"Resource not found");
}
[self performSelector:@selector(revmob) withObject:nil afterDelay:10.0];
}
- (void)revmob
{
[[RevMobAds session] showFullscreen];
}
- (IBAction)playButton:(id)sender
{
[_avPlayer play];
NSLog(@"Sound playing");
}
- (IBAction)stopButton:(id)sender
{
[_avPlayer pause];
NSLog(@"Sound Stopped");
}
@end
I've tested your code on my Mac and it seems to work just fine. It could be that the cause for your problem is that while your mp3 is visible in Xcode, it's not being added to the bundle during compilation. Like in this question.
To fix it, click the mp3 in Xcode, look at the File Inspector and check the Target Membership box next to your target (in the screenshot mine is SOTest).