音乐模块的播放功能

利用  AVAudioPlayer 来播放音乐 :
 
- 声明对象
 
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@property (nonatomic, strong) AVAudioPlayer *avAudioPlayer;
 
- 播放函数
 
NSURL *url = [[NSURLalloc] initWithString:[AppUtilurlEncodeToURLString:urlStr]];
NSData *audioData = [NSDatadataWithContentsOfURL:url];
_avAudioPlayer = [[AVAudioPlayeralloc] initWithData:audioData error:nil];
[_avAudioPlayerplay];
_avAudioPlayer.delegate = self;
_stringUrlAudio = urlStr;
 
<附加函数>
 
+ (NSString *)urlEncodeToURLString:(NSString *)urlString
{
    if (IS_NS_STRING_EMPTY(urlString)) {
        return@"";
    }
    NSString *encodingUrl = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return encodingUrl;
}
 
- 代理函数
 
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self.voiceImageViewstopAnimating];
    self.weplayPage.avAudioPlayer = nil;
}
 
图标的播放时的动态效果代码如下:
 
- (UIImageView *)voiceImageView
{
    if (!_voiceImageView) {
        _voiceImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(14, 0, 12, 12)];
        _voiceImageView.centerY = self.soundImageView.bounds.size.height/2.0;
        _voiceImageView.contentMode = UIViewContentModeScaleAspectFill;
        NSArray *arr = @[@"ic_yiqiwan_details_play1",@"ic_yiqiwan_details_play2",@"ic_yiqiwan_details_play3"];
        [self.soundImageViewaddSubview:_voiceImageView];
       
        NSMutableArray *imageArray = [NSMutableArrayarray];
        for (int i=0;i<arr.count;++i) {
            UIImage *tmpimage = [UIImageimageNamed:arr[i]];
            [imageArray cl_addObject:tmpimage];
        }
        _voiceImageView.animationImages = imageArray;
        _voiceImageView.animationRepeatCount = 0;
        _voiceImageView.animationDuration = 1.5;
        _voiceImageView.image = [UIImageimageNamed:@"ic_yiqiwan_details_play3"];
        //[_voiceImageView startAnimating];
    }
    return_voiceImageView;
}
 
动画效果如下:
 
 C9B0CD5C 5C11 4029 B109 D138BC2F41C4 
 
- OVER
posted @ 2017-11-24 01:05  Levi.duan  阅读(276)  评论(0编辑  收藏  举报