AVAudioPlayer doesn't work with (NSString *)filename - objective-c

I want to implement a player that plays sounds and/or music. I have these two methods:
-(void)playSound:(int)soundIndex
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[SoundFxFilenameArray objectAtIndex:soundIndex] ofType:#"mp3"]];
if(FxPlayer)
{
[FxPlayer release];
FxPlayer = nil;
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
FxPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[FxPlayer play];
}
-(void)playMusic:(NSString *)filename
{
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:filename ofType:#"mp3"]];
if(MusicPlayer)
{
[MusicPlayer release];
MusicPlayer = nil;
}
MusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[MusicPlayer play];
}
Now, [[SoundPlayer sharedManager] playSound:FXIndex_Default] works just fine. When I try to play music though, [[SoundPlayer sharedManager] playMusic:#"music_file_name_here" just doesn't do anything. Note that both the sound file and the music file are of the same format (mp3).
Also, the "sharedInstance" is because I implemented the singleton pattern for this audio player.
Any pointers or ideas on what is so wrong in my code so that I can play sounds but not music?

Use fileURLWithPath instead of URLWithString in playMusic:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filename ofType:#"mp3"]];
URLWithString expects a complete URL such as "file://path/to/file.mp3" as argument, fileURLWithPath expects just a file path.

Related

Play sound when app is in background or screen is locked

I have a timer that plays a sound when time reaches 0, but now I can't understand how to fire the event in order to play the sound if the app is in background mode or the screen is locked, What I found searching on the web was to set "UIBackgroundModes" in my .plist file, and add "audio" as array member.
UIBackgroundModes Array
Item0 String audio
Then add in the app delegate the following code:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
But the sound doesn't get played.. So I was wondering if it's possible to do something like this or I should change method
EDIT:
I imported:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
And this is the code I use to create the sound:
AVAudioPlayer *player;
NSString *soundFilePath = [NSString stringWithFormat:#"%#/clock.mp3",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1;
dispatch_async(dispatch_get_main_queue(), ^{
[player prepareToPlay];
});
Then to play it:
[player play];
I have just a snippet of Swift code but it should be easy to convert it to Obj-C
Try to use AVPlayerItem init with URL and then set AVPlayer init with PlayerItem
let assetUrl = self.nowPlayingItem!.associatedItem!.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL
let playerItem = AVPlayerItem(URL: assetUrl)
if let player = self.player {
player.replaceCurrentItemWithPlayerItem(playerItem)
} else {
self.player = AVPlayer(playerItem: playerItem)
}
self.player!.play()
and for this line of code:
NSString *soundFilePath = [NSString stringWithFormat:#"%#/clock.mp3",
[[NSBundle mainBundle] resourcePath]];
Have you tried:
[[NSBundle mainBundle] URLForResource:#"song" withExtension:#"mp3"];
// Create a new player item
NSURL *assetUrl = [nowPlayingItem valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:assetUrl];
// Either create a player or replace it
if (self.player) {
[self.player replaceCurrentItemWithPlayerItem:playerItem];
} else {
self.player = [AVPlayer playerWithPlayerItem:playerItem];
}
nowPlayingItem is here a MPMediaItem but just replace NSURL *assetUrl = [nowPlayingItem valueForProperty:MPMediaItemPropertyAssetURL]; by NSURL *assetUrl =[[NSBundle mainBundle] URLForResource:#"song" withExtension:#"mp3"];

Getting my AVAudio player to work

I'm trying to use AVAudioPlayer to play an mp3. Here is the code that I have:
- (void) playClip {
NSString *mp3File = [[NSBundle mainBundle] pathForResource:#"inFavor" ofType:#"mp3"];
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3File] error:NULL];
[avPlayer play];
}
the name of the file that I'm referencing is "inFavor.mp3"
I'm a bit new to Objective-C so I'm not exactly sure why this isn't working. I'm using Xcode 5.
Make the AVAudioPlayer a property of the class so that it is not released when the function goes out of scope.
#property (strong, nonatomic) AVAudioPlayer* avPlayer;
Then initialize with
self.avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3File] error:NULL];
try this code, you need to check the error. paste the log you are getting in error
NSError *error;
NSString *path = [[NSBundle mainBundle] pathForResource:#"inFavor"
ofType:#"mp3"];
AVAudioPlayer* avPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:&error];
if (error) {
NSLog(#"failed playing error: %#", error);
}else{
//avPlayer.delegate = self;
[avPlayer play];
}

The code plays BGM on only 64bit mode

I want to play BGM. The below code plays BGM on only 64bit mode. What should I do?
- (void)playBgmLoop
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"aikatsu" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// Infinite loop
audio.numberOfLoops = -1;
[audio play];
}
#end
Thank you for your help.

How to play audio, even when mute button is on?

I'm trying to play an audio file when a button is tapped.
If I step through my code it works, but if I just tap on the button no sound is played.
Here is my code:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
AVAudioPlayer * player;
NSString * path = [[NSBundle mainBundle] pathForResource:#"soundOne" ofType:#"mp3"];
NSURL * url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player play];
Is this the appropriate way to play audio? Nothing is being flagged as being deprecated.
I also see this message in the debugger:
AudioQueue: request to trim 0 + 2690 = 2690 frames from buffer containing 1152 frames
EDIT:
This code works.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"soundOne"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[self.audioPlayer prepareToPlay];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
How can I play audio with the mute button on?
Here is the code I have tried for this:
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
//NSLog(#"audio route: %#", state);
return (CFStringGetLength(state) <+ 0);
}
return NO;
Stick this before you call play:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
That should do it.

background music

(using AVFoundation.framework)
#import "AVFoundation/AVFoundation.h"
So I got a problem, I tried this code first in init method of my class (that is the one that is loaded first)
NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:#"music" ofType:#"mp3"];
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops=-1;
[player play];
But it didn't work, than I decided to do it in thread:
in init method:
[NSThread detachNewThreadSelector:#selector(playMusic) toTarget:self withObject:nil];
and method itself:
-(void) playMusic {
NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:#"music" ofType:#"mp3"];
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops=-1;
[player play];
}
it is declared in *.h file of course. It also didn't work.
I tried to debug, on line
}
of playMusic method the file is played for 2 or 3 seconds and then stops. It isn't played without debug. What is the problem?
The AVAudioPlayer will be deallocated immediately. You need to retain the player.
So make the AVAudioPlayer an instance variable of the class.