IOS-audio and video, iOS-Audio Video

Source: Internet
Author: User

IOS-audio and video, iOS-Audio Video
I. Video

MPMoviePlayerController: Play local video resources

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 plays network resources (the server is built for itself and the address is unavailable)

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];

You can add a notification to send a notification when the video is played and remove the view from the interface.

- (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 ;}

Then, implement the following code:

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

Is:

@ Interface ViewController: UIViewController {// audio player 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 = [nst1_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: @ "Continue playback" forState: UIControlStateNormal]; button. tag = 1; [_ timer invalidate]; _ timer = nil;} else {[_ audioPalyer play]; [button setTitle: @ "pause" forState: UIControlStateNormal]; button. tag = 0; [_ slider addTarget: self action: @ selector (changeSlide) forControlEvents: UIControlEventValueChanged];}

:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.