新聞中心
IOS音頻和視頻(Audio & Video)
簡(jiǎn)介
音頻和視頻在最新的設(shè)備中頗為常見(jiàn)。

創(chuàng)新互聯(lián)建站專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、勃利網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為勃利等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
將iosAVFoundation.framework和MediaPlayer.framework添加到Xcode項(xiàng)目中,可以讓IOS支持音頻和視頻(Audio & Video)。
實(shí)例步驟
1、創(chuàng)建一個(gè)簡(jiǎn)單的View based application
2、選擇項(xiàng)目文件、選擇目標(biāo),然后添加AVFoundation.framework和MediaPlayer.framework
3、在ViewController.xib中添加兩個(gè)按鈕,創(chuàng)建一個(gè)用于分別播放音頻和視頻的動(dòng)作(action)
4、更新ViewController.h,如下所示
#import#import #import @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
注意項(xiàng)
需要添加音頻和視頻文件,以確保獲得預(yù)期的輸出
輸出
運(yùn)行該程序,得到的輸出結(jié)果如下
當(dāng)我們點(diǎn)擊 play video(播放視頻)顯示如下:
當(dāng)前文章:創(chuàng)新互聯(lián)IOS教程:iOS音頻和視頻(Audio&Video)
文章起源:http://www.dlmjj.cn/article/cdpsdgo.html


咨詢(xún)
建站咨詢(xún)
