#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Private NativeMe As JavaObject
Dim timer1 As Timer
End Sub
Sub Globals
Dim vv1 As VideoView
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
NativeMe.InitializeContext
End If
vv1.Initialize("vv1")
Activity.AddView(vv1,0,0,100%x,50%y)
Dim streamURL As String = "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi"
If File.Exists(File.DirRootExternal & "/b4arecordings", "rec.avi") Then
File.Delete(File.DirRootExternal & "/b4arecordings", "rec.avi")
End If
NativeMe.RunMethod("DownloadStream",Array(streamURL,"b4arecordings","rec.avi"))
timer1.Initialize("Timer1",4000)
timer1.Enabled = True
End Sub
Sub timer1_Tick
If vv1.IsPlaying = False Then
If File.Exists(File.DirRootExternal & "/b4arecordings", "rec.avi") Then
vv1.LoadVideo(File.DirRootExternal & "/b4arecordings", "rec.avi")
vv1.Play
End If
End If
timer1.Enabled = False
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
#If JAVA
//NOTE: this java code is not good - you need to use Async-method
import android.os.Environment;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public void DownloadStream(String strUrl, String folderName, String fileName) {
final int TIMEOUT_CONNECTION = 5000; //5sec
final int TIMEOUT_SOCKET = 30000; //30sec
final int BUFFER_SIZE = 1024 * 5; // 5MB
BufferedInputStream in = null;
FileOutputStream out = null;
try {
File dir = new File(Environment.getExternalStorageDirectory() + "/"
+ folderName);
if (dir.exists() == false) {
dir.mkdirs();
}
URL url = new URL(strUrl);
File file = new File(dir, fileName);
//Open a connection to that URL.
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);
// Define InputStreams to read from the URLConnection.
// uses 5KB download buffer
InputStream is = ucon.getInputStream();
in = new BufferedInputStream(is, BUFFER_SIZE);
out = new FileOutputStream(file);
byte[] buff = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
} catch (IOException ioe) {
// Handle the error
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
// Nothing you can do
}
}
if (out != null) {
try {
out.flush();
out.close();
} catch (Exception e) {
// Nothing you can do
}
}
}
BA.Log("Finished download task");
}
#End If