mp3实验.docx

上传人:b****7 文档编号:25703435 上传时间:2023-06-11 格式:DOCX 页数:41 大小:397.78KB
下载 相关 举报
mp3实验.docx_第1页
第1页 / 共41页
mp3实验.docx_第2页
第2页 / 共41页
mp3实验.docx_第3页
第3页 / 共41页
mp3实验.docx_第4页
第4页 / 共41页
mp3实验.docx_第5页
第5页 / 共41页
点击查看更多>>
下载资源
资源描述

mp3实验.docx

《mp3实验.docx》由会员分享,可在线阅读,更多相关《mp3实验.docx(41页珍藏版)》请在冰豆网上搜索。

mp3实验.docx

mp3实验

Mp3实验报告

一、实验目的

1、掌握本地服务的管理方法;

2、掌握服务的隐式启动和显式启动方法;

3、掌握远程服务的绑定和调用方法;

4、了解AIDL语言的用途和语法。

二、实验内容

开发简易音乐播放程序,实现音乐播放、暂停、切换等基本功能。

要求分别以启动方式和绑定方式管理后台服务。

三、安卓应用程序开发环境

安装配置了安卓组件的windows操作系统

JDKEclipseAndriodAndriodSDKADTApachTomcat

四、需求分析

读取远程服务器当中的XML文件

五、实验过程及调试

1、将项目直接运行在手机上

Target选择Alwaysprompttopickdevices,表示运行项目时会弹出真机提示框“Androiddevicechoonser”

2、运行项目弹出设备选择窗口:

这里选择真机“huawei-y511_u00” 点击“OK”开始启动运行

3、很抱歉,程序已停止运行。

错误提示INSTALL_FAILED_SHARED_USER_INCOMPATIBLE:

可能原因:

apk的AndroidManifest.xml中声明了android:

sharedUserId="android.uid.system",但没有相应的签名。

Installation error:

 INSTALL_FAILED_SHARED_USER_INCOMPATIBLE

Please check logcat output for more details.

Launch canceled!

解决办法:

1、找到编译目标系统时的签名证书platform.pk8和platform.x509.pem,在android源码目录build\target\product\security下;

2、将android:

sharedUserId="android.uid.system"先注释起来。

4、应用程序未运行,提示:

FailedtoinstallFloatActivity.apkondevice'20120208':

timeout

Launchcanceled!

 

解决办法:

模拟器(或真机)超时,重启模拟器,如果是真机则先断开数据线再重新连接。

5、应用程序运行错误,提示:

可能原因:

AndroidManifest.xml配置文件配置信息错误。

6、如何读取Android模拟器中的文件?

选中文件,点击下图中第一个图标,将Android模拟器中的文件复制到其它地方就可以打开了,注意:

Android模拟器中的sdcard在mnt目录下:

 

 

7、很抱歉,程序已停止运行。

错误提示:

Causedby:

可能原因:

尝试在一个Service中放置一个对话框。

解决办法:

将此对话框设置为系统级提示框(即全局性质的提示框):

mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

相应的需要在Android配置文件中添加权限声明:

name="android.permission.SYSTEM_ALERT_WINDOW"/>

8、无法运行程序,提示.ConnectException:

Connectionrefused:

connect.

可能原因:

模拟器端的ADB出问题了。

解决办法:

打开CMD,输入adbkill-server,再输入adbdevices如下,提示ADBserverdidn'tACK:

9、在AndroidManifest.xml中添加某些权限时错误,提示Permissionisonlygrantedtosystemapp.

解决办法:

点击Window→Preferences→Android→LintErrorChecking.更改ID=ProtectedPermission项的Security。

 

六、实验小结

根据理论知识对所得到的实验数据或结果进行解释、分析。

对实验结果所作的一般性的判断、归纳、概括,实验的心得体会、建议等。

后台服务是通过启动方式或者绑定方式开启的。

在启动方式中,启动Service的组件不能够获取Service的对象实例

而在绑定方式中,可以调用Service中实现的函数。

七、实验代码

FileUtils.java

