Skip to content

Commit bae6e09

Browse files
author
敛心
committed
为AudioPlayer接口添加文件名适配
1 parent c8245c4 commit bae6e09

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

IOS/SDK/LuaViewSDK/Classes/lvsdk/LVAudioPlayer.m

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#import "LVHeads.h"
1313

1414
@interface LVAudioPlayer ()
15+
1516
@property(nonatomic,assign) BOOL playing;
17+
@property (nonatomic, copy) NSString *fileName;
18+
1619
@end
1720

1821
@implementation LVAudioPlayer{
@@ -44,16 +47,19 @@ -(id) init:(lua_State*) L{
4447
}
4548

4649
-(void) setPlayFileName0:(NSString*) fileName bundle:(LVBundle*) bundle{
50+
4751
NSString* path = [bundle resourcePathWithName:fileName];
4852
if( path ) {
4953
NSURL* url = [[NSURL alloc] initWithString:path];
5054
NSError* error = nil;
5155
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];//使用本地URL创建
5256
if( error ) {
5357
NSLog(@"[LuaView][error]%@",error);
58+
}else{
59+
self.fileName = fileName;
5460
}
5561
}
56-
if( self.playing ) {
62+
if( !self.playing ) {
5763
[self play];
5864
}
5965
}
@@ -94,25 +100,26 @@ - (id) lv_nativeObject{
94100
#pragma -mark AudioPlayer
95101

96102
static int lvNewAudioPlayer (lua_State *L) {
103+
104+
Class c = [LVUtil upvalueClass:L defaultClass:[LVAudioPlayer class]];
105+
106+
LVAudioPlayer* player = [[c alloc] init:L];
107+
LuaViewCore* lview = LV_LUASTATE_VIEW(L);
108+
97109
if( lua_gettop(L)>=1 ) {
98-
Class c = [LVUtil upvalueClass:L defaultClass:[LVAudioPlayer class]];
99-
100-
LVAudioPlayer* player = [[c alloc] init:L];
101-
LuaViewCore* lview = LV_LUASTATE_VIEW(L);
102110
NSString* fileName = lv_paramString(L, 1);
103111
[player setPlayFileName:fileName bundle:lview.bundle];
112+
}
113+
114+
{
115+
NEW_USERDATA(userData, AudioPlayer);
116+
userData->object = CFBridgingRetain(player);
117+
player.lv_userData = userData;
104118

105-
{
106-
NEW_USERDATA(userData, AudioPlayer);
107-
userData->object = CFBridgingRetain(player);
108-
player.lv_userData = userData;
109-
110-
luaL_getmetatable(L, META_TABLE_AudioPlayer );
111-
lua_setmetatable(L, -2);
112-
}
113-
return 1;
119+
luaL_getmetatable(L, META_TABLE_AudioPlayer );
120+
lua_setmetatable(L, -2);
114121
}
115-
return 0;
122+
return 1;
116123
}
117124

118125
static int play (lua_State *L) {
@@ -121,6 +128,19 @@ static int play (lua_State *L) {
121128
if( user && LVIsType(user, AudioPlayer) ){
122129
LVAudioPlayer* player = (__bridge LVAudioPlayer *)(user->object);
123130
if( player ){
131+
132+
if( lua_gettop(L)>=2 ) {
133+
NSString* fileName = lv_paramString(L, 2);
134+
135+
if (![player.fileName isEqualToString:fileName]){
136+
if (player.playing){
137+
[player stop];
138+
}
139+
LuaViewCore* lview = LV_LUASTATE_VIEW(L);
140+
[player setPlayFileName:fileName bundle:lview.bundle];
141+
}
142+
}
143+
124144
[player play];
125145
lua_pushvalue(L,1);
126146
return 1;

0 commit comments

Comments
 (0)