AVPlayer error during mp3 playback - ios

I would like to know why my AVPlayer crashes when I want it to play "some" songs.
Song #1
Song #2
That is the way I play these songs:
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:#"http://www.iwheelbuy.com/Player/Test/1.mp3"]];
AVPlayer *player = [[AVPlayer alloc] init];
[player replaceCurrentItemWithPlayerItem:item];
[player setRate:1.0f];
I have some KVO observers, one of them observes AVPlayer status and when I try to play one of these songs it gives me an error:
Domain=AVFoundationErrorDomain Code=-11800
UserInfo=0x1cdc3be0 {NSLocalizedDescription=..., NSUnderlyingError=0x1cdc14c0
"The operation couldn’t be completed. (OSStatus error -303.)"
Is there a way to play these songs? Any help is appreciated.
EDIT:
I have succeeded to make these songs play well
__block AVPlayer *currentPlayerBlock = _currentPlayer;
__block NSURL *urlBlock = url;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlBlock options:nil];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
dispatch_sync(dispatch_get_main_queue(), ^{
[currentPlayerBlock replaceCurrentItemWithPlayerItem:item];
[currentPlayerBlock setRate:1.0f];
});
});

__block AVPlayer *currentPlayerBlock = _currentPlayer;
__block NSURL *urlBlock = url;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlBlock options:nil];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
dispatch_sync(dispatch_get_main_queue(), ^{
[currentPlayerBlock replaceCurrentItemWithPlayerItem:item];
[currentPlayerBlock setRate:1.0f];
});
});
This way it works

Related

Play Audio from URL using AVAudioPlayer

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

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.

AVQueuePlayer gapless playback

I am trying to dynamically add .aac chunks (usually 10 seconds) during the playback to AVQueuePlayer to mimic the AVPlayer hls playback.
However it plays songs one after another as I wished, there is about 200ms to 400 ms gap between songs.
following question seems to have the answer it to this question but it doesn't work for me.
AVQueuePlayer playback without gap and freeze
I have a singleton player and a singleton avplayeritem to keep track of the last item.
then with following code I can successfully play the songs with gap.
NSURL *url = [NSURL URLWithString:[urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
//NSData *urlData = [NSData dataWithContentsOfURL:url];
GlobalPlayer *singleton=[GlobalPlayer sharedManager];
//AVPlayerItem *item=[AVPlayerItem playerItemWithURL:url];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *keys = [NSArray arrayWithObject:#"playable"];
if (singleton.lastItem==nil) {
//initilize player
//NSArray *playerArray=[[NSArray alloc] initWithObjects:item, nil];
//singleton.globPlayer=[[AVQueuePlayer alloc] initWithItems:playerArray];
//[singleton.globPlayer play];
singleton.globPlayer=[[AVQueuePlayer alloc] init];
singleton.lastItem=[AVPlayerItem alloc];
__block AVPlayerItem *playerItem;
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
{
dispatch_async(dispatch_get_main_queue(), ^
{
playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
[singleton.globPlayer insertItem:playerItem afterItem:nil];
singleton.lastItem=playerItem;
[singleton.globPlayer play];
});
}];
}
else
{
//[singleton.globPlayer insertItem:item afterItem:singleton.lastItem];
__block AVPlayerItem *playerItem;
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
{
dispatch_async(dispatch_get_main_queue(), ^
{
playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
[singleton.globPlayer insertItem:playerItem afterItem:singleton.lastItem];
singleton.lastItem=playerItem;
});
}];
}
Do I have to decode the chunks then feed the PCM to AVQueuePlayer? If that is the only solution how do I feed NSData that holds pcm to the AVPlayerItem ?

AVAssetResourceLoaderDelegate setup issue

I have a weird issue when trying to setup AVAssetResourceLoaderDelegate. When using this code it works most of the time on a device but it doesn't work on the simulator at all:
NSURL *url = [NSURL URLWithString:#"xxx://name"];
AVURLAsset *asset = [AVURLAsset assetWithURL:url];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[asset.resourceLoader setDelegate:self queue:self.queue];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[avPlayer play];
The issue is that the delegate method is never getting called:
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
However, this code works perfectly fine:
NSURL *url = [NSURL URLWithString:#"xxx://name"];
AVURLAsset *asset = [AVURLAsset assetWithURL:url];
[asset.resourceLoader setDelegate:self queue:self.queue];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[avPlayer play];
The only difference is that we set the delegate first and then we create AVPlayerItem with the asset. In the first case, the asset is not being copied when creating AVPlayerItem and if we check playerItem.asset.resourceLoader.delegate the delegate is set correctly but it's never getting called.
I would appreciate any help with it.
Thanks,
Michal

Playing video from within app?

I am trying to play a local video from my app using AVPlayer. I have looked everywhere but can't find any useful tutorials/demos/documentation. Here is what I am trying, but it is not playing. Any idea why? The URL is valid because I was using the same one to play a video using MPMoviePlayer successfully.
Video *currentVideo = [videoArray objectAtIndex:0];
NSString *filepath = currentVideo.videoURL;
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
self.player = [[AVPlayer alloc] initWithPlayerItem: item];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];
[self.player play];
I believe the problem is that you're assuming that after executing this line AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL]; the asset is ready to use. This is not the case as noted in the AV Foundation Programming Guide:
You create an asset from a URL using AVURLAsset. Creating the asset, however,
does not necessarily mean that it’s ready for use. To be used, an asset must
have loaded its tracks.
After creating the asset try loading it's tracks:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = #"tracks";
[asset loadValuesAsynchronouslyForKeys:#[tracksKey] completionHandler:
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
// At this point you know the asset is ready
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
...
}
}];
Please refer to this link to see the complete example.
Hope this helps!

Resources