packagemars.download;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.util.ArrayList;

importjava.util.List;

importmars.model.Mp3Info;

importandroid.os.Environment;

publicclassFileUtils{

privateStringSDCardRoot;

publicFileUtils(){

//得到当前外部储存设备的目录,File.separator是文件分隔符,比如在window下是"\"

SDCardRoot=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;

}

/**

*在SD卡上创建文件

*@paramfileName

*@return

*@throwsIOException

*/

publicFilecreateSDFile(StringfileName,Stringdir)throwsIOException{

Filefile=newFile(SDCardRoot+dir+File.separator+fileName);

System.out.println("file-->"+file);

file.createNewFile();

returnfile;

}

/**

*在SD卡上创建目录

*/

publicFilecreateSDDir(Stringdir){

FiledirFile=newFile(SDCardRoot+dir+File.separator);

System.out.println("createdir"+dirFile.mkdir());

returndirFile;

}

/**

*判断SD卡上的文件夹是否存在

*/

publicbooleanisFileExist(StringfileName,Stringpath){

Filefile=newFile(SDCardRoot+path+File.separator+fileName);

returnfile.exists();

}

/**

*将一个InputSream里面的数据写入到SD卡上

*/

publicFilewrite2SDFromInput(Stringpath,StringfileName,InputStreaminput){

Filefile=null;

OutputStreamoutput=null;

try{

createSDDir(path);

file=createSDFile(fileName,path);

output=newFileOutputStream(file);

bytebuffer[]=newbyte[4*1024];

inttemp;

while((temp=(input.read(buffer)))!

=-1){

output.write(buffer,0,temp);

}

output.flush();

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

output.close();

}catch(Exceptione){

e.printStackTrace();

}

}

returnfile;

}

/**

*读取目录中mp3文件的名字和大小+lrc文件

*/

publicListgetMp3Files(Stringpath){

Listmp3Infos=newArrayList();

Filefile=newFile(SDCardRoot+path+File.separator);

File[]files=file.listFiles();

for(inti=0;i

if(files[i].getName().endsWith("mp3")){

Mp3Infomp3Info=newMp3Info();

mp3Info.setMp3Name(files[i].getName());

mp3Info.setMp3Size(files[i].length()+"");

Stringlrcname[]=files[i].getName().split("\\.");

Stringlrc=lrcname[0]+".lrc";

if(isFileExist(lrc,"/mp3")){

mp3Info.setLrcName(lrc);

}

mp3Infos.add(mp3Info);

}

}

returnmp3Infos;

}

}

HttpDownloader.java

packagemars.download;

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

import.HttpURLConnection;

import.MalformedURLException;

import.URL;

publicclassHttpDownloader{

privateURLurl=null;

/**

*根据URL下载文件,前提是这个文件是文本的,函数的返回值就是文件当中的内容

*1.创建一个URL对象

*2.通过URL对象,创建一个HttpURLConnection对象

*3.得到InputStream

*4.从InputStream得到数据

*/

publicStringdownload(StringurlStr){

StringBuffersb=newStringBuffer();

Stringline=null;

BufferedReaderbuffer=null;

try{

//创建一个url连接

url=newURL(urlStr);

//创建一个http连接

HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();

//使用IO读取数据

buffer=newBufferedReader(newInputStreamReader(urlConn.getInputStream()));

while((line=buffer.readLine())!

=null){

sb.append(line);

}

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

buffer.close();

}catch(Exceptione){

e.printStackTrace();

}

}

returnsb.toString();

}

/**

*该函数返回整形,-1代表下载文件出错,0代表下载文件成功,1代表文件已经存在

*/

publicintdownFile(StringurlStr,Stringpath,StringfileName){

InputStreaminputStream=null;

try{

FileUtilsfu=newFileUtils();

if(fu.isFileExist(fileName,path)){

return1;

}else{

inputStream=getInputStreamFromUrl(urlStr);

System.out.println("test");

FileresultFile=fu.write2SDFromInput(path,fileName,inputStream);

if(resultFile==null){

return-1;

}

}

}catch(Exceptione){

e.printStackTrace();

}

return0;

}

