求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

iOS教程
iOS (iPhone, iPad)教程
IOS - 快速入门
IOS - 开发环境配置
iOS - Objective-C基础
iOS - 创建第一个
IOS - 动作和插座
iOS - Delegates实例
iOS - UI元素
iOS - Accelerometer(加
IOS - 通用应用程序
IOS - 摄像头管理
iOS - 位置处理
iOS - SQLite 数据库
iOS - 发送电子邮箱
iOS - 音频和视频
IOS - 文件处理
IOS - 访问地图
iOS - 应用程序内购买
iOS - iAd 整合
 
 

iOS - 音频和视频
808 次浏览
36次  

简介

最新的设备 音频和视频是很常见的。在 IOS 中分别在 AVFoundation.framework 和 MediaPlayer.framewor 支持帮助下可实现操作。

涉及的步骤

1. 创建一个简单的应用程序。

2. 选择项目文件,选择目标,然后我们添加 AVFoundation.framework 和 MediaPlayer.framework.

3. 添加两个按钮ViewController.xib的和播放音频和视频分别创建一个动作。

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

注意

我们需要添加音频和视频文件,以确保我们得到预期的输出。

输出

现在,当我们运行程序时,我们会得到下面的输出。

当我们点击播放视频时,我们会得到一个输出,如下图所示。

我们点击播放音频的时候,会听到声音。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



808 次浏览
36次
 捐助