您的位置:首页 > 移动开发 > IOS开发

iOS开发----音频、视频的实现

2015-10-08 16:55 483 查看
在项目中先把必要的库添加上去!



#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开发