IOS音(yīn)頻和(hé)視(shì)頻(Audio & Video)
簡介
音(yīn)頻和(hé)視(shì)頻在最新的設備中頗爲常見。
将iosAVFoundation.framework和(hé)MediaPlayer.framework添加到(dào)Xcode項目中,可以讓IOS支持音(yīn)頻和(hé)視(shì)頻(Audio & Video)。
實例步驟
1、創建一個簡單的View based application
2、選擇項目文(wén)件、選擇目标,然後添加AVFoundation.framework和(hé)MediaPlayer.framework
3、在ViewController.xib中添加兩個按鈕,創建一個用(yòng)于分别播放(fàng)音(yīn)頻和(hé)視(shì)頻的動作(zuò)(action)
4、更新ViewController.h,如下(xià)所示
#import
#import
#import
@interface ViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
MPMoviePlayerViewController *moviePlayer;
}
-(IBAction)playAudio:(id)sender;
-(IBAction)playVideo:(id)sender;
@end
5、更新ViewController.m,如下(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īn)頻和(hé)視(shì)頻文(wén)件,以确保獲得預期的輸出
網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發