CS175
Chris Pollett
Nov 24, 2014
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIButton *playButton; @property (weak, nonatomic) IBOutlet UIButton *stopButton; @property (strong, nonatomic) AVAudioPlayer *audioPlayer; -(IBAction)play; -(IBAction)stop; @end
// // ViewController.m // AudioExample // // Created by Chris Pollett on 11/24/14. // Copyright 2010 San Jose State University. All rights reserved. // #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Get the file path to the song to play. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"warworlds" ofType:@"mp3"]; // Convert the file path to a URL. NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; //Initialize the AVAudioPlayer. self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; // Preloads the buffer and prepares the audio for playing. [self.audioPlayer prepareToPlay]; } -(IBAction)play { // Make sure the audio is at the start of the stream. self.audioPlayer.currentTime = 0; [self.audioPlayer play]; } -(IBAction)stop { [self.audioPlayer stop]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
Which of the following is true?
// // ViewController.m // VideoDemo // // Created by Chris Pollett on 11/24/14. // Copyright (c) 2014 Chris Pollett. All rights reserved. // #import "ViewController.h" #import <MediaPlayer/MediaPlayer.h> @interface ViewController () { MPMoviePlayerController *player; } @end @implementation ViewController - (void)viewDidLoad { NSString *url = [[NSBundle mainBundle] pathForResource:@"fredott" ofType:@"m4v"]; player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]]; [player setControlStyle:MPMovieControlStyleFullscreen]; [player setFullscreen:YES]; [player prepareToPlay]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; //this callback is called when video is load [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player]; // callback called when video is finished //---play movie--- [super viewDidLoad]; } - (void) moviePlayerLoadStateChanged:(NSNotification*)notification{ if ([player loadState] != MPMovieLoadStateUnknown) { [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; [player.view setFrame: self.view.bounds]; //here we play the video [self.view addSubview:player.view]; [player play]; } } - (void) movieFinishedCallback:(NSNotification*) aNotification { [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; NSLog(@"%f", player.currentPlaybackTime); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface CameraIPhoneViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> @property (nonatomic, weak) UIImageView *imageView; @property (nonatomic, weak) UIButton *takePictureButton; @property (nonatomic, weak) UIButton *selectFromCameraRollButton; - (IBAction)getCameraPicture:(id)sender; - (IBAction)selectExistingPicture; @end
#import "CameraIPhoneViewController.h" @implementation CameraIPhoneViewController - (void)viewDidLoad { if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { takePictureButton.hidden = YES; selectFromCameraRollButton.hidden = YES; } } // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - -(IBAction)getCameraPicture: (id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsImageEditing = YES; picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self presentModalViewController:picker animated: YES]; } -(IBAction)selectExistingPicture { if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:picker animated:YES]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" message:@"Device does not support a photo library" delegate:nil cancelButtonTitle:@"Darn!" otherButtonTitles:nil]; } } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { imageView.image = image; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); [picker dismissModalViewControllerAnimated:YES]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissModalViewControllerAnimated:YES]; } @end