音乐模块的播放功能

利用   AVAudioPlayer 来播放音乐 :
 
- 声明对象
 
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@property ( nonatomic , strong ) AVAudioPlayer *avAudioPlayer;
 
- 播放函数
 
NSURL *url = [[ NSURL alloc ] initWithString :[ AppUtil urlEncodeToURLString :urlStr]];
NSData *audioData = [ NSData dataWithContentsOfURL :url];
_avAudioPlayer = [[ AVAudioPlayer alloc ] initWithData :audioData error : nil ];
[ _avAudioPlayer play ];
_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 . voiceImageView stopAnimating ];
    self . weplayPage . avAudioPlayer = nil ;
}
 
图标的播放时的动态效果代码如下:
 
- ( UIImageView *)voiceImageView
{
    if (! _voiceImageView ) {
        _voiceImageView = [[ UIImageView alloc ] 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 . soundImageView addSubview : _voiceImageView ];
       
        NSMutableArray *imageArray = [ NSMutableArray array ];
        for ( int i= 0 ;i<arr. count ;++i) {
            UIImage *tmpimage = [ UIImage imageNamed :arr[i]];
            [imageArray cl_addObject :tmpimage];
        }
        _voiceImageView . animationImages = imageArray;
        _voiceImageView . animationRepeatCount = 0 ;
        _voiceImageView . animationDuration = 1.5 ;
        _voiceImageView . image = [ UIImage imageNamed : @"ic_yiqiwan_details_play3" ];
        //[_voiceImageView startAnimating];
    }
    return _voiceImageView ;
}
 
动画效果如下:
 
  C9B0CD5C 5C11 4029 B109 D138BC2F41C4 
 
- OVER

转载于:https://www.cnblogs.com/firstrate/p/7888051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值