iOS开发----音频、视频的实现
2015-10-08 16:55
483 查看
在项目中先把必要的库添加上去!
![](https://img-blog.csdn.net/20151008165831074?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController :
UIViewController<AVAudioPlayerDelegate>
{
AVAudioPlayer * avAudioPlayer;//播放器
//UIProgressView * progressView;//控制播放进度
MPMoviePlayerViewController * _playerVc;
}
//
// ViewController.m
// Mp3
//
// Created by Mr.z on 15/10/8.
// Copyright © 2015年 An. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//设置点击播放图标
UIButton * button1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(100,
100, 60,
40)];
[button1 setTitle:@"play"
forState:UIControlStateNormal];
//设置按钮点击事件(播放音乐)
[button1 addTarget:self
action:@selector(play)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button1];
//获取音频文件
NSString * string = [[NSBundle
mainBundle]pathForResource:@"专属味道"
ofType:@"mp3"];
//音频格式转换为URl格式
NSURL * url = [NSURL
fileURLWithPath:string];
avAudioPlayer = [[AVAudioPlayer
alloc]initWithContentsOfURL:url
error:nil];
//设置委托
avAudioPlayer.delegate =
self;
[avAudioPlayer
prepareToPlay];
NSString *path = [[NSBundle
mainBundle] pathForResource:@"movie"
ofType:@"mp4"];
NSLog(@"%@", path);
NSURL *url1 = [NSURL
fileURLWithPath:path];
_playerVc = [[MPMoviePlayerViewController
alloc]
initWithContentURL:url1];
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button.frame =
CGRectMake(200,
200, 100, 40);
button.backgroundColor = [UIColor
redColor];
[button setTitle:@"run"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(play1)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
}
//按钮触发的事件
-(void)play
{
[avAudioPlayer
play];
}
- (void)play1
{
[self
presentMoviePlayerViewControllerAnimated:_playerVc];
[_playerVc.moviePlayer
play];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行效果:
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController :
UIViewController<AVAudioPlayerDelegate>
{
AVAudioPlayer * avAudioPlayer;//播放器
//UIProgressView * progressView;//控制播放进度
MPMoviePlayerViewController * _playerVc;
}
//
// ViewController.m
// Mp3
//
// Created by Mr.z on 15/10/8.
// Copyright © 2015年 An. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//设置点击播放图标
UIButton * button1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(100,
100, 60,
40)];
[button1 setTitle:@"play"
forState:UIControlStateNormal];
//设置按钮点击事件(播放音乐)
[button1 addTarget:self
action:@selector(play)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button1];
//获取音频文件
NSString * string = [[NSBundle
mainBundle]pathForResource:@"专属味道"
ofType:@"mp3"];
//音频格式转换为URl格式
NSURL * url = [NSURL
fileURLWithPath:string];
avAudioPlayer = [[AVAudioPlayer
alloc]initWithContentsOfURL:url
error:nil];
//设置委托
avAudioPlayer.delegate =
self;
[avAudioPlayer
prepareToPlay];
NSString *path = [[NSBundle
mainBundle] pathForResource:@"movie"
ofType:@"mp4"];
NSLog(@"%@", path);
NSURL *url1 = [NSURL
fileURLWithPath:path];
_playerVc = [[MPMoviePlayerViewController
alloc]
initWithContentURL:url1];
UIButton *button = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
button.frame =
CGRectMake(200,
200, 100, 40);
button.backgroundColor = [UIColor
redColor];
[button setTitle:@"run"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(play1)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
}
//按钮触发的事件
-(void)play
{
[avAudioPlayer
play];
}
- (void)play1
{
[self
presentMoviePlayerViewControllerAnimated:_playerVc];
[_playerVc.moviePlayer
play];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行效果:
相关文章推荐
- iOS开发之路--微博“更多”页面
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios开发之环境搭建-01
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- 学习Objective-C:入门手册
- iOS开发 简单实现视频音频的边下边播
- iOS开发 — Quartz 2D知识点应用 (制作了一个Demo,源代码)
- ios开发对配置文件plist的操作
- iphone5和4的屏幕兼容问题
- 9秒的课程只为培养靠谱的IOS和HTML5开发人才而定制
- iOS开发学习笔记 1-0 开篇
- iOS开发学习笔记 2-1 C语言部分 初识C语言
- iOS开发学习笔记 2-2 C语言部分 类型、运算符与表达式