播放MP3音频和gif动画的一个小模块
来源:互联网 发布:迁都南京 知乎 编辑:程序博客网 时间:2024/05/22 06:28
//
// GIFUseMode.m
// WebView
//
// Created by 周三 on 15-3-3.
// Copyright (c) 2015年 pluto. All rights reserved.
//
#import "GIFUseMode.h"
#import <ImageIO/ImageIO.h>
@implementation GIFUseMode
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (nil != self)
{
self.backgroundColor = [UIColorclearColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap)];
[selfaddGestureRecognizer:tap];
}
return self;
}
- (void)setmp3Name:(NSString *)mp3Name animateName:(NSString *)animateName
{
CGRect frame = CGRectMake(0,0,0,0);
frame.size = [UIImageimageNamed:animateName].size;
NSData *gif = [NSDatadataWithContentsOfFile: [[NSBundlemainBundle]pathForResource:animateNameofType:nil]];
CGSize size = [selfgetSizeWithHeight:frame.size.heightwidth:frame.size.width];
frame.size = size;
webView = [[UIWebViewalloc]initWithFrame:frame];
webView.scalesPageToFit =YES;
webView.userInteractionEnabled =NO;
[webViewloadData:gifMIMEType:@"image/gif"textEncodingName:nilbaseURL:nil];
webView.center =self.center;
NSString *imagePath =[[NSBundlemainBundle]pathForResource:animateNameofType:nil];
CGImageSourceRef cImageSource =CGImageSourceCreateWithURL((__bridgeCFURLRef)[NSURLfileURLWithPath:imagePath],NULL);
size_t imageCount = CGImageSourceGetCount(cImageSource);
float totalTime = 0;
for (size_t i =0; i < imageCount; i++)
{
NSDictionary *properties = (__bridgeNSDictionary *)CGImageSourceCopyPropertiesAtIndex(cImageSource, i,NULL);
NSDictionary *gifProperties = [propertiesvalueForKey:(__bridgeNSString *)kCGImagePropertyGIFDictionary];
NSString *gifDelayTime = [gifPropertiesvalueForKey:(__bridgeNSString* )kCGImagePropertyGIFDelayTime];
totalTime += [gifDelayTime floatValue];
}
// [NSTimer scheduledTimerWithTimeInterval:totalTime target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
webView.backgroundColor = [UIColorclearColor];
webView.opaque =NO;
[selfaddSubview:webView];
if (mp3Player ==nil)
{
NSURL *url = [[NSBundlemainBundle]URLForResource:mp3NamewithExtension:@"mp3"];
NSError *error;
mp3Player = [[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:&error];
[mp3Player play];
mp3Player.delegate =self;
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[selfremoveFromSuperview];
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
}
- (void)tap
{
[selfremoveFromSuperview];
[mp3Player stop];
if (nil != self.delegate)
{
[self.delegateanimationOver];
}
}
- (void)timerAction
{
[selfremoveFromSuperview];
if (nil != self.delegate)
{
[self.delegateanimationOver];
}
}
- (CGSize)getSizeWithHeight:(CGFloat)height width:(CGFloat)width
{
CGSize size = {0,0};
CGFloat fHeight = [UIScreenmainScreen].bounds.size.height;
CGFloat fWidth = [UIScreenmainScreen].bounds.size.width;
if (height < fHeight && width < fWidth)
{
size.height = height;
size.width = width;
}
else
{
if (height >= fHeight && width <= fWidth)
{
CGFloat mult = fHeight/height;
size.height = fHeight;
size.width = width*mult;
}
else if (height <= fHeight && width >= fWidth)
{
CGFloat mult = fWidth/width;
size.height = height*mult;
size.width = fWidth;
}
else if (height >= fHeight && width >= fWidth)
{
CGFloat multW = fWidth/width;
CGFloat multH = fHeight/height;
if (multW <= multH)
{
size.height = height*multW;
size.width = fWidth;
}
else
{
size.width = width*multH;
size.height = fHeight;
}
}
}
return size;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
代码下载地址 http://download.csdn.net/download/zhousan_zhou/8478889
- 播放MP3音频和gif动画的一个小模块
- MP3 和 音频播放器
- 显示gif动画(帧动画的播放)
- iOS播放MP3音频
- iOS播放MP3音频
- iOS播放MP3音频
- libmad学习进阶3-----基于oss音频驱动架构的一个mp3播放器
- 动画播放器的控制动画和音频的播放和暂停
- android 音频播放 以及 二维码模块中音频播放和震动(vibrate)的实现
- 一个透明的GIF动画
- Qt 中 实现gif动画的播放
- Qt 中 实现gif动画的播放
- iOS 简单的播放gif动画
- iOS 简单的播放gif动画
- 可以播放gif动画的ImageView
- iOS之GIF动画文件的播放
- 可以播放GIF动画的ImageView
- fedora18自带的video视频播放器不能播放视频(rmvb和avi)和音频(mp3)
- lua学习笔记
- JSP get提交中文乱码解决方法
- 【eoeAndroid社区索引】Android二维码知识汇总
- 细数微信QQ支付宝的四大难题?
- Q版人物
- 播放MP3音频和gif动画的一个小模块
- hdoj 2955 01背包变形+简单的概率论
- Version和Build的区别
- mysql服务器拒绝连接
- centos 安装 mysql出现conflicts with file from package错误的解决办法
- 什么是闭包(Closure)?
- @interface 和@property
- hive用mysql保存元数据(metadata)
- SVN cleanup 失败后的处理