利用
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
;
}
动画效果如下:
- OVER