/**

*根据URL得到输入流

*/

publicInputStreamgetInputStreamFromUrl(StringurlStr)throwsMalformedURLException,IOException{

url=newURL(urlStr);

HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();

InputStreaminputStream=urlConn.getInputStream();

returninputStream;

}

}

LrcProcesor.java

packagemars.lrc;

importjava.io.BufferedReader;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.util.ArrayList;

importjava.util.LinkedList;

importjava.util.Queue;

importjava.util.regex.Matcher;

importjava.util.regex.Pattern;

publicclassLrcProcesor{

@SuppressWarnings("rawtypes")

publicArrayListprocess(InputStreaminputStream){

//存放时间点数据

QueuetimeMills=newLinkedList();

//存放时间点对应的歌词

Queuemessages=newLinkedList();

//将放时间和歌词存放到一起

ArrayListqueues=newArrayList();

try{

//创建BufferedReader对象

InputStreamReaderinputReader=newInputStreamReader(inputStream,"GBK");

BufferedReaderbufferedReader=newBufferedReader(inputReader);

Stringtemp=null;

@SuppressWarnings("unused")

inti=0;

//创建一个正则表达式

Patternp=Ppile("\\[([^\\]]+)\\]");

Stringresult=null;

booleanb=true;

while((temp=bufferedReader.readLine())!

=null){

i++;

Matcherm=p.matcher(temp);

if(m.find()){

if(result!

=null){

messages.add(result);

}

StringtimeStr=m.group();

LongtimeMill=time2Long(timeStr.substring(1,timeStr.length()-1));

if(b){

timeMills.offer(timeMill);

}

Stringmsg=temp.substring(10);

result=""+msg+"\n";

}else{

result=result+temp+"\n";

}

}

messages.add(result);

queues.add(timeMills);

queues.add(messages);

}catch(Exceptione){

e.printStackTrace();

}

returnqueues;

}

/**

*将分钟,秒全部转化为毫秒

*@paramtimeStr

*@return

*/

publicLongtime2Long(StringtimeStr){

Strings[]=timeStr.split(":

");

intmin=Integer.parseInt(s[0]);

Stringss[]=s[1].split("\\.");

intsec=Integer.parseInt(ss[0]);

intmill=Integer.parseInt(ss[1]);

returnmin*60*1000+sec*1000+mill*10L;

}

}

Mp3Info.java

packagemars.model;

importjava.io.Serializable;

publicclassMp3InfoimplementsSerializable{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

privateStringid;

privateStringmp3Name;

privateStringmp3Size;

privateStringlrcName;

privateStringlrcSize;

publicStringgetId(){

returnid;

}

publicvoidsetId(Stringid){

this.id=id;

}

publicStringgetMp3Name(){

returnmp3Name;

}

publicvoidsetMp3Name(Stringmp3Name){

this.mp3Name=mp3Name;

}

publicMp3Info(){

super();

}

publicStringgetMp3Size(){

returnmp3Size;

}

publicvoidsetMp3Size(Stringmp3Size){

this.mp3Size=mp3Size;

}

publicStringgetLrcName(){

returnlrcName;

}

publicvoidsetLrcName(StringlrcName){

this.lrcName=lrcName;

}

publicStringgetLrcSize(){

returnlrcSize;

}

publicvoidsetLrcSize(StringlrcSize){

this.lrcSize=lrcSize;

}

@Override

publicStringtoString(){

return"Mp3Info[id="+id+",mp3Name="+mp3Name+",mp3Size="

+mp3Size+",lrcName="+lrcName+",lrcSize="+lrcSize

+",toString()="+super.toString()+"]";

}

publicMp3Info(Stringid,Stringmp3Name,Stringmp3Size,StringlrcName,

StringlrcSize){

super();

this.id=id;

this.mp3Name=mp3Name;

this.mp3Size=mp3Size;

this.lrcName=lrcName;

this.lrcSize=lrcSize;

}

}

AppConstant.java

packagemars.mp3player;

publicinterface

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 从业资格考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1