Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

How To Mute Video's Audio - Solved!

Wow...AVFoundation is awesome.

In this code snippet I create a player view that responds to double taps, resizing the video. I also mute the video's audio if the flag is true. This is if folks wish to watch the video with their own audio from the iPod app.

Now, if I could only use airplay to transmit my app's screen to Apple TV....:cool:

James A. Brannan
James A. Brannan's Site on all Things Programming, iPhone,
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface PlayerView : UIView { }
@property (nonatomic, retain) AVPlayer *player;
-(void) changeLayerSizing;
@end
#import "PlayerView.h"
#import "UIDeviceAdditions.h"
@implementation PlayerView
+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer*)player {
    return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
   [(AVPlayerLayer *)[self layer] setPlayer:player];
   ((AVPlayerLayer *)[self layer]).videoGravity = AVLayerVideoGravityResize;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [[event allTouches] anyObject];
	if ([touch tapCount] > 1) {
	  [self changeLayerSizing];
	}
}
-(void) changeLayerSizing
{
	if(	((AVPlayerLayer *)[self layer]).videoGravity ==  
              AVLayerVideoGravityResize)
	{
		((AVPlayerLayer *)[self layer]).videoGravity = 
              AVLayerVideoGravityResizeAspect;
	}
	else if(((AVPlayerLayer *)[self layer]).videoGravity == 
              AVLayerVideoGravityResizeAspect)
	{
		((AVPlayerLayer *)[self layer]).videoGravity =
              AVLayerVideoGravityResizeAspectFill;
	}
	else
	{
		((AVPlayerLayer *)[self layer]).videoGravity = 
              AVLayerVideoGravityResize;
	}
}

- (void) dealloc {
	DLog(@"PlayerView is deallocating");
	[[UIDevice currentDevice] showAvailableMemory];
	[super dealloc];
}
@end
- (BOOL) playMovie: (Video*) theVideo showControls: (BOOL) showControls
{
	BOOL success = YES;
	[[UIDevice currentDevice] showAvailableMemory];
	@try {
		showTheControls = showControls;
		AVURLAsset * sourceAsset = [[AVURLAsset alloc] 
                    initWithURL:theVideo.pathURL options:nil];
		NSString * tracksKey = @"tracks";
		[sourceAsset loadValuesAsynchronouslyForKeys:[NSArray 
                    arrayWithObject:tracksKey] completionHandler:
		 ^{
			 AVPlayerItem * tempItem = [[AVPlayerItem alloc] 
                    initWithAsset:sourceAsset];
			 [sourceAsset release];
			 self.myPlayerItem = tempItem;
			 [tempItem release];
			 [self.myPlayerItem addObserver:self forKeyPath:@"status"
					options:0 context:&ItemStatusContext];
			 [self.myPlayerItem addObserver:self forKeyPath:@"tracks"
					options:0 context:&TracksStatusContext];
			
			 if(self.myPlayerView.player == nil)
			 {			 
				 AVPlayer * myPlayer = [[AVPlayer alloc] 
                                     initWithPlayerItem:self.myPlayerItem];
				 [self.myPlayerView setPlayer:myPlayer];
				 [myPlayer release];
				 
				 CGRect frame;
				 frame.origin.x = 0;
				 frame.origin.y = 0;
				 frame.size.width = 1024;
				 frame.size.height = 768;
				 
				 self.myPlayerView.frame = frame;
				 
				 if([Utilities getSettings].isShowLaps == YES)
				 {
					 [self.view insertSubview:self.myPlayerView 
                                    atIndex:[self.view.subviews count] - 3];
				 }
				 else {
					 [self.view insertSubview:self.myPlayerView 
                                    atIndex:[self.view.subviews count] - 2];
				 }
			 }
			 else {
				 [self.myPlayerView.player 
                           replaceCurrentItemWithPlayerItem:self.myPlayerItem];
			 }
		 }];
	}
	@catch (NSException * e) {
		success = NO;
	}
	return success;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
	
	if(context == &TracksStatusContext && [Utilities 
                   getSettings].videoMuted == YES)
	{
		NSArray * myTracks = self.myPlayerItem.tracks;
		for(int i = 0; i < [myTracks count]; i++)
		{
			if([[[myTracks objectAtIndex:i] assetTrack].mediaType 
                            isEqualToString:AVMediaTypeAudio] == YES)
			{
				((AVPlayerItemTrack *)[myTracks 
                            objectAtIndex:i]).enabled = NO;
			}
		}
		return;
	}
	
    if (context == &ItemStatusContext) {
        [self.myPlayerView.player play];
        return;
    }
    return;
}

- (void) playingDone: (NSNotification *) theNotification
{
 	if(++currentMovieCounter == [self.videoArray count])
	{
		currentMovieCounter = 0;
	}
	Video * tv = [self.videoArray objectAtIndex:currentMovieCounter];
	[self playMovie:tv showControls:YES];
}

---- <snip> -----

- (void) stopImmed
{
	self.quit = YES;
	amDone = YES;
	if(self.myPlayerView.player != nil)
	{
		//stop player
		self.myPlayerView.player.rate = 0;
		self.myPlayerView.player = nil;
	}
	
	[self.myPlayerView removeFromSuperview];
	self.myPlayerView = nil;
	self.myDashViewController.running = NO;
Post edited by jamesbrannan on

Replies

Sign In or Register to comment.