AVAudioplayer not playing song from iPod library in iOS 5 - ios

I am creating an iPhone application using AVAudioPlayer. It is working fine in iOs 6 devices. But it is not playing song from iPod library in iOs 5 and lower versions. When I tried to play a music file from NSBundle it is working, so the issue is only when I try to play the song from iPod library. Here is my code, please check
if (isPlaying) {
[audioPlayer stop];
isPlaying = NO;
}
extern NSMutableArray *songsUrlArray;
extern int selectedSongIndex;
audioPlayer.volume = 1.0;
NSString *urlString = [NSString stringWithFormat:#"%#",[songsUrlArray objectAtIndex:selectedSongIndex]];
NSURL *url = [NSURL URLWithString:urlString];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(#"%#",[error description]);
else{
[audioPlayer play];
isPlaying = YES;
}
Edit:
Here is the error message that I got,
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"
When I googled, I found that OSStatus error -43 is getting when there is no contents available in the specified path. Here is the path to a song from iPod library in the 3GS device, ipod-library://item/item.mp3?id=564371652689620079. But its not playing. Please some
Please help

AVAudioPlayer only gained support for iPod URLs in iOS6. You'll have to use AVPlayer or AVQueuePlayer if you want to support target pre-iOS6 devices.
https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS6.html#//apple_ref/doc/uid/TP40011812-SW1

Related

iOS Streaming mp3

I've been searching for a good tutorial for playing and streaming audio finally I found this which seems to offer an offline audio playing.
in the YMCAudioPlayer class I've commented loading a resource and provided NSURL* generated by a direct link instead like so.
- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension
{
//NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:audioFile withExtension:fileExtension];
NSURL *audioFileLocationURL = [NSURL URLWithString:#"http://188.95.64.47/AghaninaDownload/Content/per_singer/JFire/Audios/J-FirE_and_Omar_khalid_wlaKelmeh-sample.mp3"];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
}
And I've ensured that it's a valid link, but seems not to play on emulator. what have I missed there?
EDIT
It generates the following error
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)
Might be a stupid question, but did you make it play ?
[self.audioPlayer play];
Then I would try this, to ensure your settings are correct :
[self.audioPlayer prepareToPlay];
[self.audioPlayer setCurrentTime:0.0];
[self.audioPlayer setVolume:1.0];
[self.audioPlayer play];
You can also use the delegate of AVAudioPlayer to check if something happens when you play.
Else it might be an URL of format issue.
I think you cannot play audio on EMULATOR, you have to test it on device.
Here mentioned on apple developer guid -
https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/iOS_Simulator_Guide/TestingontheiOSSimulator/TestingontheiOSSimulator.html#//apple_ref/doc/uid/TP40012848-CH4-SW1
I've found the answer in the accepted answer here
"I was able to fix the issue by loading the file first into an NSData element and then using that to initialize the AVAudioPlayer instead, like so:"
NSData *songFile = [[NSData alloc] initWithContentsOfURL:songCacheURL options:NSDataReadingMappedIfSafe error:&error1 ];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:songFile error:&error2];

AVAudioPlayer initWithContentsOfURL returns nil for AAC/M4A files

I use the following code to play an audio file. It plays fine for MP3 files, but when I try to play an AAC file, the [[AVAudioPlayer alloc] initWithContentsOfURL:] returns nil and I get the following error:
Error Domain=NSOSStatusErrorDomain Code=1937337955 "The operation couldn’t be completed. (OSStatus error 1937337955.)"
The audio file plays fine on Mac and iPhone (when I email it to myself) and is here: https://dl.dropboxusercontent.com/u/2667666/song.aac
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
// Create audio player with background music
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:#"song" ofType:#"aac"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
if (!_backgroundMusicPlayer) {
NSLog(#"_backgroundMusicPlayer pointer is null!!");
}
[_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions
[_backgroundMusicPlayer prepareToPlay];
[_backgroundMusicPlayer play];
Update: If I change the extension of the file from aac to m4a, the error code change to 1954115647 (whose four letter code is "tip?"). The four letter code for the error code that I get with the arc extension is "sync".
Initializing the audio player with initWithData:error: solves this.
For example:
NSData* data = [NSData dataWithContentsOfURL:url] ;
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:outError];
Disappointing that apple initializes their audio player based on extension, instead of looking at the binary data and trying to figure out which type of data they received.
Found the solution!
Turns out that if I simply use AVPlayer instead of AVAudioPlayer and if I use .acc as the extension of the file, then it plays just fine. now I can play both aac and mp3 files.
.aac file can be played,try to check whether the file exists?
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: backgroundMusicPath ] == YES)
{
NSLog(#"find file");
}

xcode 5 no sound from mp3 on iOS 6 or earlier

The app plays some mp3 files and is working well on iOS 7, but on iOS 6 or earlier, the mp3 files are not played or at least I can hear nothing.
This is the code I use for playing mp3:
fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/puerta.m4a", [[NSBundle mainBundle] resourcePath]]];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
// Preloads the buffer and prepares the audio for playing.
[self.audioPlayer prepareToPlay];
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
Does anyone had a similar issue and how fixed it - Many thanks for help.

iPodMusicPlayer's bug under ios 6.0.1?

I use [MPMusicPlayerController iPodMusicPlayer] setting the ipod volumn before playing a local "beep.caf" file which use AVAudioPlayer.
Strange things happened, if I kill the system ipod app before running the following code, the beep.caf played just 1~2 second which should be a 30 second audio.
The code like this:
self.ipodPlayer = [MPMusicPlayerController ipodMusicPlayer];
self.ipodPlayer.volumn = .5;
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: fileName];
NSError *err = nil;
self.trackPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&err];
if(err)
{
//LOG ERR
}
else
{
if ([trackPlayer prepareToPlay]) {
if (![trackPlayer play]) {
//ALog(#"Error:play sound failed.");
}
}
else
{
//ALog(#"Error:prepare play sound failed.");
};
}
This code works well under iOS4.3. I doubt if there is something special done when the iPodMusicPlayer init under iOS 6.0.1.
I need to set ipodPlayer's volumn before play local audio, how can I do this correctly?

AVAudioPlayer plays music in iPod touch 2G and 3G

I'm trying to play an audio file on iPhone and iPod touch with AVAudioPlayer. The code works fine in iPhone and iPod touch 4G, I can hear the music. But when I test it on iPod touch 2G, iPod touch 3G, I can't hear anything.
Below is the code I use to play audio file:
NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:#"TimerBell"
ofType:#"aif"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
error:nil];
[file release];
self.zeroAudioPlayer = audioPlayer;
zeroAudioPlayer.delegate = self;
[audioPlayer release];
[zeroAudioPlayer prepareToPlay];
[zeroAudioPlayer play];
I found that changing from kAudioSessionCategory_PlayAndRecord to kAudioSessionCategory_AmbientSound corrected this issue. I suspect this has something to do with a hardware difference on the iPod touch related to recording.
You should use audiosession like this before you play:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];

Resources