首页 > 代码库 > iOS,多媒体相关
iOS,多媒体相关
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,多媒体相关