(1 july 2013) ios basic development 4 - multimedia
DESCRIPTION
iOS Basic Development 4 - Multimedia by Eakapong Kattiya [email protected] www.ibluecode.com +66 086-673-2111TRANSCRIPT
iPhone Developer Basic Program
Multimedia
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
Course Outline
1. Introduction Xcode & Language
2. Human Interface Design
3. UITableViewController & Datasource
4. Multimedia
5. Submit App Store
Course Outline
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
www.ibluecode.com/training.html
Day 1 - 5 Slidewww.slideshare.net/eakkattiya
Additional [email protected]
twitter.com/eakkattiya facebook.com/eakapong.kattiya
Resources
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
UIImagePickerController Camera & Photo Gallery
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
#import <UIKit/UIKit.h>
@interface MediaViewController : UIViewController<UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (IBAction)selectPhoto:(id)sender;
@end
UIImagePickerController
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
@implementation MediaViewController
- (void)viewDidLoad{ [super viewDidLoad]; self.navigationController.topViewController.title = @"Media" ;}
- (IBAction)selectPhoto:(id)sender {
UIImagePickerController *imagePicker = [UIImagePickerController new]; [imagePicker setDelegate:self];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; }else{ [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; } [self presentViewController:imagePicker animated:YES completion:nil];
}
@end
UIImagePickerController
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
@implementation MediaViewController
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [picker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"info =%@",[info description]);
UIImage *pickedImage = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
self.myImageView.image = pickedImage ;}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end
UIImagePickerControllerDelegate
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
NSLog(@"info =%@",[info description]);
NSLog & Description
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
MediaPlayer & AVFoundationFramework
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
• AV FoundationSupport : .mp3 ,.aac and more..
• MediaPlayer Support : mov, mp3, mp4, mpv, and 3gp
• Http Live StreamingSupport : .M3U8 and .ts
AUDIO & VIDEO
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
Class : MPMoviePlayerViewController
Framework : MediaPlayer/MediaPlayer.h
Sample Code : MoviePlayer
Init : - initWithContentOfURL : (NSURL*)
Property : -
Method : prepareToPlay / play
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
การเรียกใช้งาน1. Add Framework <MediaPlayer>
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
การเรียกใช้งาน1. Add Framework <MediaPlayer>
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
การเรียกใช้งาน1. Add Framework <MediaPlayer>
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
การเรียกใช้งาน1. Add Framework <MediaPlayer>2. #import <MediaPlayer/MediaPlayer.h>3. Create NSURL4. Init MPMoviePlayerViewController5. call [self presentMoviePlayerViewControllerAnimated:YES] ;
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
#import <UIKit/UIKit.h>#import <MediaPlayer/MediaPlayer.h>
@interface MediaViewController : UIViewController<UIActionSheetDelegate>{
MPMoviePlayerViewController *mediaPlayerVC ; MPMoviePlayerController *mediaPlayer ;
}
- (IBAction)selectMedia:(id)sender;
@end
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
@implementation MediaViewController
- (IBAction)selectMedia:(id)sender { UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"Select Media" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Music", @"Streaming", @"Movie", @"Voice Memo", nil]; [action showInView:self.view];}
-(void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex{ if(buttonIndex == 0){ [self playMusic]; }else if(buttonIndex == 1){ [self playStreaming]; }else if(buttonIndex == 2){ [self playMovie]; }else if(buttonIndex == 3){ [self playVoiceMemo]; }}@end
UIActionSheet & Delegate
Monday, June 24, 13
- (void)playStreaming{ NSURL *url = [NSURL URLWithString: @"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"]; mediaPlayerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url]; [self presentMoviePlayerViewControllerAnimated:mediaPlayerVC];
}
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MPMoviePlayerViewController
Monday, June 24, 13
การเรียกใช้งาน1. Add Framework <MediaPlayer>2. #import <MediaPlayer/MediaPlayer.h>3. Create NSURL4. Init MPMoviePlayerController5. call setFrame6. call addSubView7. call play
MPMoviePlayerController
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
- (void)playMovie{ NSURL *url = [[NSBundle mainBundle] URLForResource:@"movie" withExtension:@"m4v"]; mediaPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[mediaPlayer.view setFrame:self.myImageView.bounds]; [mediaPlayer prepareToPlay];
[self.view addSubview:mediaPlayer.view]; }
MPMoviePlayerController
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>
@interface MediaViewController : UIViewController<AVAudioPlayerDelegate>
{ AVAudioPlayer *audioPlayer ;}
- (IBAction)pauseMedia:(id)sender;
@end
AVFoundation Framework / AVAudioPlayer
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
@implementation MediaViewController
- (void)playMusic{ NSURL *url = [[NSBundle mainBundle] URLForResource:@"background" withExtension:@"mp3"]; NSError *error = nil ; if(!audioPlayer){ audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; } [audioPlayer play];}
- (IBAction)pauseMedia:(id)sender { [audioPlayer pause]; }
@end
AVFoundation Framework / AVAudioPlayer
Monday, June 24, 13
#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>
@interface MediaViewController : UIViewController<AVAudioRecorderDelegate>
{ AVAudioRecorder *audioRecorder;}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *recordButton;
- (IBAction)recordOrStop:(id)sender;- (void)playVoiceMemo;@end
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
AVFoundation Framework / AVAudioRecorder
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
AVFoundation Framework / AudioRecorder@implementation MediaViewController
- (IBAction) recordOrStop: (id) sender { NSString *soundPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/record.caf"]; NSURL *url = [NSURL fileURLWithPath:soundPath]; if (audioRecorder) { [self.recordButton setTitle:@"Stop"]; [audioRecorder stop]; audioRecorder = nil ;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; } else { [self.recordButton setTitle:@"Recording.."]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithFloat:44100.0], AVSampleRateKey, [NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt:1], AVNumberOfChannelsKey, [NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey, nil]; AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:nil]; audioRecorder = newRecorder; audioRecorder.delegate = self; [audioRecorder prepareToRecord]; [audioRecorder record]; }}@end
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
AudioRecorderDelegate@implementation MediaViewController
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ NSLog(@"success recording");}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error{ NSLog(@"fail recording");}
@end
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Play AudioRecorder
@implementation MediaViewController
- (void)playVoiceMemo{ NSString *soundPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/record.caf"]; NSURL *url = [NSURL fileURLWithPath:soundPath]; NSError *error = nil ; audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; [audioPlayer play];}@end
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Shake Device
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Detect Device Shake#pragma mark Motion- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"motionBegan");}- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"motionCancelled");}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"motionEnded"); if(event.subtype==UIEventSubtypeMotionShake) { // Code at shake event [self vibrateDevice]; }}
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Vibrate Device
AudioToolbox Framework
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Shake Device
-(void)vibrateDevice{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Apple Sound Effect
AudioToolbox Framework
Monday, June 24, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Apple Sound Effect
-(void)playAudioEffect:(NSInteger)number{ //Tink.aiff, //Tock.aiff, //iPod Click.aiff NSArray *appleEffects = @[@"Tink",@"Tock",@"iPod Click"]; NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:[appleEffects objectAtIndex:number] ofType:@"aiff"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID); AudioServicesPlaySystemSound(soundID);}
Monday, June 24, 13