1. 程式人生 > >post上傳視訊檔案到服務端

post上傳視訊檔案到服務端

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:^{

    }];

}