MP3 file not playing in Iphone/Ipad - iphone-sdk-3.0

I am palying MP3 files in my application using code
NSString *filePath = [[NSBundle mainBundle] pathForAuxiliaryExecutable:#"congratulation.MP3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[audioPlayer prepareToPlay];
audioPlayer.currentTime = 0;
[audioPlayer play];
I am playing 15 MP3 files
All playing well, but one file is not playing it is playing very well i itunes but not in my app
Any solution for this strange problem.
Amit Battan

I have to replace the MP3 files

Related

How to play a wav file placed in Documents directory in OSX using objective c

I have been trying to play an audio file but unable to succeed.
SoundFilePath:
/Users/admin/Library/Containers/com.abc.myApp/Data/Documents/MyAudioSample1.wav
NSSound:
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:soundFilePath byReference:YES];
sound.volume = 1;
sound.delegate = self;
[sound play];
Only once out of 5 times my audio is played.
AVFoundation:
NSURL* file = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];
Add [audioPlayer play];
You are preparing to play but not playing it. After adding above line your code will be:
NSURL* file = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
Hope it helps

AVAudioPlayer or PlaySystemSoundID?

I have an iOS app which is going to provide a sound pad for the user - i.e. when they tap a button a short sound clip is played.
At the moment, the app is designed around using SystemSoundID.
Would it now be advised to use AVAudioPlayer now over this? If so, what would be the ideal code to use? Would it be the standard:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"yoursoundfile"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];

Play Audio iOS Objective-C

