How perform streaming of .m4a song file with AVAudioPlayer? - ios

I'm trying to play this song from itunes store while I'm downloading it
Why the code below doesn't play?
NSLog(#"start");
NSURL *songUrl = [NSURL URLWithString:#"http://a1804.phobos.apple.com/us/r1000/064/Music/v4/9b/b3/c7/9bb3c7dc-a06f-f18c-3e41-2ce1e36f73b4/mzaf_7432104896053262141.aac.m4a"];
NSError* errorVar = nil;
AVAudioPlayer* song = [[AVAudioPlayer alloc] initWithContentsOfURL:songUrl error:&errorVar];
[song prepareToPlay];
[song play];

I think you have to use AVPlayer instead of AVAudioPlayer.
Apple documentation suggests AVAudioPlayer is used for playback of audio data from a file or memory. AVPlayer works equally well with local and remote media files.
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
[player addObserver:self forKeyPath:#"status" options:0 context:&PlayerStatusContext];
Refer Apple Docs for more info.

You can do
// soundUrl can be a remote url, don't need to be a file url
NSData *data = [NSData dataWithContentsOfURL:soundUrl];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil];

Related

How to play mp3 file from url in iOS?

I am trying to play mp3 file located somewhere on remote server.I have the url of that music fileI have tried it playing it using AVAudioPlayer or AVPlayer but i am not able to play the mp3 file.Please tell me how can i play the mp3 file.
code to play music:
NSString *aSongURL = [NSString stringWithFormat:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
// NSLog(#"Song URL : %#", aSongURL);
AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
[anAudioStreamer play];
// Access Current Time
NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);
// Access Duration
NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);
Please tell me how do i do it?
Try Below Code.
NSURL *url = [NSURL URLWithString:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:url];
[self.player play];
Try use AVAudioPlayer, see below
NSString *aSongURL = [NSString stringWithFormat:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:aSongURL] error:nil];
[audioPlayer play];
Hope it works.
Cheers.

iOS: playing sound while other apps are playing

in my app i need to play alert sounds.
if another app (like spotify) is running, when i run my code it stops the music from spotify whenever the alert sound plays.
is there anyway to avoid this?
this is my play sound code:
- (void)playSendSound
{
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"soundBit" ofType:#"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player play];
}
You are using AVAudioPlayer to play your alert sound and sure it will stop whatever music that is playing.
Modify your code to the following:
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"soundBit" ofType:#"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[player play];
This should prevent the sound that is playing to stop when you play your alert sound.
Hope this helps.

Playing itunes song previews from cocos2d app

I am trying to get itunes song previews to play in my app. I have found some other questions like this but none of the solutions have worked for me. I am already signed up with the Affiliate program that apple has. Here are some of the things I have tried:
This got me the itunes preview URL that I am trying
NSUInteger numberOfResults = 200;
NSString *searchString = #"AC/DC";
NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *finalSearchString = [NSString stringWithFormat:#"https://itunes.apple.com/search?term=%#&entity=song&limit=%u",encodedSearchString,numberOfResults];
NSURL *searchURL = [NSURL URLWithString:finalSearchString];
dispatch_queue_t iTunesQueryQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions);
dispatch_async(iTunesQueryQueue, ^{
NSError *error = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:searchURL options:NSDataReadingUncached error:&error];
if (data && !error) {
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *array = [JSON objectForKey:#"results"];
NSLog(#"Array is:%#",array);
}
});
This plays nothing
NSURL *url = [NSURL URLWithString:[_backgroundPreviewList objectAtIndex:randomPreview]];
NSData *_objectData = [NSData dataWithContentsOfURL:url];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
{
NSLog(#"%#", [error description]);
}
else
{
[audioPlayer play];
}
This also played nothing
AVPlayer* aPlayer = [AVPlayer playerWithURL:url];
[aPlayer play];
I even tried this that didn't work
MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
If anyone has done this can they let me know how to play the songs.
This is released and working using MPMoviePlayerController, which I believe is the recommended approach for the streaming previews. Depending on what you are trying to do, you might have to work through some steps to enable play in the background. Good luck. p.s. if you get the AVPlayer working there are more docs available with solutions for background play see Technical QA doc 1668 from Apple
{
self.songPreview = [[MPMoviePlayerController alloc] init];
self.songPreview.movieSourceType = MPMovieSourceTypeStreaming;
/// set your URL from wherever you are getting it
[self.songPreview setContentURL:self.previewArtWorkButton.currentPreviewURL];
[self.view addSubview:self.songPreview.view];
/// i have set this next property to hidden because i am controlling volume and play programmatically
[self.songPreview.view setHidden:YES];
/// i have this next property set to No because I am using another trigger to play the /// song and not auto play after the buffering has completed
[self.songPreview setShouldAutoplay:NO];
[self.songPreview prepareToPlay];
}
/// later i call the play method
[self.songPreview play];
I have got it working. One of the things I changed is I took that specific class out of ARC to make sure things were staying around. Here is the code I am using now
NSURL *url = [NSURL URLWithString:#"URL"];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
NSLog(#"The url is: %#", url);
if(_player)
{
[_player release];
}
_player = [[AVPlayer playerWithPlayerItem:playerItem] retain];
[_player play];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
Where _player is an AVPlayer object

how to stream mp3 file in iphone?

I'm streaming my mp3 file but it is starting very very late
after launch the application i have to wait for 1 or 2 mints then it plays the music
why it is so ?
can anybody have an idea ?
Please Do Tell me how can i play direct track without waiting too long
here what i m doing with my code in viewDidLoad method
-(void)viewDidLoad
{
NSString *urlstr=#"http://media.hyderabadplus.com/naats/Aamir Liaquat Hussain - Panjtan Ko Salaam/Ramzan Assalam.MP3";
NSURL *url = [NSURL URLWithString:[urlstr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSData *songData=[NSData dataWithContentsOfURL:url];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:songData error:nil];
self.audioPlayer.numberOfLoops=0;
self.audioPlayer.delegate=self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}

iOS stream audio with no extension

I am looking to play a live audio stream within my app using AVAudioPlayer or AVPlayer, however the link has no extension and so whenever I try to set it up, it fails. Does anyone have any suggestions?
Code:
NSString *path = #"http://audio7.radioreference.com/24705802";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:&error];
player.volume = 1.0f;
player.numberOfLoops = -1.0f;
[player prepareToPlay];
if (player == nil) {
NSLog(#"Error: %# Description: %#", error.localizedDescription, error.description);
}
else {
[player play];
}
NSData does not constitute a stream, but static data (eg sound data that you created or data loaded from a file). You want to create an AVPlayer object with an NSURL pointing to the stream. For example:
AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:path]];
[player play];

Resources