iOS app crashes - [NSURL initFileURLWithPath:] - ios

i am an amateur ios developer with version 4.6 of xcode and OSX 10.8.4. I am trying to make an app which plays sound but when i try to run the app i get this message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* - [NSURL initFileURLWithPath:]: nil string parameter'
My app also wont launch.
Here is the code for my view controller.h:
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;
#end
And here is my view controller.m:
Viewcontroller.m:
#import "ViewController.h"
#import <RevMobAds/RevMobAds.h>
#interface ViewController ()
{
AVAudioPlayer *avPlayer;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"98648__dobroide__20100606-mosquito" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
[avPlayer setNumberOfLoops:-1];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playButton:(id)sender {
[avPlayer play];
}
- (IBAction)stopButton:(id)sender {
[avPlayer pause];
}
#end

The problem is pathForResource is returning nil. Have you added that exact file name to your bundle?
Also, you should be testing for stringPath and error before continuing to load the player. For example:
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");
}

Related

How to save audio to documents in iPhone?

I tried to record an audio and save it to documents folder, but the program can only work in simulator. Once I run it in my iPhone(ios 8.3), it cannot work. Is there something wrong with my codes? Thank you.
Here is h file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVAudioRecorder.h>
#import <Accelerate/Accelerate.h>
#interface ViewController : UIViewController{
AVAudioPlayer *player;
AVAudioRecorder *recorder;
}
- (IBAction)REC:(id)sender;
- (IBAction)STOP_and_SAVE:(id)sender;
- (IBAction)PLAY:(id)sender;
#property (nonatomic, strong) NSString *Record_path1_string;
#end
m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
- (IBAction)REC:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.Record_path1_string = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"bbb.wav"];
recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath: self.Record_path1_string] settings:nil error:nil];
[recorder prepareToRecord];
[recorder record];
NSLog(#"recording!");
}
- (IBAction)STOP_and_SAVE:(id)sender {
[recorder stop];
}
- (IBAction)PLAY:(id)sender {
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: self.Record_path1_string] error:nil];
[player play];
}
#end
I have checked the Documents folder in simulator in my Mac, the audio file has been saved, but I cannot check Documents folder in my iPhone.

Array of sounds with .plist file?

Here is my code:
MainViewConroller.h
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
AVAudioPlayer *audioPlayer;
NSArray *soundsArray;
}
#property(nonatomic, retain) AVAudioPlayer *audioPlayer;
#property(nonatomic, retain) NSArray *soundsArray;
-(void)prepareSounds;
- (IBAction)playSound:(UIButton *)sender;
#end
Here is my MainViewController.m:
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize audioPlayer;
#synthesize soundsArray;
-(void)prepareSounds
{
NSString *filepath= [[NSBundle mainBundle] pathForResource:#"Sound" ofType:#"plist"];
self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
}
- (void)stopAudio
{
if (audioPlayer!= nil) {
[audioPlayer stop];
//do some task for changing the Image i.e setting the default image
}
}
- (IBAction)playSound:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
NSString *soundName = [soundsArray objectAtIndex:(btn.tag - 1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
if([audioPlayer isPlaying])
{
[self stopAudio];
}
[self.audioPlayer play];
}
- (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.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
#end
I have 3 buttons. In the 'Supporting Files' folder I have an array of strings with the name of the sound files I want to play, named 'Sound.plist', and in the 'Supporting Files' folder I have a folder named 'Sounds', which contains the sound files.
All of my buttons play the same sound, even when I delete the .plist file. It seems to always play the sound that is first in the 'Sounds' folder. Can someone please provide some insight, I feel like I have tried everything. Thanks!

playing a mp3 file in iOS app [duplicate]

This question already has answers here:
How do I programmatically play an MP3 on an iPhone?
(4 answers)
Closed 4 years ago.
I can't figure out how to play a music file in an iPhone game.
Im creating a Game and I am trying to figure out how to play music whenever the app is launched.
I tried this:
- (void)awakeFromNib {
NSString *path = [[NSBundle mainBundle] pathForResource:#"musicamenu" ofType:#"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
This is how you do it. In your v1AppDelegate.h file add
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface v1AppDelegate : UIResponder <UIApplicationDelegate>
{
AVAudioPlayer *myAudioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *myAudioPlayer;
#property (strong, nonatomic) UIWindow *window;
#end
Now in your v1AppDelegate.m file add this
#import "v1AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#implementation v1AppDelegate
#synthesize window = _window;
#synthesize myAudioPlayer;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Soothing_Music2_Long" ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
// Override point for customization after application launch.
return YES;
}
If you wish to stop or start this music from anywhere else in your code then simply add this
#import "v1AppDelegate.h"
- (IBAction)stopMusic
{
v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.myAudioPlayer stop];
}
- (IBAction)startMusic
{
v1AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.myAudioPlayer play];
}
I recommend to add the play music method in applicationDidBecomeActive: method. Because you want the music played every time the app is launched. Note you should hold a reference to the player. Else the music will not be played.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Play music on another queue so that the main queue is not blocked.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self playMusic];
});
}
- (void)playMusic
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"done" ofType:#"mp3"];
NSError *error = nil;
NSURL *url = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.player play];
}
For Swift 3.1 :
Use this two imports :
import AVFoundation
import AudioToolbox
Create a reference for your AVAudioPlayer :
private var mAudioPlayer : AVAudioPlayer?
Use this function for play a sound you are storing in your app :
func onTapSound(){
let soundFile = Bundle.main.path(forResource: "slap_placeholder.wav", ofType: nil)
let url = URL(fileURLWithPath: soundFile!)
self.mAudioPlayer = try! AVAudioPlayer(contentsOf: url as URL)
self.mAudioPlayer?.play()
}

