欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IOS多媒体开发(xcode9 Obje-C)--EMail/Camera/Video/Audio

程序员文章站 2022-04-11 15:17:08
...

1.首先对项目添加相关库文件:
IOS多媒体开发(xcode9 Obje-C)--EMail/Camera/Video/Audio
2.头文件中添加相关引用和变量:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate>{
    AVAudioPlayer *audioPlayer;
    AVPlayerViewController *avPlayerView;
    MFMailComposeViewController *mailView;
    UIImagePickerController *imagePicker;
    IBOutlet UIImageView *mImageView;
}

@end
  1. 相机,在main.storyboard中添加一按钮启动相机,action代码如下:
- (IBAction)openCamera:(id)sender {
    imagePicker = [[UIImagePickerController alloc]init];
    if(!imagePicker) {
        NSLog(@"No camera APP in your device.");
        return;
    }
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    //[self presentModalViewController:imagePicker animated:YES];
    [self presentViewController:imagePicker animated:YES completion:nil];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if(image == nil) {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    NSLog(@"image picker:%@", image);
    mImageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
  1. 电子邮件,通过按钮启动:
- (IBAction)sendEmail:(id)sender {
    mailView = [[MFMailComposeViewController alloc]init];
    if(!mailView) {
        NSLog(@"No Email APP in your Device.");
        return;
    }
    mailView.mailComposeDelegate = self;
    [mailView setSubject:@"My first email"];
    [mailView setMessageBody:@"Are you feel better right now." isHTML:NO];
    //[self presentModalViewController:mailComposer animated:YES];
    [self presentViewController:mailView animated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    if(result) {
        NSLog(@"Result:%ld", result);
    } else {
        NSLog(@"Error:%@", error);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

注意,旧版本中通过[self presentModalViewController:mailComposer animated:YES];来显示,关闭时对应为
[self dismissModalViewControllerAnimated:YES];

  1. 播放音频和视频文件:
- (IBAction)playVideo:(id)sender {
    NSLog(@"Start play vedio video_test.mp4");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"video_test" ofType:@"mp4"];
    AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
    avPlayerView = [AVPlayerViewController new];
    avPlayerView.player = player;
    [self presentViewController:avPlayerView animated:YES completion:nil];
}

- (IBAction)playAudio:(id)sender {
    NSLog(@"Start play audio audio_test.mp3");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"audio_test" ofType:@"mp3"];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    [audioPlayer play];
    
}

示例图:
IOS多媒体开发(xcode9 Obje-C)--EMail/Camera/Video/Audio

相关标签: IOS开发