iOS 教程
- 第1节:IOS 简介
- 第2节:IOS 环境搭建
- 第3节:IOS Objective C 基础知识
- 第4节:IOS 创建第一款iPhone应用程序
- 第5节:IOS 操作(action)和输出口(Outlet)
- 第6节:IOS 委托(Delegates)
- 第7节:IOS UI元素
- 第8节:IOS 加速度传感器(accelerometer)
- 第9节:IOS 通用应用程序
- 第10节:IOS 相机管理
- 第11节:IOS 定位操作
- 第12节:IOS SQLite数据库
- 第13节:IOS 发送电子邮件
- 第14节:IOS 音频和视频(Audio & Video)
- 第15节:IOS 文件处理
- 第16节:IOS 地图开发
- 第17节:IOS 应用内购买
- 第18节:IOS 整合iAD
- 第19节:IOS GameKit
- 第20节:IOS 故事板(Storyboards)
- 第21节:IOS 自动布局
- 第22节:IOS Twitter和Facebook
- 第23节:IOS 内存管理
- 第24节:IOS 应用程序调试
IOS 音频和视频(Audio & Video)
简介
音频和视频在最新的设备中颇为常见。
将iosAVFoundation.framework和MediaPlayer.framework添加到Xcode项目中,可以让IOS支持音频和视频(Audio & Video)。
实例步骤
1、创建一个简单的View based application
2、选择项目文件、选择目标,然后添加AVFoundation.framework和MediaPlayer.framework
3、在ViewController.xib中添加两个按钮,创建一个用于分别播放音频和视频的动作(action)
4、更新ViewController.h,如下所示
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
MPMoviePlayerViewController *moviePlayer;
}
-(IBAction)playAudio:(id)sender;
-(IBAction)playVideo:(id)sender;
@end
5、更新ViewController.m,如下所示
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)playAudio:(id)sender{
NSString *path = [[NSBundle mainBundle]
pathForResource:@"audioTest" ofType:@"mp3"];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:
[NSURL fileURLWithPath:path] error:NULL];
[audioPlayer play];
}
-(IBAction)playVideo:(id)sender{
NSString *path = [[NSBundle mainBundle]pathForResource:
@"videoTest" ofType:@"mov"];
moviePlayer = [[MPMoviePlayerViewController
alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[self presentModalViewController:moviePlayer animated:NO];
}
@end
注意项
需要添加音频和视频文件,以确保获得预期的输出
输出
运行该程序,得到的输出结果如下
当我们点击 play video(播放视频)显示如下: