標籤:
1.本地音頻播放
2.本地視頻播放
本地音頻播放
匯入標頭檔
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
import "ViewController.h"
@interface ViewController ()
//AVAudioPlayer要為全域變數才能播放
@property (strong,nonatomic) AVAudioPlayer *audioPlayer;
@end
@implementation ViewController
@synthesize audioPlayer=_audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.navigationItem setTitle:@"音頻"];
//播放音頻;注意:如果點擊了stop,那麼一定要讓播放器重新建立,否則會出現一些莫名其面的問題
[self.getAudioPlayer play];
}
/**
*建立音頻播放器
*return 音頻播放器
*/
-(AVAudioPlayer *)getAudioPlayer{
NSString *path=[[NSBundle mainBundle] pathForResource:@"愛的太遲"ofType:@"mp3"];
NSURL *url=[NSURL fileURLWithPath:path];
//建立一個播放器
_audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//音量0.0-1.0之間
_audioPlayer.volume=0.2;
//迴圈次數
_audioPlayer.numberOfLoops=1;
//播放位置
_audioPlayer.currentTime=0.0;
//聲道數
NSUInteger channels=_audioPlayer.numberOfChannels;//唯讀屬性
//期間
NSTimeInterval duration=_audioPlayer.duration;//擷取期間
//分配播放所需的資源,並將其加入內部播放隊列
[_audioPlayer prepareToPlay];
return _audioPlayer;
}
@end
本地視頻播放
匯入標頭檔
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
import "ViewController.h"
@interface ViewController ()
//視頻播放控制器
@property (strong,nonatomic) MPMoviePlayerController *moviePlayer;
@end
@implementation ViewController
@synthesize moviePlayer=_moviePlayer;
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.navigationItem setTitle:@"視頻"];
//播放視頻
[self.getMoviePlayer play];
}
/**
*建立視頻控制器
*return 視頻控制器
*/
-(MPMoviePlayerController *)getMoviePlayer{
NSString *path=[[NSBundle mainBundle] pathForResource:@"DotA2官方宣傳片"ofType:@"mp4"];
NSURL *url=[NSURL fileURLWithPath:path];
_moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
_moviePlayer.view.frame=self.view.frame;
//自動調整長寬
_moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_moviePlayer.view];
return _moviePlayer;
}
iOS,多媒體相關