I'm trying to play a streaming video with a AVPlayer without any network delay. Fortunately, our app has a progress screen before the screen that plays the video. I'm hoping to use some of the time on this progress screen to pre-load the video so it plays without delay on the next screen.
The most promising approach that I've come up with is to use an AVQueuePlayer to play 2 videos, the first would be a video that I play off screen (so you don't see it) and is silent, so you don't hear it. From what I've read on SO AVQueuePlayer buffers the nth+1 video when the nth video is near completion.
Here's my code to do this:
NSString *blackVideoPath = [[NSBundle mainBundle] pathForResource:#"black10-counting" ofType:#"MOV"];
AVPlayerItem *blackVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:blackVideoPath]];
AVPlayerItem *realVideoItem = [AVPlayerItem playerItemWithURL:_videoWithEffectsURL];
NSArray *theItems = [NSArray arrayWithObjects:blackVideoItem, realVideoItem, nil];
AVQueuePlayer *theQueuePlayer = [AVQueuePlayer queuePlayerWithItems:theItems];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[theItems firstObject]];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:theQueuePlayer];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
[theQueuePlayer play];
This does play my first video (for debugging I have a video with a counting soundtrack) but when reach the end and advance to the next screen (in playerItemDidReachEnd) my real video doesn't play immediately.
Just to make sure my URLs were correct I reversed blackVideoItem and realVideoItem in the list and I do hear the soundtrack to my "real" video.
I've searched SO a lot and it doesn't seem like there is a way to play a streaming video without a delay. I'd love to be wrong.
You may be able to do this without AVQueuePlayer.
As early as possible, create the AVPlayer with the remote asset.
Observe the AVPlayerItem (playback buffers, etc.) to make sure it has buffered enough data to play smoothly at the beginning. (See: Preloading video to play without delay)
Show your progress screen.
When the player item is ready, remove the progress screen
Depending on the network connection, you may not experience smooth playback the whole way through, but monitoring the player item should let you show some UI if your player is buffering.
Related
I have a relatively simple setup involving 1 AVPlayer looping some ambient audio in the background and a second player playing a shorter sound at certain points.
What I've observed is that when the short sound is played, I hear the ambient clip cut out for about a second while there is a staticy pop. It then proceeds to continue playing while the short sound is played at the same time. This only happens on device - it's not noticeable on the sim, which seems to point to a potential performance issue.
I can't quite figure out why the first AVPlayer has this blip. Here is the code for the ambient player:
NSString *path = [[NSBundle mainBundle] pathForResource:kAmbientTrack ofType:#"mp3"];
_ambientPlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
[self.ambientPlayer play];
It's slightly more complex, as I also listen for a notification when it ends and the restart it, but this issue happens even during the initial play before any looping occurs.
So while that is going in the background, I play another clip like so:
self.announcementPlayer = [[AVPlayer alloc] initWithURL:[myObject audioPathUrl]];
[self.announcementPlayer play];
So nothing too unique there - just two AVPlayers playing.
The only other piece of interest is how I set up the audio session when the app launches.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
I'm doing this to allow the user to play music from other apps in the background.
I think my code is pretty straight forward, and really have no idea why I would be getting this blip.
I fixed this somewhat by changing the audio players to be AVAudioPlayers. That resolved the popping in most cases. However, I still get the ducking. Specifically, if I play an AVAudioPlayer and, while it's still playing, I start to load an AVPlayer, it cuts off the AVAudioPlayer for about a second. Here's what I'm talking about:
[myAVAudioPlayer play];
AVPlayer *player = [AVPlayer playerWithURL:[self.currentExercise videoPathUrl]];
The addition of that second line causes the first second or so of myAVAudioPlayer to be silent.
I've created an AVPlayer and set the forwardPlaybackEndTime to make a local video stop at a given timestamp. Sure enough, the video stops at the time I've requested. All good.
Now I want the video to continue when triggered by a user action (touching a button, for example). Unfortunately, I can't seem to make that happen without the video restarting from the beginning.
I'll spare you all of the AVPlayer setup code (which is mostly taken from the AV Foundation Programming Guide), but given these variables:
AVPlayer *avPlayer;
AVPlayerItem *playerItem;
I can set the end time like so:
[playerItem setForwardPlaybackEndTime: CMTimeMake(30, 30)];
To attempt the resume, I've tried this:
[playerItem setForwardPlaybackEndTime: CMTimeMake(30, 30)];
[avPlayer setRate: 1.0];
No dice. I've also tried setting the end time and calling play. No luck. I've tried seekToTime to put the playhead at the place where the video stopped in case that would help. It doesn't.
Can someone please explain how to make this work? Thanks!
Try setting the forwardPlaybackEndTime back to the default value, kCMTimeInvalid then continue to play the video.
[playerItem setForwardPlaybackEndTime: kCMTimeInvalid];
[playerItem seekToTime: CMTimeMake(30, 30) toleranceBefore: kCMTimeZero toleranceAfter: kCMTimeZero];
[avPlayer play];
I have an app that I want to make which requires streaming audio files from web server. I use AVPlayer as the player. The problem is, some responses that I am receiving from the server has two audio files on it. And this makes the streaming hard. My audio player UI by the way is like this:
I have a slider for the streamed time ranges (the black one) and another slider for the AVPlayer.currentTime. I have two audio music streamed and their music durations are added together which is now 8:46. My first music has 6 minutes duration and my second music has 1:46. As you can see in the above photo, my streamed time ranges slider indicates that AVAsset has completely streamed the first music. My problem is, I can't continue streaming and playing the next music when the first one has reached it's end. It just stop and the slider value gets back to 0.
What I want to accomplish is that when the first item has reached its end, AVPlayer would load another player item and that would be the second music. Will continue to play and slider will continue to move.
Is this possible? What are your suggestions? Thanks experts.
To load audio files one after the other, you can use AVQueuePlayer.
NSURL *song1 = [NSURL URLWithString:#"audio url1"];
NSURL *song2 = [NSURL URLWithString:#"audio url2"];
AVPlayerItem *playerItem1 = [[AVPlayerItem alloc] initWithURL:song1];
AVPlayerItem *playerItem2 = [[AVPlayerItem alloc] initWithURL:song2];
NSArray *songs = #[playerItem1, playerItem2];
self.queuePlayer = [[AVQueuePlayer alloc] initWithItems:songs];
[self.queuePlayer play];
Hope this might help you.
I'm creating an app where the user is supposed to video record himself while another video is playing on the screen next to the camera view. The result will be two video files, one source, and one recording. Using AVCaptureSession etc., I have successfully managed to record a video at the same time as another video is playing on the screen. The problem is that It's not completely in sync.
This is how I have set it up right now:
-(void)playAndRecordInSync //Session is already set etc.
{
player = [AVPlayer playerWithURL:url];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[playerLayer setFrame:leftCameraView.bounds];
[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[leftCameraView.layer addSublayer:playerLayer];
//Will stop recording camera when source video reaches end(notification):
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
//Start playback:
[player play];
//Start recording:
[movieOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[movieOutput stopRecording];
}
The result of this, is two videos, but the second (recorded) video is slightly longer than the source/original video. When playing them on top of each other (at the same time), then video nr. 2 has a delay of almost half a second. It feels like video2 has started recording way too soon, because of the "extra" time. It could probably also be that the playback started too late. Either way, I don't know how to make any consistency here based on this. I could probably play around a lot with sleep etc, but that would only work for my phone, as this has something to do with how fast the phone is.
Even if there is a delegate method like -(void)willStartPlaying for the AVPlayer, which I don't think there is, there would still be a problem with sync, as there's a minor wait till the return. Is there a way to fire two commands at once? Or is there another solution for this?
EDIT
I've been reading this (AVSynchronizedLayer), but I'm not sure if this is applicable to my situation, and I don't understand how to proceed.
I found out the delay was always caused by the playback starting too late, but both the recording and the playback were stopped at the same time, so I resolved this by finding v2.duration - v1.duration, and subtract the result from the beginning of v2, using AVAssetExportSession. When playing back both videos at once now, in different layers, there's still some delay from starting both, but by exporting the videos, I've confirmed that they're the same length and in sync now.
In my app I have 52 mp3 and when I load my view controller I alloc all 52 mp3 in this way:
NSString *pathFrase1 = [NSString stringWithFormat:#"%#/%#",[[NSBundle mainBundle] resourcePath],[NSString stringWithFormat:#"a%d_1a.mp3",set]];
NSURL *filePath1 = [NSURL fileURLWithPath:pathFrase1 isDirectory:NO];
f1 = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath1 error:nil];
[f1 prepareToPlay];
but it's very slow when I open viewcontroller, then is there a way to alloc mp3 when I use it? and release its AVAudioPlayer??
It's a little more complicated to handle, but instead of using AVAudioPlayer, use AVPlayer. AVPlayer is designed to play AVAssets, which can be preloaded files. Specifically, you'll want to use a subclass of AVAsset called AVURLAssets, which can load up your URL: Loading AVAsset. You can then use AVAsset to create an AVPlayerItem. An AVPlayerItem is a lightweight wrapper that AVPlayer uses to keep track of play state for an AVAsset. The nice thing about using AVPlayer is that it can play an AVMutableComposition, which itself can contain multiple AVAssets. AVPlayer can also play a queue of AVAssets and provide you with information on when it is beginning to play a new AVAsset, and which one. If you load your MP3's into a bunch of AVURlAssets you can load them and keep them around, creating AVPlayerItem & AVPlayer only when you want to play one (or more) of the MP3's.
AVAudioPlayer is designed to play single files, but it uses AVAssets (and probably AVPlayer) behind the scenes. It's nice for simple situations, but anything more complex and you really want to use AVPlayer.
I should also point out that AVPlayerItem & AVPlayer are light weight objects. They don't take long at all to instantiate. It's loading the AVAsset that takes all the time. So you can feel free to create and destroy AVPlayerItem & AVPlayer objects as you need.
Finally, AVAsset and AVPlayer sometimes rely on blocks for notifications. So, for example, you may need to use c-blocks when loading up AVURLAsset to get notification on when an Asset if fully loaded. Just be aware that those blocks aren't called on the main thread. So if you try to update any UI elements or do any animations from that block it won't work right. You need to dispatch another block on to the main thread to do this, for example: dispatch_async(dispatch_get_main_queue(),^{....update UI element code....});. For more information about about dispatching blocks see Apple's Concurrency Programming Guide and Block Programming Guide.
Sure, but there will be a delay as the AVAudioPlayer is allocated and prepared for playing. Can you predict what will play and when? If so, maybe you can load a couple of seconds before you need a particular mp3 to play.
An alternative, which may not work depending on your timing requirements, is the following:
- (void)prepareAudioPlayer {
static int index = -1;
index = index + 1;
if (index < [[self FileNames] count]) {
NSError *err = nil;
NSString *audioFilePath = #""; // Figure out how to get each file name here
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
if (err) {
NSLog(#"Audio Player Error: %#", [err description]);
}
[player prepareToPlay];
// Add this player to the "AudioPlayers" array
[[self AudioPlayers] addObject:player];
// Recurse until all players are loaded
[self prepareAudioPlayer];
}
}
This solution requires properties of FileNames and AudioPlayers.
Once this is set up, you could do something like the following (probably in viewDidLoad):
// Make the file name array
[self setFileNames:[NSMutableArray array]];
// Initiate the audio player loading
[self performSelectorInBackground:#selector(prepareAudioPlayer) withObject:nil];
Later, when you need to play a particular file, you can find the index of the file name in the FileNames array and the call play on the AVAudioPlayer for that index in the AudioPlayers array.
This seems like maybe not the best way to do things, but it might work if you require it this way.
Here is the using method , If the sound is playing, current Time is the offset of the current playback position, measured in seconds from the start of the sound. If the sound is not playing, current Time is the offset of where playing starts upon calling the play method, measured in seconds from the start of the sound.
By setting this property you can seek to a specific point in a sound file or implement audio fast-forward and rewind functions.
The value of this property increases monotonically while an audio player is playing or paused.
If more than one audio player is connected to the audio output device, device time continues incrementing as long as at least one of the players is playing or paused.
If the audio output device has no connected audio players that are either playing or paused, device time reverts to 0.
Use this property to indicate “now” when calling the play AtTime: instance method. By configuring multiple audio players to play at a specified offset from deviceCurrent Time, you can perform precise synchronization—as described in the discussion for that method.To learn more visit..enter link description here