我收藏了許多影片,做了一個windows的程式,建資料庫對這些影片建標簽進行管理,可按影員、影片型別等標簽進行索引,一直想把這個程式遷移到電視機頂盒里。前一段時間對android進行了一次密集攻關,包括SMB的訪問如何傳遞用戶名與密碼、得到SMB后如何播放兩個關鍵點的技術。
一、先說第一個技術要點,得益于本論壇的無私幫助,順利攻克。話不多說,直接講用法,貼原始碼。
首先,在工程的libs檔案夾中放入jcifs-1.3.14.jar檔案,然后在build path中引入這個jar。
在工程中新建一個類,暫且命名為smbServer用于開啟子執行緒,連接SMB:
public class smbServer {
public NtlmPasswordAuthentication Authentication=null;
public void getSmbConnect(){
ExecutorService ser=Executors.newFixedThreadPool(1);
ConnectSmbThread smb=new ConnectSmbThread();
Future<NtlmPasswordAuthentication> result=ser.submit(smb);
try {
Authentication=result.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ser.shutdownNow();
}
private class ConnectSmbThread implements Callable<NtlmPasswordAuthentication> {
private String ip="192.168.1.101";//這個是SMB的IP地址
@Override
public NtlmPasswordAuthentication call() throws Exception {
// TODO Auto-generated method stub
UniAddress mDomain=null;
try {
mDomain = UniAddress.getByName(ip);
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
NtlmPasswordAuthentication mAuthentication = new NtlmPasswordAuthentication(ip, "用戶名","密碼");
if(mDomain!=null)
{
try {
SmbSession.logon(mDomain, mAuthentication);
} catch (SmbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}else{
return null;
}
return mAuthentication;
}
public String getSMBFiles(String smbPath){
SmbFile smbFolder=new SmbFile(smbPath,mAuthentication);
try {
SmbFile files = smbFolder.listFiles();
String strFiles=new String;
for(int i=0;i<files.length;i++){
strFiles=files.getName();
}
}catch (SmbException e) {
}
return strFiles;
}
}
在MainActivity中建立一個smbServer物件:
smbServer smb=new smbServer();
smb.getSmbConnect();
String files[]=smb.getSMBFiles("192.168.1.101/");
二、得到SMB檔案后如何簡單實作播放,其間經歷的程序大費周章,論壇里有說要轉成Http流的,有說什么都不用轉直接傳遞的,但卻沒有提供實作方法,也有顯得特別高深的,說是要開啟流媒體服務在后臺快取,然后還提供了源代碼。進去一瞧,屁用沒有,純粹是騙幣的。
我的最終實作方法其實特別簡單,一句話:拿來主義,能用就是好的。
先貼上原始碼:
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(smbPath), "video/*");
startActivity(intent);
如果SMB設定了密碼,用下面一行代碼
smbPath=smbPath.replace("smb://", "smb://用戶名:密碼@");
比如你的SMB檔案是:smb://192.168.1.101/abc/efg.mpg
通過上同一行代碼就變成:smb://用戶名:密碼@192.168.1.101/abc/efg.mpg
隱式開啟Activity需要外部有App能夠回應,這個App就是VLC。安裝了VLC后這個功能就能實作。
但是在使用程序中發現有些能夠順利打開播放,有些會報錯。經過多次摸索,發現只要對VLC進行設定后,沒有再報錯。
VLC的設定方法:打開VLC,進入“設定”頁面,找到“高級”選項,在“網路快取”中設定數值為任意數值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/188239.html
標籤:Android
上一篇:android studio下提示gradle project sync failed.Basic functionality will not work解決
