你好,歡迎來到IOS教程網

 Ios教程網 >> IOS基礎知識 >> IOS入門簡介 >> iOS音頻和視頻(Audio & Video)

iOS音頻和視頻(Audio & Video)

編輯:IOS入門簡介

IOS音頻和視頻(Audio & Video)


簡介

音頻和視頻在最新的設備中頗為常見。

將iosAVFoundation.framework和MediaPlayer.framework添加到Xcode項目中,可以讓IOS支持音頻和視頻(Audio & Video)。

實例步驟

1、創建一個簡單的View based application

2、選擇項目文件、選擇目標,然後添加AVFoundation.framework和MediaPlayer.framework

3、在ViewController.xib中添加兩個按鈕,創建一個用於分別播放音頻和視頻的動作(action)

4、更新ViewController.h,如下所示

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@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

注意項

需要添加音頻和視頻文件,以確保獲得預期的輸出

輸出

運行該程序,得到的輸出結果如下

AudioVideo_Output

當我們點擊 play video(播放視頻)顯示如下:

video_Output

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved