【问题标题】:IOS 7 base64 video encodingIOS 7 base64 视频编码
【发布时间】:2013-12-19 00:41:16
【问题描述】:

我在 iOS 7 中尝试使用 base64 编码视频时遇到问题。

我不确定我的 NSdata 是否设置正确。

视频正在我的 iOS 模拟器上播放,不过还好。但是,当代码到达 NSData 块时,它什么也不做,我的数据和 base64String 日志返回 null。

请检查我的代码

//Try to encode Video
//Path of the video
NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone"  ofType:@"m4v"];
//startthe player
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(movieFinishCallBack) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
//set the frame
player.view.frame = CGRectMake(10,10,300,300);
[self.view addSubview:player.view];
//start to play
[player play];

NSData *data = [[NSData dataWithContentsOfFile:@"trailer_iphone.m4v" ]base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *base64String = [data base64EncodedStringWithOptions:2];
NSLog(@"data = %@ base64String = %@",data, base64String);

非常感谢!

【问题讨论】:

    标签: video ios7 base64 mpmovieplayercontroller video-encoding


    【解决方案1】:

    我明白了!!!! 在 NSData 中,我不能直接将内容分配为文件。 首先必须声明视频路径,就像视频播放器一样。 例如:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@"m4v"];
    

    然后我们像这样声明引用先前路径的 NSData:

    NSData *data = [[NSData dataWithContentsOfFile:path]
    

    最后我们只需要设置 iOS7 base64 编码支持:

    NSData *data = [[NSData dataWithContentsOfFile:path]base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    

    就是这样!!!完全编码!!!

    如果您希望将代码分成小块,请查看link

    我最后的方法是这个:

    -(void)videoBreak{     
     NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@"m4v"];    
     NSData *data = [[NSData dataWithContentsOfFile:url]base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];    
        NSUInteger length = [data length];
        NSLog(@"First log %@ length = %i",data,length);
        NSUInteger chunkSize = 4 * 1024;
        NSUInteger offset = 0;
        do {
        NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
        NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[data bytes] + offset
        length:thisChunkSize
        freeWhenDone:NO];
        offset += thisChunkSize;
        // do something with chunk        
        NSLog(@"chunk = %@ \"n\"offset = %i \"n\"thisChunkSize = %i \"n\"lenght = %i",chunk,     offset,thisChunkSize, length);
        } while (offset < length);
        }
    

    【讨论】:

      猜你喜欢
      • 2013-10-05
      • 1970-01-01
      • 2011-12-20
      • 2015-05-24
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多