I'm trying to get an audio file playing in my iOS app. This is my current code
NSString *soundFilePath = [NSString stringWithFormat:#"%#/test.m4a", [[NSBundle mainBundle] resourcePath]];
NSLog(#"%#",soundFilePath);
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer setVolume:1.0];
audioPlayer.delegate = self;
[audioPlayer stop];
[audioPlayer setCurrentTime:0];
[audioPlayer play];
I've being checking a bunch of other posts but they all generally do the same thing. I'm not getting an error, but I don't hear any audio playing on the simulator or device.
This is the path I get for the audio file on the simulator
/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a
Any help is appreciated!
Maybe you should try using the NSBundle method pathForResource:ofType: method to create the path to the file.
The following code should successfully play an audio file. I have only used it with an mp3, but I imagine it should also work with m4a. If this code does not work, you could try changing the format of the audio file. For this code, the audio file is located in the main project directory.
/* Use this code to play an audio file */
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
EDIT
Ok, so try this:
NSString *soundFilePath = [NSString stringWithFormat:#"%#/test.m4a",[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
Also, make sure you do the proper imports:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
You need to store a strong reference to 'AVAudioPlayer'
#property (strong) AVAudioPlayer *audioPlayer;
And if you are sure that your audio file is in following Bundle Resources, it should play.
Project>Build Phases>Copy
To play file with extension .caf, .m4a, .mp4, .mp3,.wav, .aif
Download following two files from GitHub
SoundManager.h
SoundManager.m
Add these files to your project
Also add audio files to resource folder (sample files)
mysound.mp3, song.mp3
Import header file in desired file
#import "SoundManager.h"
And add following two lines in -(void)viewDidLoad
[SoundManager sharedManager].allowsBackgroundMusic = YES;
[[SoundManager sharedManager] prepareToPlay];
Use following line to play sound (mysound.mp3)
[[SoundManager sharedManager] playSound:#"mysound" looping:NO];
Use following line to stop the sound (mysound.mp3)
[[SoundManager sharedManager] stopSound:#"mysound"];
Also you can play music (song.mp3) as
[[SoundManager sharedManager] playMusic:#"song" looping:YES];
And can be stop music as
[[SoundManager sharedManager] stopMusic];
Complete Documentation is on GitHub
First import this framework to your file
#import <AVFoundation/AVFoundation.h>
Then declare an instance of AVAudioPlayer.
AVAudioPlayer *audioPlayer;
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 play];
To play your bundle audio file using below code if you generate the system sound ID
for(int i = 0 ;i< 5;i++)
{
SystemSoundID soundFileObject;
NSString *audioFileName = [NSString stringWithFormat:#"Alarm%d",i+1];
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: audioFileName
withExtension: #"caf"];
// Store the URL as a CFURLRef instance
CFURLRef soundFileURLRef = (__bridge CFURLRef) tapSound;
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
[alarmToneIdList addObject:[NSNumber numberWithUnsignedLong:soundFileObject]];
}
You can play the sound by using below code
AudioServicesPlaySystemSound([[alarmToneIdList objectAtIndex:row]unsignedLongValue]);
To achieve this below framework need to add in your Xcode
AudioToolbox
Finally below header files are needs to be import in controller
#import AudioToolbox/AudioToolbox.h
#import AudioToolbox/AudioServices.h
Updated for Swift 4
Playing from main bundle
- iOS 11 only
import AVFoundation
var player: AVAudioPlayer?
The following code loads the sound file and plays it, returning an error if necessary.
guard let url = Bundle.main.url(forResource: "test", withExtension: "m4a") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
guard let player = player else { return }
player.play()
} catch let error {
// Prints a readable error
print(error.localizedDescription)
}
Swift 4.2
var soundFilePath = "\(Bundle.main.resourcePath ?? "")/test.m4a"
var soundFileURL = URL(fileURLWithPath: soundFilePath)
var player = try? AVAudioPlayer(contentsOf: soundFileURL)
player?.numberOfLoops = -1 //Infinite
player?.play()

How to play system button click sound on iPad?

I need to play some system sound when users click button in my application which is running on iPad. How to implement it in iOS?
If you want to play a short sound (shorter than 30 sec), you can do it easily like this:
Note: You'll have to add AudioToolbox framework and import it (#import <AudioToolbox/AudioToolbox.h>)
SystemSoundID mySSID;
NSString *path = [[NSBundle mainBundle] pathForResource:#"beep" ofType:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: path], &mySSID);
AudioServicesPlaySystemSound(mySSID);
Also note that the file can be:
No longer than 30 seconds in duration
In linear PCM or IMA4 (IMA/ADPCM) format
Packaged in a .caf, .aif, or .wav file
You should use AVAudioPlayer.
There's a great tutorial here on using AVAudioPlayer to play sounds. A very simple example of it's use:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3",dataPath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
NSString *path = [[NSBundle mainBundle] pathForResource:#"gorilla 2" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];

.mp3 and .wav files works on simulator, but not on device

I have very common question as i searched for it but cant get the correct answer for me.
here is the piece of code on which I m trying hard to play Audio files.
in viewDidLoad method:
NSURL *buttonSound = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/button4.wav", [[NSBundle mainBundle] resourcePath]]];
NSURL *correctSound = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/key_drop1.wav", [[NSBundle mainBundle] resourcePath]]];
NSURL *wrongSound = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/sound8.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
ButtonPressedSound = [[AVAudioPlayer alloc] initWithContentsOfURL:buttonSound error:&error];
ButtonPressedSound.numberOfLoops = 0;
correctAnsweredSound = [[AVAudioPlayer alloc] initWithContentsOfURL:correctSound error:&error];
correctAnsweredSound.numberOfLoops = 0;
wrongAnsweredSound = [[AVAudioPlayer alloc] initWithContentsOfURL:wrongSound error:&error];
wrongAnsweredSound.numberOfLoops = 0;
in IBAction of pressing a button m calling "play" in this way.
[ButtonPressedSound play];
[self performSelector:#selector(stop) withObject:nil afterDelay:0.50];
[correctAnsweredSound play];
[self performSelector:#selector(stop) withObject:nil afterDelay:0.50];
[wrongAnsweredSound play];
[self performSelector:#selector(stop) withObject:nil afterDelay:1.0];
my play and stop methods are :
-(IBAction)play
{
self.ButtonPressedSound.currentTime = 0;
[self.ButtonPressedSound play];
self.correctAnsweredSound.currentTime = 0;
[self.correctAnsweredSound play];
self.wrongAnsweredSound.currentTime = 0;
[self.wrongAnsweredSound play];
}
-(IBAction)stop
{
[self.ButtonPressedSound stop];
[self.correctAnsweredSound stop];
[self.wrongAnsweredSound stop];
}
what I have done so far...
I have searched to these links but not get the answer for my code.
AVAudioPlayer only plays in simulator but not device why?! (iPhone-SDK)
mp3 Sounds Playing on simulator but not on Device (Audio BeatBox)
http://www.icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/
...........
I have checked all the names n there is not any typo mistake.
Dont know where m getting wrong , pls help !!!
I've had similar problems before, but this solution is just a random guess that worked for me at the time.
On the real iPhone, file paths are case-sensitive. On the simulator, they are not. So I would check to make sure your file paths are exactly correct, including case.