测试软件
--0)添加下拉刷新控件 用于网络状态不好等情况刷新
--1)优化播放界面返回键事件
--2)优化列表长按与下载键点击事件
--为什么这么久都不更新呢?
--答案是懒
--andlua没有,就用setImageBitmap什么的
--导入Glide和ViewPager与SwipeRefreshLayout
local Glide = luajava.bindClass "com.bumptech.glide.Glide"
local ViewPager = luajava.bindClass "androidx.viewpager.widget.ViewPager"
local SwipeRefreshLayout = luajava.bindClass "androidx.swiperefreshlayout.widget.SwipeRefreshLayout"
--根据编辑器的不同,使用PageView和ViewPager
--使用fa2请导入以下类库
-----------------------------------
--[[
--导入所需库
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import {"android.widget.EditText",
"android.net.Uri",
"android.graphics.drawable.GradientDrawable",
"androidx.viewpager.widget.ViewPager",
"com.androlua.Http",
"com.bumptech.glide.Glide",
"androidx.appcompat.app.AlertDialog",
"android.app.AlertDialog",
"android.animation.ObjectAnimator",
"com.androlua.LuaBroadcastReceiver",
"android.widget.RelativeLayout",
"org.w3c.dom.Text",
"android.widget.FrameLayout",
"android.content.IntentFilter",
"android.view.KeyEvent",
"android.widget.Toast",
"android.content.Intent",
"android.view.View",
"android.graphics.Color",
"android.content.Context",
"android.widget.TextView",
"android.graphics.Typeface",
"android.view.Gravity",
"androidx.cardview.widget.CardView",
"android.graphics.ColorFilter",
"android.widget.ProgressBar",
"android.view.WindowManager",
"com.androlua.LuaAdapter",
"android.widget.LinearLayout",
"android.widget.ImageView",
"android.media.MediaPlayer",
"android.widget.ListView",
"android.app.DownloadManager",
"androidx.recyclerview.widget.RecyclerView$Orientation",
"androidx.viewpager2.widget.ViewPager2$Orientation",
"android.graphics.drawable.GradientDrawable$Orientation",
}
--]]
--使用AndLua+请导入以下类库
-----------------------------------
--[[
--导入所需库
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
activity.setTheme(R.Theme_Blue)
activity.setTitle("海纳云")
activity.ActionBar.hide()
import {"android.widget.LinearLayout",
"android.media.MediaPlayer",
"android.widget.ImageView",
"com.androlua.LuaAdapter",
"com.androlua.LuaBroadcastReceiver",
"android.widget.FrameLayout",
"android.graphics.Typeface",
"android.graphics.ColorFilter",
"android.widget.Toast",
"android.content.Intent",
"android.content.IntentFilter",
"android.app.DownloadManager",
"android.net.Uri",
"android.content.Context",
"org.w3c.dom.Text",
"android.R$id",
"android.view.KeyEvent",
"android.widget.CardView",
"android.animation.ObjectAnimator",
"com.androlua.Http",
"android.widget.ListView",
"android.view.WindowManager",
"android.app.AlertDialog",
"android.view.View",
"android.graphics.drawable.GradientDrawable",
"android.view.Gravity",
"android.widget.EditText",
"android.graphics.drawable.GradientDrawable$Orientation",
"android.widget.RelativeLayout",
"android.widget.TextView",
"android.widget.ProgressBar",
"com.nirenr.Color",
"android.graphics.Color"
}
--]]
--设置美化提示样式
---------------------------
function 美化提示(内容)
toasts={
LinearLayout;
{
CardView;
layout_width="88%w";
CardBackgroundColor="#7f000000";
layout_height="60dp";
layout_marginTop='6%w';--布局顶距
radius="10dp";
elevation="";
id="美化窗口",
{
LinearLayout,
orientation='horizontal',
layout_width='fill',
layout_height='fill',
{
CardView;
layout_gravity="center|left";
layout_width="40dp";
CardBackgroundColor="#00ffffff";
layout_height="40dp";
radius="20dp";
layout_marginLeft="20dp";
elevation="";
id="提示图片动画载体";
{
ImageView;--个人中心
layout_gravity="center";
layout_width="fill";
layout_height="fill";
scaleType="fitXY";
--src="icon/icon_recent_sysmsg.png";
src="https://pan.tenire.com/view.php/1a4aa2c5be8a274d8c897a3cbbd8d1dc.png";
};
};
{
TextView;
textColor="#ffffffff";
layout_gravity="center";
textSize="13dp";
layout_marginLeft="15dp";
id="提醒文字";
};
};
};
};
local toast=Toast.makeText(activity,"内容",Toast.LENGTH_LONG).setGravity(Gravity.TOP, 0, 0).setView(loadlayout(toasts))
提醒文字.Text=tostring(内容)
toast.show()
提醒文字.getPaint().setFakeBoldText(true)
end
--设置弹出消息样式
----------------------------
function Toasts(content)
Toast_layout={
LinearLayout,
orientation="vertical",
layout_width="fill",
layout_height="fill",
{
CardView,
layout_width="wrap",
layout_height="wrap",
CardBackgroundColor="#00000000",
cardElevation="0dp",
radius="19dp",
{
TextView,
layout_width="wrap",
layout_height="wrap",
background="#cc000000",
padding="8dp",
textSize="15sp",
TextColor="#ffeeeeee",
gravity="center",
id="wenzi",
},
},
}
local toast=Toast.makeText(activity,"文本",Toast.LENGTH_SHORT).setView(loadlayout(Toast_layout))
toast.setGravity(Gravity.BOTTOM,0,200)
wenzi.Text=tostring(" "..content.." ")
toast.show()
end
--设置按钮动画
------------------------------
function Animation(view,time)
import "android.animation.ObjectAnimator"
ObjectAnimator().ofFloat(view,"scaleX",{1.2,.8,1.1,.9,1}).setDuration(time).start()
ObjectAnimator().ofFloat(view,"scaleY",{1.2,.8,1.1,.9,1}).setDuration(time).start()
end
--配置弹窗圆角
-------------------------
function DialogRadius(控件,背景色,上角度,下角度)
if not 上角度 then
if (activity.width <=720)then
上角度=18
else
上角度=28
end
end
if not 下角度 then
下角度=上角度
end
local GradientDrawable = luajava.bindClass "android.graphics.drawable.GradientDrawable"
控件.setBackgroundDrawable(GradientDrawable()
.setShape(GradientDrawable.RECTANGLE)
.setColor(背景色)
.setCornerRadii({上角度,上角度,上角度,上角度,下角度,下角度,下角度,下角度}))
end
--获取状态栏和导航栏高度
-----------------------------------------
function getStatusBarHeight()--函数
local resources=activity.getResources();
local resourceId=resources.getIdentifier("status_bar_height","dimen","android");
local height=resources.getDimensionPixelSize(resourceId);
return height;
end
--配置波纹函数
------------------
function 波纹函数(id,颜色)
local ColorStateList = luajava.bindClass "android.content.res.ColorStateList"
local attrsArray = {android.R.attr.selectableItemBackgroundBorderless}
local typedArray =activity.obtainStyledAttributes(attrsArray)
ripple=typedArray.getResourceId(0,0)
Pretend=activity.Resources.getDrawable(ripple)
Pretend.setColor(ColorStateList(int[0].class{int{}},int{颜色}))
id.setBackground(Pretend.setColor(ColorStateList(int[0].class{int{}},int{颜色})))
end
--设置隐藏状态栏但不隐藏字体
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
--状态栏背景透明
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
--正文内容开始
--------------------
Tritium=--全屏框架
{
LinearLayout;--线性控件
orientation='vertical';--布局方向
layout_width='fill';--布局宽度
layout_height='fill';--布局高度
background='#FFFFFFFF';--布局背景
{
LinearLayout;
layout_height="match_parent";
gravity="top|center";
layout_width="fill";
orientation="vertical";
{
ViewPager;--代码手册与fa2请使用
--PageView;--其他平台请使用
layout_height="fill";
layout_width="fill";
layout_gravity='top|center';--在父控件中的对齐方式
layout_weight="1";
id="pagev";
pages=
{
{
LinearLayout;
layout_height="fill";
layout_width="fill";
orientation="vertical";
{
CardView;--卡片控件
layout_height=getStatusBarHeight();--设置卡片控件高度为状态栏高度
backgroundColor=0xFFFFFFFF;--卡片颜色
radius="0dp";--卡片圆角
CardElevation="0dp";--卡片阴影
layout_width="match_parent";--卡片宽度
};
{
LinearLayout,
-- layout_marginTop="4.5%h",
orientation="horizontal",
layout_width="fill",
layout_height="56dp",
gravity="left|center",
{
ImageView,
id="返回",
layout_width="7%w",
layout_height="7%h",
--src="icon/__ic_menu.png",
src="https://s1.ax1x.com/2023/01/08/pSV2kWD.png";
layout_marginLeft="3%w",
},
{
CardView,
layout_width="fill",
layout_height="fill",
layout_margin="0dp",
cardElevation="0dp",
cardBackgroundColor="#ffffff",
radius="0dp",
{
CardView,
layout_width="75%w",
layout_height="fill",
layout_margin="10dp",
cardElevation="0dp",
layout_marginLeft="1.8%w",
cardBackgroundColor="#FFF8F8F8",
radius="25dp",
FocusableInTouchMode=true;--禁止弹出输入法
{
EditText,
layout_marginLeft="3%w",
layout_width="70%w",
layout_gravity="left|center",
backgroundColor=0,
hint="请输入关键词搜索音乐...",
textSize="12sp",
singleLine=true;--设置单行输入,禁止换行
layout_marginBottom="-1.5dp",
id="edit",
},
{
TextView,
id="清空搜索框",
text="×",
textSize="25sp",
textColor="#FF888888",
layout_marginBottom="1dp",
layout_gravity="right|center",
layout_marginRight="5%w",
},
},
{
ImageView,
id="搜索",
layout_width="50dp",
layout_marginBottom="-1dp",
layout_height="20dp",
--src="icon/__ic_search.png",
src="https://s1.ax1x.com/2023/01/08/pSV2ESe.png";
layout_gravity="right|center",
colorFilter="#333333",
layout_marginRight="1%w",
onClick=function()
Animation(搜索,250)
if (edit.Text=="") then
美化提示("请输入所需搜索的关键词进行搜索")
else
搜索歌曲()
end
end,
};
};
};
{
LinearLayout;
layout_width="fill";
gravity="center";
id="加载";
background="#ffffff";
layout_height="fill";
{
ProgressBar;
};
};
{
SwipeRefreshLayout;
layout_height="fill",
layout_width="fill",
id="Pulling";
{
LinearLayout;
orientation="vertical";
layout_height="fill";
layout_width="fill";
{
ListView,
id="歌曲列表",
layout_height="-1",
layout_width="-1",
DividerHeight=1;
BackgroundColor="#FFFFFFFF";
verticalScrollBarEnabled=false,--隐藏纵向滑条
overScrollMode=View.OVER_SCROLL_NEVER,--隐藏圆弧阴影,
};
};
};
},
--音乐列表结束↑
--播放界面开始↓
{
LinearLayout;
layout_height="fill";
layout_width="fill";
orientation="vertical";
{
FrameLayout,
layout_width="match",
layout_height="match",
{
ImageView,
layout_width="150%h",
layout_height="150%h",
layout_gravity="center",
--src="/storage/emulated/0/AndLua/project/海纳云开发版/Images/login.png",
src="https://s1.ax1x.com/2023/01/08/pSVIOKO.jpg";
},
{
LinearLayout,
layout_width="match",
layout_height="match",
Orientation=1,
gravity="center",
{
CardView,
layout_width="80%w",
layout_height="80%w",
cardElevation="6dp",
radius="20dp",
layout_margin="24dp",
{
ImageView,
layout_width="match",
layout_height="match",
--src="/storage/emulated/0/Tencent/QQ_Images/-15bbf5683c5b8fa1.jpg",
src="";
id="img",
scaleType='fitXY';--图片拉伸
layout_gravity='center';--重力
},
},
{
TextView,
text="未播放音乐",
textColor="#ffffff",
Typeface=Typeface.DEFAULT_BOLD;
textSize="32dp",
gravity="center",
id="歌曲名称",
},
{
TextView,
text="暂无歌手",
textColor="#757575",
layout_margin="10dp",
textSize="18dp",
gravity="center",
id="歌手名称",
},
{
CardView,
layout_width="70dp",
layout_height="70dp",
cardElevation="0dp",
radius="20dp",
layout_margin="50dp",
cardBackgroundColor=0xFF2196F3,
id="play",
{
ImageView,
layout_width="match",
layout_height="wrap",
padding="20dp",
ColorFilter="#ffffff",
id="buttonimg",
src="https://img.icons8.com/material-outlined/550/play--v1.png",
};
};
{
SeekBar,
layout_width='match',
id='seek_bar',
paddingLeft="10%w";
paddingRight="10%w";
};
{
RelativeLayout;
layout_width="fill";
layout_marginTop="5%w";
paddingLeft="10%w";
paddingRight="10%w";
{
LinearLayout,
gravity="center",
layout_alignParentLeft="true";
layout_centerVertical="true";
{
TextView;
id="time1";
text="00:00";
textColor=0xFFFFFFFF,
--Typeface=Typeface.createFromFile(File(activity.getLuaDir().."/font/zt1.ttf"));
layout_marginRight="5dp",
};
{
TextView;
id="/";
text="/";
--Typeface=Typeface.createFromFile(File(activity.getLuaDir().."/font/zt1.ttf"));
textColor=0xFFFFFFFF,
layout_marginRight="5dp",
};
{
TextView;
id="time2";
text="00:00";
--Typeface=Typeface.createFromFile(File(activity.getLuaDir().."/font/zt1.ttf"));
textColor=0xFFFFFFFF,
};
};
{
ImageView,
layout_alignParentRight="true";
layout_centerVertical="true";
layout_width="6%w",
layout_height="6%w",
ColorFilter=0xFFFFFFFF,
id="download",
--src="icon/ic_file_download_black_24dp.png",
src="https://s1.ax1x.com/2023/01/11/pSmzR2T.png",
};
};
};
};
},
--播放界面结束↑
--功能配置开始↓
};
};
};
}
activity.setContentView(loadlayout(Tritium))
--主界面布局表结束↑
--搜索歌曲配置开始↓
--定义一张歌曲列表布局表
--------------------------
歌曲list=
{
LinearLayout;
layout_height="-1";
layout_width="-1";
orientation="vertical";
BackgroundColor=0,
{
LinearLayout;
layout_width="-1";
layout_height="65dp";
{
CardView;
CardElevation="0dp";
CardBackgroundColor=0,
Radius="5dp";
layout_width="-1";
layout_height="-2";
layout_margin="8dp";
{
CardView;
CardElevation="0dp";
CardBackgroundColor=0,
-- Radius=dp2px(8)-2;
layout_margin="0px";
layout_width="-1";
layout_height="-1";
{
LinearLayout;
layout_width="-1";
layout_height="-1";
BackgroundColor=0,--背景色
-- id="音乐_波纹";
{
RelativeLayout;
layout_width="match_parent";
layout_height="match_parent";
{
LinearLayout;
layout_height="match_parent";
padding="5dp";
gravity="center";
id="布局_图片";
layout_width="50dp";
{
CardView;
layout_height="-1";
radius="5dp";
CardBackgroundColor=0,
layout_width="50dp";
{
ImageView;
--src="icon.png";
layout_height="match_parent";
id="图片";
layout_width="match_parent";
scaleType="fitXY";--设置图片缩放显示
};
};
};
{
LinearLayout;
layout_height="match_parent";
layout_width="match_parent";
layout_toRightOf="布局_图片";
{
RelativeLayout;
layout_height="match_parent";
layout_width="match_parent";
layout_marginLeft="8dp";
{
TextView;
id="歌名";
layout_marginTop="3dp";
textColor="#FF000000";
text="歌名";
singleLine=true;
ellipsize="end";
maxLines=1;
};
{
TextView;
layout_below="歌名";
layout_marginTop="3dp";
id="歌手";
text="歌手";
singleLine=true;
ellipsize="end";
maxLines=1;
};
};
};
};
};
};
};
{
TextView;
layout_width="-2";
layout_height="-2";
id="链接";
visibility="gone";
};
};
};
--搜索歌曲定义规则
--------------------
local function search(keyword,props,callback)
local cjson,Http=require"cjson",luajava.bindClass "com.androlua.Http"
local url,data="http://music.163.com/api/search/get/web",{s=keyword,type="1"}
if props table.foreach(props,lambda k,v=>data[k]=v) end
Http.post(url,data,lambda code,data do
if (code==200) then
local t={}
local data=cjson.decode(data)
加载.setVisibility(View.GONE)--隐藏控件
Pulling.setRefreshing(false)--刷新完成 结束动画
for _,data in pairs(data.result.songs)
table.insert(t,{
data.artists[1].name,
data.name,
data.album.artist.img1v1Url,
tointeger(data.id)
})
end when callback callback(t)
end
end)
end
--发起歌曲搜索
---------------
function 搜索歌曲()
加载.setVisibility(View.VISIBLE)--显示控件
search(edit.Text,{limit="100"},function(data)
--获取单曲信息(作者,歌曲名,封面图,歌曲id)
adp=LuaAdapter(activity,歌曲list)
歌曲列表.setAdapter(adp)--列表
for i=1,#data do
local author,name,img,id=table.unpack(data[i])
adp.add{歌名=name,歌手=author,图片=img,链接=id}--设置内容
end
end)
end
--加载初始歌曲列表
-------------------
function 初始化数据()
Http.get("https://api.uomg.com/api/rand.music?sort=热歌榜&format=json",nil,nil,nil,function(code,content)
if (code==200) and content then
--1)导入cjson库
local cjson=require("cjson")
--2)将JSON转换成table类型
JSON=cjson.decode(content)
--3)从table里面获取需要的数据
local name=JSON.data.name
--4)设置结果
search(name,{limit="100"},function(data)
--获取单曲信息(作者,歌曲名,封面图,歌曲id)
adp=LuaAdapter(activity,歌曲list)
歌曲列表.setAdapter(adp)--列表
for i=1,#data do
local author,name,img,id=table.unpack(data[i])
adp.add{歌名=name,歌手=author,图片=img,链接=id}--设置内容
end
end)
else
Toasts("推荐获取失败 但您可以正常搜索")
end
end)
end
--定义下拉刷新(用于刷新搜索和刷新推荐列表)
------------------------------------------
Pulling.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener{
onRefresh=function()
--判断搜索框是否有文字或者文字是否超出搜索限制
if (edit.Text == "") or (#edit.Text >150) then
初始化数据()--刷新推荐列表
else
搜索歌曲()--发起搜索 刷新数据
end
end
})
--回车键监听(用于搜索)
---------------------
import "java.awt.event.*"
edit.setOnKeyListener(
{
onKey=function(v,keyCode,event)
if (KeyEvent.KEYCODE_ENTER == keyCode and KeyEvent.ACTION_DOWN == event.getAction()) then
if (edit.Text == "") or (#edit.Text >150) then
Toasts("加载错误 请正确输入关键词")
else
搜索歌曲()--发起搜索
end
end
end
})
--输入框内容监听(用于配置清空按钮)
--------------------------------
清空搜索框.setVisibility(View.GONE)
edit.addTextChangedListener
{
onTextChanged=function(s)
if (edit.Text=="")then
清空搜索框.setVisibility(View.GONE)
else
清空搜索框.setVisibility(View.VISIBLE)
end
end
}
--初始化
local ti
local mediaPlayer
--配置销毁定时器
-------------------
local function releaseTicker()
if ti then
ti.stop()
ti=nil
end
end
--定义列表点击事件
--------------------
歌曲列表.onItemClick=function(a,pos,c,d)--列表单击
local 歌曲名=(pos.Tag.歌名.text)--获取文本
local 歌手名=(pos.Tag.歌手.text)
local 音乐ID=(pos.Tag.链接.text)
local URL = luajava.bindClass "java.net.URL"
local url=URL("http://music.163.com/song/media/outer/url?id="..音乐ID..".mp3")
Thread(Runnable{
run=function()
if (url.openConnection().contentLength==0)then
this.runOnUiThread(Runnable{
run=function()
Toasts("暂不可播放")
end
})
else
this.runOnUiThread(Runnable{
run=function()
--配置播放器数据
歌曲名称.setText(歌曲名)
歌手名称.setText(歌手名)
网易链接="http://music.163.com/song/media/outer/url?id="..音乐ID..".mp3"
Toasts("左右滑动 切换播放器和列表")--提示消息
pagev.setCurrentItem(1)--跳转到播放器界面
task(200,function()--200毫秒
if (Boole==true)then
初始化图片()
--停止播放
mediaPlayer.stop()
import "android.media.MediaPlayer"
mediaPlayer = MediaPlayer()
mediaPlayer.reset()--初始化参数
--设置播放资源
mediaPlayer.setDataSource(网易链接)
mediaPlayer.prepare()
else
初始化图片()
import "android.media.MediaPlayer"
mediaPlayer = MediaPlayer()
mediaPlayer.reset()--初始化参数
--设置播放资源
mediaPlayer.setDataSource(网易链接)
mediaPlayer.prepare()
end
--时间换算
local function ms2minsec(t)
local s=t/1000
local sec=tointeger(s%60)
if sec<10 then
sec="0"..sec
end
local min=tointeger(s//60)
if min<10 then
min="0"..min
end
return min..":"..sec
end
--创建定时器
local function setTicker ()
if not ti then
ti=Ticker()
ti.Period=500
ti.onTick=function()
if mediaPlayer then
local currentposition=mediaPlayer.getCurrentPosition()
seek_bar.setProgress(currentposition)
if (mediaPlayer.isPlaying()==true) then
local imgurl="https://img.icons8.com/material-outlined/550/pause--v1.png"
Glide.with(activity).load(imgurl).into(buttonimg)
Boole=true
else
local imgurl="https://img.icons8.com/material-outlined/550/play--v1.png"
Glide.with(activity).load(imgurl).into(buttonimg)
Boole=false
end
else
releaseTicker()
end
end
--启动Ticker定时器
ti.start()
end
end
--缓冲完成的监听
mediaPlayer.setOnPreparedListener(MediaPlayer.OnPreparedListener{
onPrepared=function(mediaPlayer)
setTicker ()
seek_bar.setEnabled(true)
local videoduration=mediaPlayer.Duration
seek_bar.setMax(videoduration)
mediaPlayer.start()
end
} )
--播放完成回调
mediaPlayer.setOnCompletionListener(MediaPlayer.OnCompletionListener{
onCompletion=function(mediaPlayer)
seek_bar.setEnabled(false)
end
})
--进度跳转完成监听
mediaPlayer.setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener{
onSeekComplete=function(mediaPlayer)
end
})
--网络流媒体的缓冲变化时回调
mediaPlayer.setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener{
onBufferingUpdate=function(mediaPlayer,BufferPercent)
seek_bar.setSecondaryProgress(BufferPercent/100*mediaPlayer.Duration)
end
})
--缓存
mediaPlayer.setOnInfoListener(MediaPlayer.OnInfoListener{
onInfo=function(mediaPlayer,what,extra)
if what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START then
elseif what == MediaPlayer.MEDIA_INFO_BUFFERING_START then
local imgurl="https://img.icons8.com/material-outlined/550/play--v1.png"
Glide.with(activity).load(imgurl).into(buttonimg)
elseif what == MediaPlayer.MEDIA_INFO_BUFFERING_END then
local imgurl="https://img.icons8.com/material-outlined/550/pause--v1.png"
Glide.with(activity).load(imgurl).into(buttonimg)
end
end
})
--错误回调
mediaPlayer.setOnErrorListener(MediaPlayer.OnErrorListener{
onError=function(mediaPlayer,what,extra)
if what == MediaPlayer.MEDIA_ERROR_UNKNOWN then
Toasts("网络连接失败,请重新尝试!")
else
if what == MediaPlayer.MEDIA_ERROR_SERVER_DIED then
Toasts("网络连接失败,请重新尝试!")
end
end
end
})
--拖动条监听
seek_bar.setOnSeekBarChangeListener{
onStartTrackingTouch=function()
--开始拖动
if mediaPlayer.isPlaying()
releaseTicker()
end
end,
onStopTrackingTouch=function(v)
--停止拖动
if mediaPlayer.isPlaying()
mediaPlayer.seekTo(v.getProgress())
setTicker ()
end
end,
onProgressChanged=function(SeekBar,progress)
--状态改变
time1.text=ms2minsec(progress)
time2.text=ms2minsec(SeekBar.Max)
end
}
--end
end)
end
})
end
end
}).start()
end
--定义列表长按事件
--------------------
歌曲列表.onItemLongClick=function(a,pos,c,d)--列表单击
local 名=(pos.Tag.歌名.text)--获取文本
local 手=(pos.Tag.歌手.text)
local 音乐ID=(pos.Tag.链接.text)
local 链="http://music.163.com/song/media/outer/url?id="..音乐ID..".mp3"
local URL = luajava.bindClass "java.net.URL"
local url=URL(链)
local ProgressDialog = luajava.bindClass "android.app.ProgressDialog"
local dialogs= ProgressDialog(this)
dialogs.setProgressStyle(ProgressDialog.STYLE_SPINNER)
dialogs.setMessage([[正在解析下载中 请稍候...
若长时间无响应请返回重试.]])
dialogs.setCancelable(true)
dialogs.setCanceledOnTouchOutside(false)
dialogs.show()
Thread(Runnable{
run=function()
if (url.openConnection().contentLength==0)then
this.runOnUiThread(Runnable{
run=function()
dialogs.dismiss()
Toasts("暂不可下载")
end
})
else
local c=url.openConnection().contentLength
local 体积=string.format("%0.2f",c/1024/1024).."MB"
this.runOnUiThread(Runnable{
run=function()
local Tritium_Download=
{
LinearLayout;--线性布局
orientation="vertical";--重力属性
id="Download_father_Tritium",--控件ID
{
TextView;--文本控件
id="linkhint",--控件ID
layout_marginTop="25dp";--布局方向
text="下载链接",--文本内容
layout_width="80%w";--布局宽度
textColor=WidgetColors,--控件ID
layout_gravity="center";--对齐方式
};
{
EditText;--编辑框控件
layout_width="80%w";
layout_gravity="center";
id="链接";--控件ID
text=链,--文本内容
};
{
TextView;--文本控件
layout_marginTop="10dp";--控件顶距
text="音乐名称",--文本内容
layout_width="80%w";--布局宽度
textColor=WidgetColors,--字体颜色
layout_gravity="center";--对齐方式
};
{
EditText;--编辑框控件
layout_width="80%w";--布局宽度
layout_gravity="center";--对齐方式
id="名称";--控件ID
text=手.."-"..名,--文本内容
};
{
TextView;--文本控件",
text="下载路径",--文本内容
layout_width="80%w";--布局宽度
textColor=WidgetColors,--字体颜色
layout_marginTop="10dp";--控件顶距
layout_gravity="center";--对齐方式
};
{
EditText;--编辑框控件
id="pathedit",--控件ID
layout_width="80%w";--布局宽度
layout_gravity="center";--对齐方式
Text="Download/";--文本内容
id="路径";--控件ID
};
};
--弹出下载窗口
local dialog=AlertDialog.Builder(this)--弹窗
--.setTitle("提示信息")--设置标题
.setView(loadlayout(Tritium_Download))--设置布局
.setPositiveButton("下载("..体积..")",
{
onClick=function(v)--积极按钮点击事件
美化提示("正在下载 打开通知栏查看下载进度")
local Uri = luajava.bindClass "android.net.Uri"
local Context = luajava.bindClass "android.content.Context"
local IntentFilter = luajava.bindClass "android.content.IntentFilter"
local DownloadManager = luajava.bindClass "android.app.DownloadManager"
local LuaBroadcastReceiver = luajava.bindClass "com.androlua.LuaBroadcastReceiver"
local downloadManager=activity.getSystemService(Context.DOWNLOAD_SERVICE);
local url=Uri.parse(链接.Text);
local request=DownloadManager.Request(url);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setDestinationInExternalPublicDir(路径.Text,""..名称.Text..".mp3");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Download_ID=downloadManager.enqueue(request)
local intentFilter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
local broadcastReceiver = LuaBroadcastReceiver(LuaBroadcastReceiver.OnReceiveListener{
onReceive=function(context,intent)
local ID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
if (ID == Download_ID) then
Toasts("文件已保存于:"..路径.Text..名称.Text.."mp3")--提示下载
end
end
})
activity.registerReceiver(broadcastReceiver,intentFilter)
end
})--设置积极按钮
.setNeutralButton("系统播放",
{
onClick=function(v)--调用系统播放器播放音频
local Uri = luajava.bindClass "android.net.Uri"
local Intent = luajava.bindClass "android.content.Intent"
intent =Intent(Intent.ACTION_VIEW)
uri = Uri.parse(链接.Text)
intent.setDataAndType(uri,"audio/*")
this.startActivity(intent)
end
})
.setNegativeButton("取消下载",nil)--设置消极按钮
.show()--显示弹窗
dialogs.dismiss()
dialog.create()
--调用弹窗圆角
DialogRadius(dialog.getWindow(),0xffffffff)
p=dialog.getWindow().getAttributes()
if (activity.width >1080)then
p.width=(900);
else
p.width=(activity.width*0.85);
end
dialog.getWindow().setAttributes(p);
end
})
end
end
}).start()
return true
end
--定义清空按钮点击事件
---------------------
清空搜索框.onClick=function()
Animation(清空搜索框,150)
edit.setText("")
end
--返回图片点击事件
---------------------
返回.onClick=function()--返回图片点击事件
Animation(返回,250)
activity.finish()--关闭当前页面
end
--搜索歌曲配置开结束↑
--音乐播放器配置结束↓
--修改SeekBar颜色
-----------------------
local PorterDuffColorFilter = luajava.bindClass "android.graphics.PorterDuffColorFilter"
local PorterDuff = luajava.bindClass "android.graphics.PorterDuff"
--修改SeekBar滑条颜色
seek_bar.ProgressDrawable.setColorFilter(PorterDuffColorFilter(0xFFFFFFFF,PorterDuff.Mode.SRC_ATOP))
--修改SeekBar滑块颜色
seek_bar.Thumb.setColorFilter(PorterDuffColorFilter(0xFF2196F3,PorterDuff.Mode.SRC_ATOP))
--定义配置专辑图片方法
---------------------
function 初始化图片()
local 选择=math.random(1,2)--随机数获取专辑图片
if(选择==1) then--路径一
ID="动漫男"
elseif (选择==2) then--路径二
ID="动漫女"
end
--由于网易云官方接口专辑图片异常
--从其他路径获取随机图片
Http.get("https://api.uomg.com/api/rand.avatar?sort="..ID.."&format=json",nil,nil,nil,function(code,content)
if (code==200) and content then
--1)导入cjson库
local cjson=require("cjson")
--2)将JSON转换成table类型
JSON=cjson.decode(content)
--3)从table里面获取需要的数据
local imgurl=JSON.imgurl
--4)设置结果到图片控件
Glide.with(activity).load(imgurl).into(img)
end
end)
end
--定义播放按键点击事件
----------------------
play.onClick=function()
if 网易链接 then--判断音乐链接状态
if (mediaPlayer.isPlaying() == true) then--判断是否在播放
mediaPlayer.pause()--暂停播放音乐
--配置播放键图片
local imgurl="https://img.icons8.com/material-outlined/550/play--v1.png"
Glide.with(activity).load(imgurl).into(buttonimg)
else
mediaPlayer.start()--开始播放音乐
--配置播放键图片
local imgurl="https://img.icons8.com/material-outlined/550/pause--v1.png"
Glide.with(activity).load(imgurl).into(buttonimg)
end
else
--提示无音乐播放消息
Toasts("暂无音乐 请先选择需要播放的音乐")
end
end
--配置图片保存方法
-------------------
function 保存图片(name,Bitmaps)
local FileOutputStream = luajava.bindClass "java.io.FileOutputStream"
local System = luajava.bindClass "java.lang.System"
--创建多级文件夹保存文件
File("/storage/emulated/0/Pictures/").mkdirs()
if Bitmaps then
out = FileOutputStream(tostring(File(name)))
Bitmaps.compress(Bitmap.CompressFormat.PNG,90, out)
out.flush()
out.close()
--提示保存完成消息
Toasts("保存成功 保存路径:"..name)
--通过广播通知相册更新
local Uri = luajava.bindClass "android.net.Uri"
local Intent = luajava.bindClass "android.content.Intent"
activity.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"..name)))
return true
else
--提示保存失败消息
Toasts("保存失败 请等待图片加载完成后保存")
return false
end
end
--配置专辑图片长按事件
----------------------
img.onLongClick=function()
--获取图片Bitmap对象
local Bitmap = luajava.bindClass "android.graphics.Bitmap"
Bitmaps=img.getDrawable().getBitmap()
--配置图片保存路径
local name="/storage/emulated/0/Pictures/"..System.currentTimeMillis()..".png"
--执行方法保存图片
保存图片(name,Bitmaps);
return true
end
--配置停止播放
-------------------
function onStop()
if(type(mediaPlayer)~="nil")then
mediaPlayer.stop()--停止播放
end
end
function onDestroy()--退出时结束
if ti then
ti.stop()
end
end
--配置页面滑动监听以配置状态栏样式
--------------------------------
pagev.setOnPageChangeListener(ViewPager.OnPageChangeListener
{
onPageSelected=function(v)
if v==0 then page=0
--状态栏字体黑
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
end
if v==1 then page=1
--状态栏字体白
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
end
end;
})
--重写返回键事件
local ostime=0--初始化参数
function onKeyDown(code,event)--返回键监听
if string.find(tostring(event),"KEYCODE_BACK") ~= nil then
if page==1 then
pagev.setCurrentItem(0)--跳转到列表界面
else
if ostime+2 > tonumber(os.time()) then
activity.finish()--结束活动
else
Toasts("再按一次返回键退出")--提示返回
ostime=tonumber(os.time())--复写参数
end
end
return true
end
end
--配置下载图片点击事件
----------------------
download.onClick=function()
Animation(download,250)
if 网易链接 then--判断音乐链接状态
local URL = luajava.bindClass "java.net.URL"
local url=URL(网易链接)
local ProgressDialog = luajava.bindClass "android.app.ProgressDialog"
local dialogs= ProgressDialog(this)
dialogs.setProgressStyle(ProgressDialog.STYLE_SPINNER)
dialogs.setMessage([[正在解析下载中 请稍候...
若长时间无响应请返回重试.]])
dialogs.setCancelable(true)
dialogs.setCanceledOnTouchOutside(false)
dialogs.show()
Thread(Runnable{
run=function()
if (url.openConnection().contentLength==0)then
this.runOnUiThread(Runnable{
run=function()
dialogs.dismiss()
Toasts("获取失败 音乐文件状态异常")
end
})
else
local c=url.openConnection().contentLength
local 体积=string.format("%0.2f",c/1024/1024).."MB"
this.runOnUiThread(Runnable{
run=function()
local Tritium_Download=
{
LinearLayout;--线性布局
orientation="vertical";--重力属性
id="Download_father_Tritium",--控件ID
{
TextView;--文本控件
id="linkhint",--控件ID
layout_marginTop="25dp";--布局方向
text="下载链接",--文本内容
layout_width="80%w";--布局宽度
textColor=WidgetColors,--控件ID
layout_gravity="center";--对齐方式
};
{
EditText;--编辑框控件
layout_width="80%w";
layout_gravity="center";
id="链接";--控件ID
text=网易链接,--文本内容
};
{
TextView;--文本控件
layout_marginTop="10dp";--控件顶距
text="音乐名称",--文本内容
layout_width="80%w";--布局宽度
textColor=WidgetColors,--字体颜色
layout_gravity="center";--对齐方式
};
{
EditText;--编辑框控件
layout_width="80%w";--布局宽度
layout_gravity="center";--对齐方式
id="名称";--控件ID
text=歌手名称.Text.."-"..歌曲名称.Text,--文本内容
};
{
TextView;--文本控件",
text="下载路径",--文本内容
layout_width="80%w";--布局宽度
textColor=WidgetColors,--字体颜色
layout_marginTop="10dp";--控件顶距
layout_gravity="center";--对齐方式
};
{
EditText;--编辑框控件
id="pathedit",--控件ID
layout_width="80%w";--布局宽度
layout_gravity="center";--对齐方式
Text="Download/";--文本内容
id="路径";--控件ID
};
};
--弹出下载窗口
local dialog=AlertDialog.Builder(this)--弹窗
--.setTitle("提示信息")--设置标题
.setView(loadlayout(Tritium_Download))--设置布局
.setPositiveButton("下载("..体积..")",
{
onClick=function(v)--积极按钮点击事件
美化提示("正在下载 打开通知栏查看下载进度")
local Uri = luajava.bindClass "android.net.Uri"
local Context = luajava.bindClass "android.content.Context"
local IntentFilter = luajava.bindClass "android.content.IntentFilter"
local DownloadManager = luajava.bindClass "android.app.DownloadManager"
local LuaBroadcastReceiver = luajava.bindClass "com.androlua.LuaBroadcastReceiver"
local downloadManager=activity.getSystemService(Context.DOWNLOAD_SERVICE);
local url=Uri.parse(链接.Text);
local request=DownloadManager.Request(url);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setDestinationInExternalPublicDir(路径.Text,名称.Text..".mp3");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Download_ID=downloadManager.enqueue(request)
local intentFilter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
local broadcastReceiver = LuaBroadcastReceiver(LuaBroadcastReceiver.OnReceiveListener{
onReceive=function(context,intent)
local ID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
if (ID == Download_ID) then
Toasts("文件已保存于:"..路径.Text..名称.Text.."mp3")--提示下载
end
end
})
activity.registerReceiver(broadcastReceiver,intentFilter)
end
})--设置积极按钮
.setNeutralButton("系统播放",
{
onClick=function(v)--调用系统播放器播放音频
local Uri = luajava.bindClass "android.net.Uri"
local Intent = luajava.bindClass "android.content.Intent"
intent =Intent(Intent.ACTION_VIEW)
uri = Uri.parse(链接.Text)
intent.setDataAndType(uri,"audio/*")
this.startActivity(intent)
end
})
.setNegativeButton("取消下载",nil)--设置消极按钮
.show()--显示弹窗
dialogs.dismiss()
dialog.create()
--调用弹窗圆角
DialogRadius(dialog.getWindow(),0xffffffff)
p=dialog.getWindow().getAttributes()
if (activity.width >1080)then
p.width=(900);
else
p.width=(activity.width*0.85);
end
dialog.getWindow().setAttributes(p);
end
})
end
end
}).start()
else
--提示无音乐下载消息
Toasts("暂无音乐 请先选择需要播放与下载的音乐")
end
end
初始化数据()
初始化图片()
波纹函数(buttonimg,0xFFFFFFFF)