post上传视频文件到服务端

来源:互联网 发布:nginx的优化 编辑:程序博客网 时间:2024/05/01 07:37

UIButton *videoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
videoBtn.frame = CGRectMake(20, 20, SCREENWIDTH/3, 40);

[videoBtn setImage:[UIImage imageNamed:@"录制视频.png"] forState:UIControlStateNormal];[videoBtn addTarget:self action:@selector(getVideoAction:) forControlEvents:UIControlEventTouchUpInside];[biggerKuang addSubview:videoBtn];

//添加视频模块
- (void)getVideoAction:(UIButton *)sender {
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){    UIImagePickerController* pickerView = [[UIImagePickerController alloc] init];    //UIImagePickerControllerSourceTypePhotoLibrary从手机视频调取,UIImagePickerControllerSourceTypeCamera调用摄像机    pickerView.sourceType = UIImagePickerControllerSourceTypeCamera;    //UIImagePickerControllerSourceTypePhotoLibrary 从手机视频调取,UIImagePickerControllerSourceTypeCamera调用摄像机    NSArray* availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];    //availableMedia[0]是照相机 availableMedia[1]摄像机    pickerView.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];    //设置前置摄像头    pickerView.cameraDevice = UIImagePickerControllerCameraDeviceFront;    pickerView.videoMaximumDuration = 30;    pickerView.delegate = self;    [self loadpoetry];    UILabel *head = [[UILabel alloc]initWithFrame:CGRectMake(20, 44, SCREENWIDTH - 40, 50)];    head.text = @"请点击录制视频按钮,并大声朗读以下诗句";    [head setTextColor:[UIColor blueColor]];    [head setTextAlignment:NSTextAlignmentCenter];    head.numberOfLines = 0;    [pickerView.view addSubview:head];    aview = [[UILabel alloc]initWithFrame:CGRectMake(10, head.bottom + 5, SCREENWIDTH - 20, 50)];

// aview.text = @”请阅读以下诗歌:锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦!”;
// aview.text = responseObject[@”message”];
[aview setTextColor:[UIColor orangeColor]];
[aview setFont:[UIFont fontWithName:@”Helvetica-Bold” size:18]];
[aview setTextAlignment:NSTextAlignmentCenter];
aview.numberOfLines = 0;
aview.backgroundColor = [UIColor clearColor];
[pickerView.view addSubview:aview];

    [self presentViewController:pickerView animated:true completion:^{    }];}else{    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有相机" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];    [alert show];    return;}

}

  • (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info
    {
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];

    NSString *videoPath = [NSString stringWithFormat:@”%@”,[info objectForKey:UIImagePickerControllerMediaURL]];
    _videoURL = [NSURL URLWithString:videoPath];
    NSLog(@”123%@”,_videoURL);
    //计算视频文件大小
    // NSString *_fileSize = [NSString stringWithFormat:@”%ld kb”, (long)[self getFileSize:[[_videoURL absoluteString] substringFromIndex:16]]];
    //计算视频总时长
    // NSString *_videoLen = [NSString stringWithFormat:@”%.0f s”, [self getVideoDuration:_videoURL]];
    // NSLog(@”视频大小%@,视频长度%@”,_fileSize,_videoLen);
    // [self encode];
    // NSLog(@”321%@”,_playUrl);

    //视频上传
    AFHTTPRequestOperationManager *mag = [AFHTTPRequestOperationManager manager];
    mag.responseSerializer = [AFHTTPResponseSerializer serializer];
    // NSString *url = @”http://192.168.0.115:8081/video/save“;
    NSString *url = [NSString stringWithFormat:@”%@video/save”,K_URL];
    [mag POST:url parameters:@{@”userPhone”:self.zh} constructingBodyWithBlock:^(id formData) {

    [formData appendPartWithFileURL:_videoURL name:@"video" error:NULL];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@”成功%@”,responseObject);
    NSString *str = @”success”;
    flog = [NSString stringWithFormat:@”%@”,str];
    NSLog(@”1111%@”,flog);
    [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@”失败%@”,error);

    }];

    [picker dismissViewControllerAnimated:true completion:^{

    }];

}

0 0
原创粉丝点击