how to play mp3 in asset url using AVPlayer - ios7

i have url in ipod-library://item/item.mp3?id=8788117795090152241
how to play mp3 in this url
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url_selectedSong];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
but not play song

Use this:
NSError * error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if(player) {
[player play];
}

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 Play/Pause/Stop local audio file on three different buttons

I want to play/pause/stop local audio file. Buttons for all functionalities are different. Check attached screenshot for reference.
Below is my click events,
-(IBAction)click_Play:(id)sender {
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Here, I am facing issue in pause functionality. After pausing the audio when I click on play button again. It start playing from starting and not at the point where I pause it. Any solution?
Thanks in advance!
Change your code to this:
-(IBAction)click_Play:(id)sender {
if (!player)
{
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
else {
[player play];
}
}
Don't create instance of AVAudioSession and AVAudioPlayer again and again in play button action. So, keep it simple, create instance of AVAudioSession and AVAudioPlayer in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
}
-(IBAction)click_Play:(id)sender {
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Edit:
Call this method when you what to change track. I'll never recommend to create instance of object again and again
-(void)changeTrackWithPath:(NSString *)path{
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
[player replaceCurrentItemWithPlayerItem:playerItem];
[player play];
}

The app crashes when audio is playing and the device receives a phone call

The app crashes when audio is playing and the device receives a phone call. When I hang up the call and return to the application, it is blocked.
Code:
NSURL *audioFileLocationURL;
NSString *MyAudioString;
MyAudioString = #"my-audio";
audioFileLocationURL = [[NSBundle mainBundle] URLForResource:MyAudioString withExtension:#"mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
[audioPlayer setNumberOfLoops:1];
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(updatemyProgress) userInfo:nil repeats:YES];
if (error) {
NSLog(#"%#", [error localizedDescription]);
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[audioPlayer prepareToPlay];
[audioPlayer setDelegate:self];
[audioPlayer play];
}
Done:
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player

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.

AVAudioPlayer play audio properly in Ipod Touch 4g but not in Ipad, Ipad2, why?

Here is my code:
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:[settings alarmAudioName] ofType:#"mp3"];
NSLog(#"%#",path);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
NSLog(#"%#",[NSURL fileURLWithPath:path]);
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
{
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(#"Should have been played");
}
NSLog:
/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA Development.app/Song 1.mp3
file://localhost/var/mobile/Applications/9B5B4E10-A936-4D90-8CBE-17854A0B22F7/SSCA%20Development.app/Song%201.mp3
Its works correct in Ipod Touch 4g, but not working in Ipad, Ipad2, why?
Thanks the answers,