I have AVPlayer Questions.
1.How to control the volume of it?
2.How to know if the AVPlayer is reloading music because bad connection, do i have some inidication of it?
AVPlayer uses the system volume, so if you need to provide controls for this you can use MPVolumeView which gives you the slider for volume control.
For audio fading, you can use an AVAudioMix. Here's some code:
//given an AVAsset called asset...
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
id audioMix = [[AVAudioMix alloc] init];
id volumeMixInput = [[AVMutableAudioMixInputParameters alloc] init];
//fade volume from muted to full over a period of 3 seconds
[volumeMixInput setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:
CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(3, 1))];
[volumeMixnput setTrackID:[[asset tracks:objectAtIndex:0] trackID]];
[audioMix setInputParameters:[NSArray arrayWithObject:volumeMixInput]];
[playerItem setAudioMix:audioMix];
You can also abruptly set the volume for a mix at a given time with:
[volumeMixInput setVolume:.5 atTime:CMTimeMakeWithSeconds(15, 1)];
Hope this helps. This API is definitely not obvious. I'd highly recommend watching the WWDC 10 video entitled Discovering AV Foundation. It's excellent.
Related
I'm switching from MPMoviePlayerController to AVPlayer as I need finer grained control over video swapping. The .mov file I was playing with MPMoviePlayerController played fine, but after switching to AVPlayer I hear the audio from the video, but the video just shows the view background that I added the AVPlayerLayer to. Here's how I'm initializing the AVPlayer.
self.player = [[AVPlayer alloc] initWithURL:video];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.playerContainer.bounds;
[self.playerContainer.layer addSublayer:playerLayer];
Then later I just issue a.
[self.player play];
When the video plays I hear the audio, but see no video. I also tried setting the zPosition to no luck.
playerLayer.zPosition = 1;
Found out it was a result of using AutoLayout. In the viewDidLoad the self.playerContainer.bounds is a CGRectZero.
I had to assign the playerLayer frame in the viewDidAppear to match the playerContainer.
Since we use AVPlayerLayer (a subclass of CALayer), we need to set the frame
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.avPlayerLayer.frame = self.movieContainerView.bounds;
}
Make sure you the statement that plays the video:
[self.player play]
is invoked from main dispatch queue like this (Swift 3):
DispatchQueue.main.async {
self.player.play()
}
I have tried AVPlayer for playing online http streamed music file, it works fine.
What is the way to get the current playing audio track name,artist etc?
Is there any way to adjust the volume of AVPlayer while playing music (using UISlider)?
You can adjust the volume by:
player.volume = slider.value;
MPMediaItem *currentItem = self.musicPlayer.nowPlayingItem;
//Display the artist, album, and song name for the now-playing media item.
//These are all UILabels.
self.songLabel.text = [currentItem valueForProperty:MPMediaItemPropertyTitle];
self.artistLabel.text = [currentItem valueForProperty:MPMediaItemPropertyArtist];
self.albumLabel.text = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];
I am trying to use UIVideoEditorController to edit my video, but it seems to lose my video resolution. My original video was 720 x 1280, but after using the UIVideoEditorController, the quality becomes 360 x 640.
I tried to set the videoQuality to be UIImagePickerControllerQualityTypeHigh or even UIImagePickerControllerQualityTypeIFrame1280x720, but that doesn't help.
I am working on the iPad and here are my code:
self.editorController = [[[UIVideoEditorController alloc] init] autorelease];
self.editorController.videoPath = self.tempVideoPath;
self.editorController.delegate = self;
self.editorController.videoQuality = UIImagePickerControllerQualityTypeHigh;
CKLog(#"%d", self.editorController.videoQuality);
self.popOverController = [[[UIPopoverController alloc] initWithContentViewController:self.editorController] autorelease];
self.popOverController.delegate = self;
self.popOverController.popoverContentSize = CGSizeMake(700, 700);
[self.popOverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.videoView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
According to the UIVideoEditorController documentation:
A UIVideoEditorController object, or video editor, manages the
system-supplied user interface for trimming video frames from the
start and end of a previously recorded movie as well as reencoding to
lower quality...
The UIVideoEditorController class has a property called videoQuality. It allows us to choose the video quality when saving the trimmed video. (The default value is UIImagePickerControllerQualityTypeLow)
UIImagePickerControllerQualityTypeHigh uses the highest-quality video recording supported for the active camera on the device. It looks much better, but still seems to lose a little of the original resolution.
I have a video, and I want to display the video at a specific time time and stop it at a specific time. I am using MPMoviePlayerController.
You should take a look at the MPMoviePlayerController Class Reference, and the initialPlaybackTime and endPlaybackTime properties.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
player.initialPlaybackTime = 5; // beginning time in seconds
player.endPlaybackTime = 15; // end time for playback in seconds
I want to make an app where users can create funny stick figure animations.
It would be cool if it is possible to export them as video. Can I "draw" video frames frame by frame and render them into a H.264 or other video format?
The length will be between 2 seconds and 5 minutes. I heared a while back that there is a framework to edit video but in my case I really need to create a video from scratch. What are my options?
You might need to use a multimedia framework which provides more lower level control, like gstreamer or ffmeg.
Alternately, you can create an MJPEG and figure out a way to transcode it.
Yes, you can examine :
CEMovieMaker
Usage:
UIImage *frameImg = <Some Image>;
NSDictionary *settings = [CEMovieMaker videoSettingsWithCodec:AVVideoCodecTypeH264
withWidth:source.size.width
andHeight:source.size.height
];
///
CEMovieMaker * movieMaker = [[CEMovieMaker alloc] initWithSettings:settings];
/// Complete video
[movieMaker createMovieFromImages:[self.movieImages copy] withCompletion:^(NSURL *fileURL){
//AVPlayerViewController or
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[playerController.view setFrame:self.view.bounds];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer play];
[self.view addSubview:playerController.view];
}];