播放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

0 0
原创粉丝点击