I am trying to play a sound when I click a button, the sound clip is 10 sec, the application runs fine and when I click the button nothing happens there are no errors or nothing, this is my code
.m file
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
- (IBAction)Play:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"Blesgaes" ofType:#"mp3"];
NSURL *audioURL =[NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
.h file
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)Play:(id)sender;
I have added AVFoundation framework and I have followed these steps
http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/
http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_6)
http://code-and-coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html
Any ideas what I am doing wrong.
Below is the simple code:-
- (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!");
}
}
Related
Hi I am trying to play an audio file, but it doesn't work for some reason. I dont get any errors so i assume it must work but my audio doesn't play can u help me pls
NSString *path = #"%#/Elevator.mp3";
NSString *soundFilePath = [NSString stringWithFormat:path,[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 1; //Infinite
[player play];
I do have this line constantly in my console
skipping input stream 0 0 0x0
Try creating class attribute for a player:
#property (nonatomic, strong) AVAudioPlayer *player;
And then use it to play sound. Otherwise, your player will be deallocated right after the method where it was created finishes executing.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:yourUrlString]];
NSURLSession *session = [NSURLSession sharedSession];
task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
[self performSelectorOnMainThread:#selector(upadateCurrentTrack:) withObject:data waitUntilDone:NO];
}
}];
[task resume];
-(void)upadateCurrentTrack:(NSData *)data {
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
I created sample one for your question.It plays audio successfully now.
I forgot to say this.First set
ViewController.h
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h> //For AVPlayerViewController
#import <AVFoundation/AVFoundation.h> //For AVAudioPlayer
#interface ViewController : UIViewController
#property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionPlayAudio:(id)sender
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Elevator" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player play];
}
#end
I'm not sure why my AVAudioPlayer is always nil?
#import <AVFoundation/AVFoundation.h>
#property (nonatomic) AVAudioPlayer *audioPlayer;
#synthesize audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:#"%#/background-music-aac.caf", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// Create audio player object and initialize with URL to sound
NSError *audioError = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&audioError];
if (audioPlayer == nil) {
NSLog(#"Error? %#", audioError);
NSLog(#"AudioPlayer: %#", audioPlayer.description);
}
I feel like I have this all set up right, but it doesn't work and I'm getting nil/null for the audioPlayer.
The .caf file was the problem, thanks to Brandon for the help
I'm following in a tutorial on how to play mp3 sounds when pressing a button.
I created a button (playSound).
I added it to the view controller interface:
- (IBACTION)playSound:(id)sender;
in the implementation I declared the needed header files and I wrote the following:
#import "AudioToolbox/AudioToolbox.h"
#import "AVFoundation/AVfoundation.h"
- (IBAction)playSound:(id)sender {
//NSLog(#"this button works");
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3"];
//NSLog(#"%#", audioPath);
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
//NSLog(#"%#", audioURL);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
I'm not getting any errors. the NSlogs are logging the URL fine, and now I have no clue where to look further. I also checked if my MP3 sound was maybe damaged, but that was also not the case. all i hear is a little crackling noise for 1 second. then it stops.
You declared a local variable audioPlayer to hold a pointer to the player. As soon as your button handler returns the player is being released before it has a chance to play your sound file. Declare a property and use it instead of the local variable.
In YourViewController.m file
#interface YourViewController ()
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#end
or in YourViewController.h file
#interface YourViewController : UIViewController
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
#end
Then replace audioPlayer with self.audioPlayer in your code.
try this its working for me
in .h
AVAudioPlayer * _backgroundMusicPlayer;
in .m
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:#"Theme" ofType:#"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
[_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions
[_backgroundMusicPlayer setNumberOfLoops:-1];
[_backgroundMusicPlayer play];
Edited
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Name of your audio file"
ofType:#"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate=self;
[audioPlayer play];
Try this and let me know. Make sure you set the delegate for audioPlayer.
Firstly re add your music file in your project , then try this code
With the log you can see error.
NSError *error;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"bg_sound" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error){
//Print Error
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops: -1];
[audioPlayer play];
audioPlayer.volume=1.0;
}
Make sure your music files are properly added in project
After updating to xCode 4.6 my audio player crashes with no error logs. I cannot log the error as it crashes on play. I get no message on the debugger only a thread/ breakpoint on play.Anyone any ideas?
NSError *error;
NSString *stringPath1 = [[NSBundle mainBundle] pathForResource:#"beep" ofType:#"mp3"];
NSURL *url1 = [NSURL fileURLWithPath:stringPath1];
playerForRecording = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
[playerForRecording setDelegate:self];
[playerForRecording play];
http://i1107.photobucket.com/albums/h385/snksnk1/stack%20overflow/ScreenShot2013-02-05at54421PM_zps72842a8f.png
I'm using the same compiler and this works for me:
NSError *error;
NSString *stringPath1 = [[NSBundle mainBundle] pathForResource:#"beep" ofType:#"mp3"];
NSURL *url1 = [NSURL fileURLWithPath:stringPath1];
NSAssert(url1, #"URL is valid.");
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
if(!self.player)
{
NSLog(#"Error creating player: %#", error);
}
[self.player play];
Of course, in the .h, are the lines:
#import <AVFoundation/AVFoundation.h>
#interface myProgramViewController : UIViewController<AVAudioPlayerDelegate>
...
#property (nonatomic, strong) AVAudioPlayer* player;
with, in the .m, the corresponding:
#synthesize player = mPlayer;
Hope this helps.
I want to play a sound when a button is pressed. I tried this:
-(void)PlayClick
{
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"BubblePopv4"
ofType:#"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];
}
…and imported #import <AudioToolbox/AudioToolbox.h> and #import <AVFoundation/AVFoundation.h>
but for some reason the sound is not playing. The name and type are right because the file I added to the project is "BubblePopv4.mp3". The volume on the iPad is on maximum, and the sound plays fine on iTunes! Any ideas?
Leave it, Don't do it in a new Class, simple stuff for you:
- (void)playSoundWithOfThisFile:(NSString*)fileNameWithExtension {
// Eg - abc.mp3
AVAudioPlayer *audioPlayerObj;
NSString *filePath;
filePath= [[NSString stringWithFormat:#"%#", [[NSBundle mainBundle] resourcePath]] retain];
if(!audioPlayerObj)
audioPlayerObj = [AVAudioPlayer alloc];
NSURL *acutualFilePath= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",filePath,fileNameWithExtension]];
NSError *error;
[audioPlayerObj initWithContentsOfURL:acutualFilePath error:&error];
[audioPlayerObj play];
}
Do this:
In .h file:
Use this delegate - <AVAudioPlayerDelegate>
#import "AVFoundation/AVAudioPlayer.h"
AVAudioPlayer *audioPlayerObj;
NSString *filePath;
In .m file , In some method:
filePath= [[NSString stringWithFormat:#"%#", [[NSBundle mainBundle] resourcePath]] retain];
if(!audioPlayerObj)
audioPlayerObj = [AVAudioPlayer alloc];
And add these two methods:
- (id)initWithFileName:(NSString*)fileNameWithExtension {
if ((self = [super init])) {
NSURL *acutualFilePath= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",filePath,fileNameWithExtension]];
NSError *error;
[audioPlayerObj initWithContentsOfURL:acutualFilePath error:&error];
}
return self;
}
- (void)playSound {
[audioPlayerObj play];
}
You can pass the filename to this Method - initWithFileName
and then call playSound method to play sound for you.
Hope this helps.
Replace url initialization with:
NSURL* musicFile = [NSURL initFileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"BubblePopv4"
ofType:#"mp3"]];