iOS,多媒體相關

來源:互聯網
上載者:User

標籤:

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,多媒體相關

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.