First, the video
- The video player needs to add mediaplayer.framework.
- Video playback mainly provides two classes, one MPMoviePlayerController, and the other is Mpmovieplayerviewcontroller.
- Video resources can be obtained locally or from the server
MPMoviePlayerController playing local video resources
MPMoviePlayerController Play network resources (the server is built for itself, the address is not available)
You can add a notification that notifies you when the video has finished playing and removes the view from the interface.
Mpmovieplayerviewcontroller Playing video Resources
:
Mpmovieplayerviewcontroller How to make the video cross-screen playback.
Horizontal screen playback, need to inherit mpmovieplayerviewcontroller this class. Suppose Mympmovieplayerviewcontroller inherits this class.
We need to implement two methods in Mympmovieplayerviewcontroller.
Then implement the following code:
For:
Second, audio
Audio words we need frame avfoundation.framework
Audio, it is generally necessary to use the progress bar to show the progress of audio playback.
| 123456789101112 |
@interfaceViewController : UIViewController{ //音频播放器 AVAudioPlayer * _audioPalyer; IBOutletUISlider *_slider; float currentProgress; NSTimer*_timer;}- (IBAction)playButton:(id)sender;- (IBAction)stopButton:(id)sender;@end |
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- (void)viewDidLoad{ [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _slider.value = 0.0;} - (IBAction)playButton:(id)sender{ _timer = [NSTimerscheduledTimerWithTimeInterval:1.0f target:selfselector:@selector(moveSlide) userInfo:nilrepeats:YES]; NSString*path = [[NSBundlemainBundle]pathForResource:@"1"ofType:@"mp3"]; NSData*data = [NSDatadataWithContentsOfFile: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:selfaction:@selector(changeSlide) forControlEvents:UIControlEventValueChanged]; }} |
:
ios-Audio and video