IOS开发学习笔记(十四)——处理视频

这一小节我们看看iOS下对多媒体文件的处理。

Assets Library

iOS处理视频必须遵循以下原则:

  1. iOS下是不允许你直接通过访问文件的方式去访问应用程序以外的资源的,必须使用Asserts库;
  2. 你可能会理解多媒体包括音频、照片、图片、视频,但Assets库仅允许你处理在Photo这个应用中的资源,也就是照片和视频;
  3. 这些内容都是分组保存的(在你的‘照片’应用中),并且存在一个默认的组;
  4. 组内的文件可以过滤;
  5. 每个资源都有一个url,可以通过url来访问该资源;

Get Assets Library Details

下面是个小例子,我们枚举资源库对象。

  1. 添加AssetsLibrary.framework;
  2. 引入AssetsLibray头文件;
  3. 编写枚举处理:

- (IBAction)getAssetsLibDetailClick:(id)sender {
    numberOfGroups = 0;
    numberOfAssets = 0;
    
    ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
    
    NSUInteger groupTypes = ALAssetsGroupAll;
    
    // 成功枚举回调方法
    ALAssetsLibraryGroupsEnumerationResultsBlock resultBlock = ^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            numberOfGroups++;
            numberOfAssets += group.numberOfAssets;
            NSLog(@"the name of the group is, %@", [group valueForProperty:ALAssetsGroupPropertyName]);
        } else {
            NSString *msg = [NSString stringWithFormat:@"There are %d groups with %d assets.", numberOfGroups, numberOfAssets];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Assets Status" message:msg delegate:nil cancelButtonTitle:@"okey" otherButtonTitles:nil, nil];
            
            [alert show];
        }
    };
    
    // 失败回调方法
    ALAssetsLibraryAccessFailureBlock failedBlock = ^(NSError *err) {
        NSLog(@"Error is %@", [err localizedDescription]);
    };
    
    // 根据groupType枚举group,注意是block中是异步处理,所以需要将最后的结果放在group=NO中
    [lib enumerateGroupsWithTypes:groupTypes usingBlock:resultBlock failureBlock:failedBlock];
    

}

PlayVideo

  1. 添加MediaPlayer库;
  2. 实现一个MPMoviePlayerViewController的子类:
    .h头文件:
    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    
    
    @interface MoviePlayerController : MPMoviePlayerViewController
    
    
    @end

    .m实现文件:
    #import "MoviePlayerController.h"
    
    @implementation MoviePlayerController
    
    // 6.0之后使用以下两个方法控制横屏、竖屏
    - (BOOL)shouldAutorotate {
        // 支持自动横竖屏切换
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        // 必须shouldAutorotate返回YES
        // 返回支持的方向
        return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
    }
    
    
    // 6.0之前用于控制横、竖
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            return YES;
        }
        return NO;
    }
    
    
    
    @end
    


  3. 然后调用播放器:

    - (IBAction)playMovie:(id)sender {
        NSString *urlstring = @"http://xxxx.com/ring/Q350_2007861557.3gp";
        NSURL *url = [NSURL URLWithString:urlstring];
        
        MPMoviePlayerViewController *playerViewController = [[MoviePlayerController alloc] initWithContentURL:url];
        [self presentMoviePlayerViewControllerAnimated:playerViewController];
    }

  4. 即可实现打开播放器全屏播放。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于iOS开发中的视频转码,你可以使用AVFoundation框架来实现。AVFoundation提供了一些类和方法,可以处理视频数据、录制和播放媒体等操作。 要进行视频转码,你可以按照以下步骤进行操作: 1. 导入AVFoundation框架到你的项目中。 2. 创建一个AVAsset对象,代表要转码的视频文件。可以使用AVURLAsset来加载本地文件或者使用AVAssetReader来读取网络流。 3. 创建一个AVAssetExportSession对象,用于配置转码的参数和输出文件格式。你可以指定输出文件的格式、压缩质量、分辨率等。 4. 设置输出文件的路径和名称。 5. 调用AVAssetExportSession的exportAsynchronously(completionHandler:)方法开始转码。你可以通过completionHandler来获取转码进度和结果。 以下是一个简单的示例代码: ```swift import AVFoundation func transcodeVideo(sourceURL: URL, destinationURL: URL) { let asset = AVURLAsset(url: sourceURL) guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetMediumQuality) else { return } exportSession.outputURL = destinationURL exportSession.outputFileType = .mp4 exportSession.exportAsynchronously { switch exportSession.status { case .completed: // 转码成功 print("Transcoding completed.") case .failed: // 转码失败 if let error = exportSession.error { print("Transcoding failed with error: \(error.localizedDescription)") } case .cancelled: // 转码被取消 print("Transcoding cancelled.") default: break } } } // 调用示例 let sourceURL = URL(fileURLWithPath: "path/to/source/video.mov") let destinationURL = URL(fileURLWithPath: "path/to/output/video.mp4") transcodeVideo(sourceURL: sourceURL, destinationURL: destinationURL) ``` 请注意,上述代码只是一个简单示例,你可能需要根据你的具体需求进行更多的参数配置和错误处理。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值