Ios-audio-video

提供:Dev Guides
移動先:案内検索

iOS-オーディオとビデオ

オーディオとビデオは、最新のデバイスでは非常に一般的です。 iOSでは、それぞれ AVFoundation.framework および MediaPlayer.framework を使用してサポートされています。

関与するステップ

ステップ1 *-簡単な Viewベースのアプリケーション*を作成します。

ステップ2 *-プロジェクトファイルを選択し、ターゲットを選択してから、 *AVFoundation.framework および MediaPlayer.framework を追加する必要があります。

  • ステップ3 *-ViewController.xibに2つのボタンを追加し、オーディオとビデオをそれぞれ再生するアクションを作成します。

ステップ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

Note

期待どおりの出力を得るために、オーディオファイルとビデオファイルを追加する必要があります。

出力

アプリケーションを実行すると、次の出力が得られます-

iOSチュートリアル

ビデオを再生をクリックすると、以下に示すような出力が得られます-

iOSチュートリアル

[音声の再生]をクリックすると、音声が聞こえます。