android中如何获取超时时长的异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中如何获取超时时长的异常相关的知识,希望对你有一定的参考价值。

android获取超时时长的异常方式如下:设置超时机制
client.getParams().setIntParameter(
HttpConnectionParams.SO_TIMEOUT, TIME_OUT_DELAY); // 超时设置
client.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT_DELAY);// 连接超时
这里设置了两种超时,第一种是请求超时,第二种时连接超时。
当向服务器发出请求后,请求和服务器建立socket连接,但是很长时间内都没有建立socket连接,这就时第一种请求超时,这种情况主要发生在请求了
一个不存在的服务器。超时之后,会抛出InterruptedIOException异常。
Timeout for blocking operations. The argument value is specified in
milliseconds. An InterruptedIOException is thrown if this timeout
expires.
参考技术A 以下是几个常用的异常提示处理
public String ExceptionCode(Exception e)
if (e instanceof HttpException)
return 网络异常;

else if (e instanceof SocketTimeoutException)
return 响应超时;

else if (e instanceof ConnectTimeoutException)
return 请求超时;

else if (e instanceof UnknownHostException)
return 无法连接服务器;

else if (e instanceof IOException)
return 网络异常

... ...本回答被提问者采纳

iPhone:如何获取从库中选择的视频时长?

【中文标题】iPhone:如何获取从库中选择的视频时长?【英文标题】:iPhone :How to get duration of video selected from library? 【发布时间】:2012-01-02 09:16:52 【问题描述】:

我正在使用UIImagePickerController 从库中选择视频文件。用户可以上传视频。

当用户想要捕获视频并上传时,我也在使用videoMaximumDuration 属性。

我想知道如何获取所选视频文件的时长?这样我就可以限制用户上传持续时间超过 20 秒的视频。

我可以通过此代码获得有关所选视频的一些基本信息:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

    selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
    NSError *error;
    NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:selectedVideoUrl.path error:&error];
    NSNumber * size = [properties objectForKey: NSFileSize];
    NSLog(@"Vide info :- %@",properties);

但是没有关于所选视频的持续时间。

谢谢...

【问题讨论】:

【参考方案1】:

得到了解决方案:我使用AVPlayerItem 类和AVFoundationCoreMedia 框架。

#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAsset.h>

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

    selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];

    CMTime duration = playerItem.duration;
    float seconds = CMTimeGetSeconds(duration);
    NSLog(@"duration: %.2f", seconds);

【讨论】:

您还需要链接CoreMedia框架,CMTimeGetSeconds才能正常工作。 我们可以裁剪一段特定时长的视频吗? @Maulik- 我见过这个但没有帮助。 获取 nan 以获取 mp4 文件...但是我可以通过 AVURLAsset 获取视频帧,所以我知道该文件在那里并且可以后一种方式处理... 滚动到本页底部以获得有效答案。【参考方案2】:

使用 AVPlayerItem 对这个问题的另一个答案对我不起作用,但这确实使用 AVURLAsset:

#import <AVFoundation/AVFoundation.h>

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

    NSURL *videoURL=[info objectForKey:@"UIImagePickerControllerMediaURL"];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

    NSTimeInterval durationInSeconds = 0.0;
    if (asset)
        durationInSeconds = CMTimeGetSeconds(asset.duration);

【讨论】:

【参考方案3】:

斯威夫特 5

无论使用didFinishPickingMediaWithInfo 还是didFinishRecordingToOutputFileAtURL,您都可以:

// needed only for didFinishPickingMediaWithInfo
let outputFileURL = info[.mediaURL] as! URL

// get the asset
let asset = AVURLAsset(url: outputFileURL)

// get the time in seconds
let durationInSeconds = asset.duration.seconds

【讨论】:

UIImagePickerControllerMediaURL 现在是 UIImagePickerController.InfoKey.mediaURL @OwaisYusufzai 谢谢,我已将代码更新为 swift 5【参考方案4】:

如果您使用AVFoundation 和AssetLibrary 框架,您可以枚举所有资产,仅对视频应用过滤器,并使用- (id)valueForProperty:(NSString *)property 方法获取每个视频的时长。通过ALAssetPropertyDuration 获取财产。下面的代码在控制台上打印出以下内容。

视频剪辑编号 0 为 66.80833333333334 秒

1 号视频剪辑为 190.06 秒

2 号视频剪辑为 13.74 秒

- (void)viewDidLoad 
[super viewDidLoad];
// Do any additional setup after loading the view.

if (!assetItems) 
    assetItems = [[NSMutableArray alloc] init];
 else 
    [assetItems removeAllObjects];


if (!assetLibrary) 
    assetLibrary = [[ALAssetsLibrary alloc] init];


ALAssetsLibraryGroupsEnumerationResultsBlock listBlock = ^(ALAssetsGroup *group, BOOL *stop) 
    if (group) 
        [group setAssetsFilter:[ALAssetsFilter allVideos]];
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
            if (result) 
                [assetItems addObject:result];
                NSString *duration = [result valueForProperty:ALAssetPropertyDuration];
                NSLog(@"video clip number %d is %@ seconds\n",index, duration);
            
        ];
    
;

ALAssetsLibraryAccessFailureBlock failBlock = ^(NSError *error)  // error handler block
    NSString *errorTitle = [error localizedDescription];
    NSString *errorMessage = [error localizedRecoverySuggestion];
    NSLog(@"%@...\n %@\n",errorTitle,errorMessage);
;
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:listBlock failureBlock:failBlock];

【讨论】:

【参考方案5】:

Swift 3.0 和 Swift 4

let outputFileURL = info[UIImagePickerControllerMediaURL] as! URL
// get the asset
let asset = AVURLAsset.init(url: outputFileURL) // AVURLAsset.init(url: outputFileURL as URL) in swift 3
// get the time in seconds
let durationInSeconds = asset.duration.seconds
print("==== Duration is ",durationInSeconds)

【讨论】:

【参考方案6】:

是的,您可以使用 MPMediaPlayerController 定义的属性“duration”。请尝试一下并检查输出。你可以参考这里duration property

尝试使用 MPMediaPlayerController 播放视频,然后使用属性“duration”。您将能够清楚地了解有关视频时长的详细信息。

【讨论】:

以上是关于android中如何获取超时时长的异常的主要内容,如果未能解决你的问题,请参考以下文章

Java如何实现任务超时处理

如何在Android中锁定屏幕时长按电源按钮启用GPS?

Android 获取File类视频文件的总时长

Android 获取File类视频文件的总时长

如何解决高并发,连接等待超时的异常

如何在 PHP 中获取视频时长、尺寸和大小?