How to play mp3 file from url in iOS? - 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.

Related

Playing a video from session.outputURL with MPMoviePlayerController

I'm trying to play a captured video that recorded with AVFoundation and saved in the device's library.
I have the url of the video:
NSURL *movieURL = session.outputURL;
output:
file:///private/var/mobile/Containers/Data/Application/F255AC7A-2E0C-40B2-A195-52C03ED5B299/tmp/video.mp4
Now I want to play this video, and I think that the best player for this is the MPMoviePlayerController player (let me know if I'm wrong).
This is my code: but for some reason its not working:
NSURL *movieURL = session.outputURL;
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[player prepareToPlay];
[player.view setFrame: CGRectMake(0, 0, 200, 200)];
[self.view addSubview: player.view];
[player play];
Use below
player = [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"video" ofType:#"mp4"]]];
It will work then

AVAudioPlayer is not restarting the bg music?

I'm on latest Xcode version. I'm using following code to restart my background music. But these codes are not restarting the background music.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"game" ofType:#"wav"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player stop];
player.currentTime = 0;
[player play];
To use AVAudioPlayer, you need a strong reference to it. Try to set the player as a property of your controller, then use [self.player play]
Declare AVAudioPlayer into .h file.

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.

How to play a sound when my app launches

How would I play a sound when my iOS app launches; should I use an AVPlayer to play an mp3 file?
In didFinishLaunchingWithOptions: use below code to play audio
NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];

How perform streaming of .m4a song file with AVAudioPlayer?

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];

Resources