iOS button not playing audio file on button tap

This is a newbie question.
I have a very simple app that is supposed to only play an audio file when a button on the main view is tapped.
I am using XCode version 4.6.
I added an audio file 'audioclip.wav' to my project. I have added the AVFoundation.framework.
My project only has ViewController.h and ViewController.m.
I double clicked and dragged from the button in the storyboard to the .h file using the assistant editor to create my IBAction connection.
My ViewController.h:
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController <AVAudioPlayerDelegate> {
}
- (IBAction)playAudio:(id)sender;
#end
My ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audioclip" ofType:#"wav" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
#end
For whatever reason (probably something silly I left out) The audioclip.wav file does not play when I click the button. I have tested on the iPhone 6.1 simulator and on my device.
I think there is some problem with wav files. Some people seem to get it to work, but I've tried several different files and none of them play. The mp3 and m4a files that are commented out in the code below worked fine. There's no need to do anything with a delegate, it's optional.
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (strong,nonatomic) AVAudioPlayer *audioPlayer;
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
//NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"11 Prayer" ofType:#"mp3" ];
//NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"Sunshine" ofType:#"m4a" ];
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"space" ofType:#"wav" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
If I log the error, with the wav file I get:
Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn’t be completed. (OSStatus error 1685348671.)"
Please try to use this one..
- (IBAction)playAudio:(id)sender
{
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"audioclip" ofType:#"wav"]];
NSData *data =[NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
audioPlayer.delegate = self;
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
i hope this will help u
try like this ,
NSString *audio=[[NSBundle mainBundle]pathForResource:#"audioclip" ofType:#"wav"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:audio];
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
avPlayer.delegate = self;
[avPlayer prepareToPlay];
[avPlayer play];
and once check IBOutlet connected or not and check the button action method with breakpoints.
As i can see you have made the code quite simple, so the way it's made does require that it is not a big wav file.
And by that you need to cut down the audio file to something like 10 seconds and under.
I was using the .m4a when I encountered this problem. For me, not implementing AVAudioSession was the issue
var session = AVAudioSession.sharedInstance()
do {
session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.setActive(true)
} catch {
gf.displayAlert("problem encountered", userMessageTitle: "")
self.navigationController?.popViewControllerAnimated(true)
}

AVAudioPlayer not playing my mp3

I'm trying to get a short audio file (mp3) to play in my app. Here is the code i'm using:
AVAudioPlayer *AudioPlayer;
NSError *error;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"filename"
ofType:#"mp3"]];
AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
AudioPlayer.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
if (error)
{
NSLog(#"Error: %#",
[error localizedDescription]);
}
else
{
[AudioPlayer play];
}
I don't really know what i'm missing, the examples i've followed seem to match what i'm doing.
edit: I should also mention that the code runs without error, there is a try catch around this.
I had a similar problem due to ARC. Instead of defining the AVAudioPlayer in the same method you are using it, you should should have an instance variable somewhere else, such as the UIViewController. This way ARC doesn't auto release this object and audio can play.
I've made some additions and got it work:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface FirstViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *data;
UIButton *playButton_;
}
#property(nonatomic, retain) IBOutlet UIButton *playButton;
-(IBAction)musicPlayButtonClicked:(id)sender;
#end
#import "ViewController.h"
#implementation FirstViewController
#synthesize playButton=playButton_;
- (IBAction)musicPlayButtonClicked:(id)sender {
NSString *name = [[NSString alloc] initWithFormat:#"09 No Money"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:#"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data play];
}
- (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
In Interface Builder I've added a button and connected it with IBOutlet and musicPlayButtonClicked IBAction.
And don't forget to add AVFoundation framework to your project.
ps
I'm really sorry for my english, please, be indulgent.
I had this problem myself, until I found out that the sound was not coming through the speakers (instead - it was redirected to the calls speaker). Try adding this to redirect the sound through the speakers:
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute);

Resources