做(zuò)自(zì)由與創造的先行(xíng)者

iOS音頻(pín)和(hé)視(shì)頻(pín)(Audio & Video)

iOS開(kāi)發手冊

IOS音頻(pín)和(hé)視(shì)頻(pín)(Audio & Video)

簡介

音頻(pín)和(hé)視(shì)頻(pín)在最新的設備中頗為(wèi)常見(jiàn)。

将iosAVFoundation.framework和(hé)MediaPlayer.framework添加到Xcode項目中,可(kě)以讓IOS支持音頻(pín)和(hé)視(shì)頻(pín)(Audio & Video)。

實例步驟

1、創建一(yī)個簡單的View based application

2、選擇項目文件、選擇目标,然後添加AVFoundation.framework和(hé)MediaPlayer.framework

3、在ViewController.xib中添加兩個按鈕,創建一(yī)個用于分别播放音頻(pín)和(hé)視(shì)頻(pín)的動作(zuò)(action)

4、更新ViewController.h,如(rú)下(xià)所示

#import

#import

#import

@interface ViewController : UIViewController

{

AVAudioPlayer *audioPlayer;

MPMoviePlayerViewController *moviePlayer;

}

-(IBAction)playAudio:(id)sender;

-(IBAction)playVideo:(id)sender;

@end

5、更新ViewController.m,如(rú)下(xià)所示

#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

注意項

需要(yào)添加音頻(pín)和(hé)視(shì)頻(pín)文件,以确保獲得預期的輸出

網站建設開(kāi)發|APP設計開(kāi)發|小程序建設開(kāi)發
下(xià)一(yī)篇:iOS發送電子郵件
上(shàng)一(yī)篇:iOS 故事闆(Storyboards)