#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface RootViewController : UIViewController<AVAudioPlayerDelegate> { //声明音乐播放器 AVAudioPlayer *_audioPlayer; } - (IBAction)pressStart:(id)sender; - (IBAction)pressStop:(id)sender; - (IBAction)pressVolume:(id)sender; - (IBAction)pressProgress:(id)sender; @property (strong, nonatomic) IBOutlet UISlider *progressSlider; @end #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //获得音频路径 NSString *mp3String=[[NSBundle mainBundle]pathForResource:@"song1" ofType:@"mp3"]; NSURL *url=[NSURL fileURLWithPath:mp3String]; _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; //设置音乐播放器的代理回调 _audioPlayer.delegate=self; //获得音频播放的总时间单位为秒 NSLog(@"%f",_audioPlayer.duration); //获得当前播放时间 //NSLog(@"%f",_audioPlayer.currentTime); //每隔一秒调用timerClick方法 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerClick) userInfo:nil repeats:YES]; } -(void)timerClick { NSLog(@"%f",_audioPlayer.currentTime); self.progressSlider.value=_audioPlayer.currentTime/_audioPlayer.duration; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)pressStart:(id)sender { //将音频加载到内存 [_audioPlayer prepareToPlay]; //开始播放 [_audioPlayer play]; } - (IBAction)pressStop:(id)sender { //暂停 [_audioPlayer stop]; } - (IBAction)pressVolume:(id)sender { UISlider *slider=(UISlider *)sender; //通过slider.value控制音频播放器的音量 //音频播放器播放音量的范围为0~1 [_audioPlayer setVolume:slider.value]; } //设置播放进度 - (IBAction)pressProgress:(id)sender { //_audioPlayer.currentTime=self.progressSlider.value; _audioPlayer.currentTime=self.progressSlider.value*_audioPlayer.duration; } #pragma -mark AVAudioPlayerDelegate //当音乐成功播放结束时调用,进入中断(接电话,收短信)不会调用该方法 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { NSLog(@"fnishPlaying"); } //进入中断时暂停播放 - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player { [_audioPlayer stop]; } //停止中断时启动播放 - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags { [_audioPlayer play]; } @end
#import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface RootViewController : UIViewController { //声明视频播放器 MPMoviePlayerViewController *_playerController; } - (IBAction)pressStart:(id)sender; @end #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)pressStart:(id)sender { //获得MP4文件路经 // NSString *mp4String=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp4"]; NSString *mp4String=[[NSBundle mainBundle]pathForResource:@"1.mp4" ofType:nil]; //封装NSURL NSURL *url=[NSURL fileURLWithPath:mp4String];//封装本地文件的路径 //NSURL *u=[NSURL URLWithString:@"<#string#>"];//封装互联网的路径,也可以播放网络的MP4 _playerController=[[MPMoviePlayerViewController alloc]initWithContentURL:url]; //设置视频播放器的视频来源 _playerController.moviePlayer.movieSourceType=MPMovieSourceTypeFile;//类型为本地类型 //开始播放视频 [_playerController.moviePlayer play]; //隐藏播放器的导航条和控制条 _playerController.moviePlayer.controlStyle=MPMovieControlStyleNone; //给当前对象添加监听,当视频结束时调用监听的方法 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playEnd) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; //显示视频播放器页面 [self presentViewController:_playerController animated:YES completion:^{ }]; } -(void)playEnd { NSLog(@"播放结束"); [_playerController.moviePlayer stop]; } @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> { UITableView *_tbView1; UITableView *_tbView2; } @end #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _tbView1=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 160, 480)]; _tbView1.dataSource=self; _tbView1.delegate=self; //隐藏右侧滚动条,纵向 _tbView1.showsVerticalScrollIndicator=NO; [self.view addSubview:_tbView1]; _tbView2=[[UITableView alloc]initWithFrame:CGRectMake(160, 0, 160, 480)]; _tbView2.dataSource=self; _tbView2.delegate=self; [self.view addSubview:_tbView2]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma -mark UITableViewDataSources -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView==_tbView1) { return 30; }else{ return 40; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *str=@"strIdentifier"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; } cell.textLabel.text=[NSString stringWithFormat:@"%i",indexPath.row]; return cell; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { //如果左侧滚动时,要求右侧一起滚动,而且滚动的幅度相同 if (scrollView==_tbView1) { //设置右侧的滚动幅度与左侧相同 [_tbView2 setContentOffset:_tbView1.contentOffset]; }else { [_tbView1 setContentOffset:_tbView2.contentOffset]; } } #pragma -mark UITableViewDelegate -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView==_tbView1) { //随机值函数arc4random_uniform() return arc4random_uniform(100)+44; }else{ return arc4random_uniform(100)+33; } } @end
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)pressSuspend:(id)sender { //暂停下载 [_request clearDelegatesAndCancel]; } //http://121.41.88.127:8080/zhangchu/HandheldKitchen/ipad/20141227135528915.jpg - (IBAction)pressStart:(id)sender { //准备下载图片的接口 NSString *strURL=@"http://121.41.88.127:8080/zhangchu/HandheldKitchen/ipad/20141227135528915.jpg"; _request=[[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:strURL]]; //获得沙盒目录 NSString *sandBoxString=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSLog(@"%@",sandBoxString); NSString *imgString=[NSString stringWithFormat:@"%@/download.jpg",sandBoxString]; NSString *tempString=[NSString stringWithFormat:@"%@/temp",sandBoxString]; //设置下载文件的目录 [_request setDownloadDestinationPath:imgString]; //设置临时文件的目录 [_request setTemporaryFileDownloadPath:tempString]; //设置下载进度 [_request setDownloadProgressDelegate:self.pView];//设置让谁显示下载进度 //设置允许断点续传 [_request setAllowResumeForFileDownloads:YES]; //开始下载 [_request startAsynchronous]; } @end
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)pressBtn:(id)sender { //判断当前设备摄像头是否能用 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //打开照相机 UIImagePickerController *pickerController=[[UIImagePickerController alloc]init]; //设置相片来源 pickerController.sourceType=UIImagePickerControllerSourceTypeCamera; //设置图片是否允许编辑处理 pickerController.allowsEditing=YES; //设置代理回调 pickerController.delegate=self; //显示照相机 [self presentViewController:pickerController animated:YES completion:^{ }]; }else { UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"警告" message:@"当前设备不能用" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; [alertView show]; } } #pragma -mark UIImagePickerControllerDelegate //可以获得编辑的图片 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image=[info objectForKey:@"UIImagePickerControllerEditedImage"]; } @end