iOS-音頻和視頻,iOS-音頻視頻

來源:互聯網
上載者:User

iOS-音頻和視頻,iOS-音頻視頻
一、視頻

MPMoviePlayerController播放本地視頻資源

NSString *path = [[NSBundle mainBundle]pathForResource:@"root" ofType:@"mp4"];NSURL *url = [NSURL fileURLWithPath:path];_playcontroller = [[MPMoviePlayerController alloc]initWithContentURL:url];_playcontroller.view.frame = CGRectMake(0, 100, 320, 300);[_playcontroller play];[self.view addSubview:_playcontroller.view];

 MPMoviePlayerController播放網路資源(伺服器為自己搭建,地址不可使用)

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/media/root.mp4"];_playcontroller.view.frame = CGRectMake(0, 100, 320, 300);
[_playcontroller play];[self.view addSubview:_playcontroller.view];

 可以加一個通知,在視頻播放完畢的時候發出通知,將視圖從介面上移除。

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(removeMedia) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];}-(void)removeMedia{    [_playcontroller.view removeFromSuperview];}

 MPMoviePlayerViewController播放視頻資源

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/media/root.mp4"];MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];[self presentViewController:player animated:YES completion:nil];

 :

-(BOOL)shouldAutorotate{ return YES;}-(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskLandscape;}

 然後在實現下面代碼:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/media/root.mp4"];myMPMoviePlayerViewController *player = [[myMPMoviePlayerViewController alloc]initWithContentURL:url];[self presentViewController:player animated:YES completion:nil];

 為:

@interface ViewController : UIViewController{ //音頻播放器 AVAudioPlayer * _audioPalyer; IBOutlet UISlider *_slider; float currentProgress; NSTimer *_timer;}- (IBAction)playButton:(id)sender;- (IBAction)stopButton:(id)sender;@end

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    _slider.value = 0.0;}- (IBAction)playButton:(id)sender{   _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(moveSlide) userInfo:nil repeats:YES];        NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp3"];    NSData *data = [NSData dataWithContentsOfFile:path];    _audioPalyer = [[AVAudioPlayer alloc]initWithData:data error:nil];    [_audioPalyer play];        _slider.maximumValue = _audioPalyer.duration;    [_slider addTarget:self action:@selector(changeSlide) forControlEvents:UIControlEventValueChanged];}-(void)changeSlide{    _audioPalyer.currentTime = _slider.value;}-(void)moveSlide{    _slider.value = _audioPalyer.currentTime;}- (IBAction)stopButton:(id)sender{    UIButton *button = (UIButton *)sender;       if (button.tag == 0)    {        [_audioPalyer pause];        [button setTitle:@"繼續播放" forState:UIControlStateNormal];        button.tag = 1;        [_timer invalidate];        _timer = nil;    }    else    {        [_audioPalyer play];        [button setTitle:@"暫停" forState:UIControlStateNormal];        button.tag = 0;        [_slider addTarget:self action:@selector(changeSlide) forControlEvents:UIControlEventValueChanged];    }}

 :

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.