I am Playing Audio from URL given below
"http://sumeetmusiclocal.wsisites.net/Songs/Kashyala Lavato (Lavani).mp3"
By Using Code given below
NSString *urlString = #"http://sumeetmusiclocal.wsisites.net/Songs/Kashyala Lavato (Lavani).mp3";
NSURL *audioFileLocationURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
[audioPlayer setNumberOfLoops:-1];
if (error) {
NSLog(#"%#", [error localizedDescription]);
[[self volumeControl] setEnabled:NO];
[[self playPauseButton] setEnabled:NO];
[[self alertLabel] setText:#"Unable to load file"];
[[self alertLabel] setHidden:NO];
} else {
[[self alertLabel] setText:[NSString stringWithFormat:#"%# has loaded", #"HeadspinLong.caf"]];
[[self alertLabel] setHidden:NO];
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
//Load the audio into memory
[audioPlayer prepareToPlay];
}
But I getting Error saying
Printing description of error:
Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"
2016-03-08 12:43:45.835 SumeetApp[1839:78581] The operation couldn’t be completed. (OSStatus error 2003334207.)
Can anyone please help me to solve this error or suggest me some other way for playing audio from url.
Thank you
Try This
AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:#"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4"]];
AVPlayerItem *playerItem1 = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *player1 = [AVPlayer playerWithPlayerItem:playerItem1];
AVPlayerLayer *playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:player1];
playerLayer1.videoGravity = AVLayerVideoGravityResizeAspectFill;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
playerLayer1.frame = self.view.frame;
});
[self.view.layer insertSublayer:playerLayer1 atIndex:1];
[player1 play];
Hope this work for you.
AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:#"http://sumeetmusiclocal.wsisites.net/Songs/Kashyala Lavato (Lavani).mp3"]];
[player play];
Try this code this is working for me.
Try This
NSURL *url = [NSURL URLWithString:#"YOUR_MP3URL"];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:#"YOUR_MP3URL"];
for play
[self.player play];
Hope this helps.
For Error:
you trying to access a file that didn't exist.
so, initWithContentsOfURL:audioFileLocationURL will return nil.
To play Audio:
NSURL *url = [NSURL URLWithString:#"<URL>];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<URL>];
[player play];
Related
I've created a custom camera similar to snapchat with AVFoundation. The picture aspect works: take a picture and I display the picture. Now I'm trying to take a video and display the video.
I have captured a successful video file, tested and works with MPMoviePlayer, but am unable to play it back on AVPlayer.
- (void)takeVideoAction:(id)sender {
NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"output.mp4"];
NSFileManager *manager = [[NSFileManager alloc] init];
if ([manager fileExistsAtPath:outputPath]){
[manager removeItemAtPath:outputPath error:nil];
}
[movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath] recordingDelegate:self];
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:outputFileURL options:nil];
[asset loadValuesAsynchronouslyForKeys:#[#"tracks"] completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
if([asset statusOfValueForKey:#"tracks" error:&error] == AVKeyValueStatusFailed){
NSLog(#"error: %#", [error description]);
}
else{
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.playerLayer];
[self.player play];
}
});
}];
}
This is my first time using AVFoundation & AVPlayer. Is it that I can not [self.player play] after the asset loads right? I need to wait for item status to be AVPlayerItemStatusReadyToPlay?
ERROR: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server."
I am using AVPlayerLayer for playing video. I used the below code and it worked fine.
AVPlayer *avPlayerq = [AVPlayer playerWithURL:NSURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
Hope this may help you.
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
I'm trying to play remote .mp3 file
but its giving the following error.
The operation couldn’t be completed. (OSStatus error -43.)
here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
isPlaying = NO;
/*
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"Dar-e-Nabi-Per" ofType:#"mp3"]];
*/
NSURL *url = [NSURL
URLWithString:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
}
else
{
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
}
}
Can anyone please guide me where i'm doing mistake
For steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer.
AVPlayer Documentation
Sample Code:
- (void)playSampleSong:(NSString *)iSongName {
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);
}
Use this to play song from URL
NSData *songData=[NSData dataWithContentsOfURL:url];
self.player = [[AVAudioPlayer alloc] initWithData:songData error:nil];
self.player.numberOfLoops=0;
_player.delegate=self;
[_player prepareToPlay];
[_player play]
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];
sorry for my question but i´ve implemented a intro- video and despite of the hardware silent-switch of my iPad, the audio is playing.
I´m also using the AVAudioplayer within my app just for playing short sound samples. Within this class, its the only region where i´ve set up the "AVAudioSessionCategory".
But for all audio playback only, there´s nothing hearable. Its just for my intro-video.
Any help how to fix that "audio-bug" so the movie player is silent?
Thanks you
Here´s my Audio-class:
- (id)initWithSoundfileName:(NSString*) file
{
if ((self = [super init]))
{
NSString* filename = [file stringByDeletingPathExtension];
NSString* fileextension = [file pathExtension];
// get file path from bundle
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: filename ofType: fileextension];
NSLog(#"AudioPlayer init: %#", soundFilePath);
NSURL* fileurl = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSError* error = nil;
AVAudioPlayer* audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileurl error:&error ];
if (error) { NSLog(#"Error creating AVAudioPlayer %#", [error description]);}
// set audio policy
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];
self.player = audioplayer;
[self.player prepareToPlay];
[self.player setDelegate:self];
}
return self;
}
-(void) play{
[self.player play];
}
And here´s my video-playback method:
- (void)playIntroVideo
{
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"mymovie" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:movpath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayerController.useApplicationAudioSession = NO;
[self.moviePlayerController.view setFrame: self.view.bounds];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayerController];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController play];
}
Like #Till mentioned:
When I change the MoviePlayerController property to useApplicationAudioSession=TRUE, it fixes my problem. Audio Playback is silent.