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