Skip to content

Commit b42c3a0

Browse files
committed
Fix problem with rename files
1 parent 0f4ccdc commit b42c3a0

File tree

6 files changed

+94
-43
lines changed

6 files changed

+94
-43
lines changed

AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.immortalplayer"
4-
android:versionCode="1"
5-
android:versionName="1.0" >
4+
android:versionCode="2"
5+
android:versionName="2.0" >
66

77
<uses-sdk
88
android:minSdkVersion="14"

gen/com/immortalplayer/R.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ public static final class menu {
3838
public static final int main=0x7f070000;
3939
}
4040
public static final class string {
41-
public static final int VideoView_error_button=0x7f050005;
42-
public static final int VideoView_error_text_invalid_progressive_playback=0x7f050003;
43-
public static final int VideoView_error_text_unknown=0x7f050004;
44-
public static final int action_settings=0x7f050002;
41+
public static final int VideoView_error_button=0x7f050004;
42+
public static final int VideoView_error_text_invalid_progressive_playback=0x7f050002;
43+
public static final int VideoView_error_text_unknown=0x7f050003;
44+
public static final int action_settings=0x7f050001;
4545
public static final int app_name=0x7f050000;
46-
public static final int hello_world=0x7f050001;
4746
}
4847
public static final class style {
4948
/**

src/com/immortalplayer/HttpGetProxy.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import java.net.ServerSocket;
88
import java.net.Socket;
99
import java.net.SocketAddress;
10-
import java.net.SocketException;
1110
import java.util.ArrayList;
1211

1312
import com.immortalplayer.HttpGetProxyUtils.ProxyRequest;
1413
import com.immortalplayer.HttpGetProxyUtils.ProxyResponse;
1514

15+
1616
import android.net.Uri;
1717
import android.util.Log;
1818

@@ -29,12 +29,13 @@ public class HttpGetProxy{
2929
private SocketAddress serverAddress;
3030
private ProxyResponse proxyResponse=null;
3131
private String mUrl;
32-
private String mMediaFilePath, newPath;
32+
private String mMediaFilePath, newPath, newPath1;
3333
private File file, file1;
3434
private Proxy proxy=null;
3535
ArrayList<range> ranges = new ArrayList<range>();
3636
private boolean startProxy, error=false;
3737
private Thread prox;
38+
3839
/**
3940
* Initialize the proxy server, and start the proxy server
4041
*/
@@ -69,29 +70,31 @@ public String getLocalURL() {
6970

7071
public void stopProxy ()
7172
{
72-
startProxy=false;
73+
startProxy=false;
7374
try {
74-
localServer.close();
75+
if (localServer!=null)
76+
{localServer.close();
77+
localServer=null;}
7578
} catch (IOException e) {
76-
// TODO Auto-generated catch block
7779
e.printStackTrace();
7880
}
7981
}
8082

81-
public void setPaths (String dirPath, String file5, String url, long MaxSize,int maxnum)
83+
public void setPaths (String dirPath, String file5, String url, int MaxSize,int maxnum)
8284
{
8385
new File(dirPath).mkdirs();
84-
Utils.asynRemoveBufferFile(dirPath, maxnum, MaxSize);
86+
long maxsize1=MaxSize*1024*1024;
87+
Utils.asynRemoveBufferFile(dirPath, maxnum, maxsize1);
8588
mUrl=url;
8689
mMediaFilePath = dirPath + "/" + file5;
87-
file = new File(dirPath + "/" + file5);
90+
file = new File(mMediaFilePath);
8891
file1 = new File(dirPath + "/-" + file5);
8992
error=false;
9093
}
9194

9295
public void startProxy()
9396
{
94-
startProxy=true;
97+
startProxy=true;
9598
Log.i(TAG, "1 start proxy");
9699
prox=new Thread() {
97100
public void run() {
@@ -151,7 +154,7 @@ public void run() {
151154
HttpGetProxyUtils utils = null;
152155
int bytes_read;
153156
byte[] file_buffer = new byte[1024];
154-
157+
File file2, file3;
155158
byte[] local_request = new byte[1024];
156159
byte[] remote_reply = new byte[1024 * 50];
157160
ProxyRequest request = null;
@@ -173,7 +176,7 @@ public void run() {
173176
serverAddress = new InetSocketAddress(remoteHost, HTTP_PORT);
174177
utils = new HttpGetProxyUtils(sckPlayer, serverAddress);
175178
isExists = file.exists();
176-
if (mMediaFilePath!=newPath) {error=false; urlsize=0; ranges.clear(); ranges.trimToSize(); newPath=mMediaFilePath;}
179+
if (mMediaFilePath!=newPath) {error=false; urlsize=0; ranges.clear(); ranges.trimToSize(); newPath=mMediaFilePath; newPath1=file1.getAbsolutePath();}
177180

178181
//Read from file
179182
if ((isExists) || ((file1.exists()) && (error==true)))
@@ -243,7 +246,7 @@ public void run() {
243246
}
244247
return;
245248
}
246-
error=false;
249+
error=false;
247250

248251
os = new RandomAccessFile(file1, "rwd");
249252
} else {// MediaPlayer's request is invalid
@@ -254,6 +257,9 @@ public void run() {
254257
// ------------------------------------------------------
255258
// The feedback network server sent to the MediaPlayer, network server -> Proxy -> MediaPlayer
256259
// ------------------------------------------------------
260+
261+
//sckServer.setSoTimeout(200); // this parameter is experimental
262+
//sckPlayer.setSoTimeout(200);
257263
while ((sckServer != null) &&
258264
((bytes_read = sckServer.getInputStream().read(remote_reply)) != -1) &&
259265
(sckPlayer.isClosed()==false))
@@ -279,7 +285,7 @@ public void run() {
279285

280286

281287
// Send the binary data
282-
if (proxyResponse._other != null) {
288+
if (proxyResponse._other != null) {
283289
utils.sendToMP(proxyResponse._other);
284290
os.seek(proxyResponse._currentPosition);
285291
os.write(proxyResponse._other, 0, proxyResponse._other.length);
@@ -298,8 +304,9 @@ public void run() {
298304
if (os != null)
299305
{
300306
os.close();
301-
302-
if (isExists==false) {
307+
file2=new File (newPath);
308+
file3=new File (newPath1);
309+
if (file2.exists()==false) {
303310
range r=new range();
304311
r.setstart(request._rangePosition);
305312
r.setend(proxyResponse._currentPosition);
@@ -317,7 +324,7 @@ public void run() {
317324
}
318325
}
319326
if (urlsize==(h-1)) {
320-
file1.renameTo(file);
327+
file3.renameTo(file2);
321328
}
322329
}
323330
}
@@ -328,4 +335,4 @@ public void run() {
328335
}
329336
}
330337
}
331-
}
338+
}

src/com/immortalplayer/HttpParser.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
public class HttpParser {
1818
final static public String TAG = "HttpParser";
1919
final static private String RANGE_PARAMS="Range: bytes=";
20-
final static private String RANGE_PARAMS_0="Range: bytes=0-";
2120
final static private String CONTENT_RANGE_PARAMS="Content-Range: bytes ";
21+
final static private String CONTENT_LENGTH_PARAMS="Content-Length: ";
2222
final static public String HTTP_BODY_END = "\r\n\r\n";
2323
final static public String HTTP_RESPONSE_BEGIN = "HTTP/";
2424
final static public String HTTP_DOCUMENT_BEGIN = "<html>";
2525
final static public String HTTP_REQUEST_BEGIN = "GET ";
2626
final static public String HTTP_REQUEST_LINE1_END = " HTTP/";
2727

28-
private static final int HEADER_BUFFER_LENGTH_MAX = 1024 * 50;
28+
private static final int HEADER_BUFFER_LENGTH_MAX = 1024 * 50;//like remote_reply in httpgetproxy
2929
private byte[] headerBuffer = new byte[HEADER_BUFFER_LENGTH_MAX];
3030
private int headerBufferLength=0;
3131

@@ -85,15 +85,20 @@ public ProxyRequest getProxyRequest(byte[] bodyBytes){
8585
else
8686
result._body = result._body.replace(":" + localPort, ":"+ remotePort);
8787
//Without Range then add the fill, easy to deal with later
88+
String rangePosition="0";
8889
if(result._body.contains(RANGE_PARAMS)==false)
89-
result._body = result._body.replace(HTTP_BODY_END,
90-
"\r\n"+RANGE_PARAMS_0+HTTP_BODY_END);
91-
Log.i(TAG, result._body);
92-
93-
//Get Ranage position
94-
String rangePosition=Utils.getSubString(result._body,RANGE_PARAMS,"-");
90+
{
91+
result._rangePosition=0;
92+
} else {
93+
rangePosition=Utils.getSubString(result._body,RANGE_PARAMS,"-");
94+
try {
9595
Log.i(TAG,"------->rangePosition:"+rangePosition);
9696
result._rangePosition = Integer.valueOf(rangePosition);
97+
} catch (Exception e) {
98+
result._rangePosition=0;
99+
e.printStackTrace();
100+
}}
101+
Log.i(TAG, result._body);
97102

98103
return result;
99104
}
@@ -127,15 +132,33 @@ public ProxyResponse getProxyResponse(byte[] source,int length)
127132
//Sample Content-Range: bytes 2267097-257405191/257405192
128133
try {
129134
// Get the starting position
130-
String currentPosition = Utils.getSubString(text,CONTENT_RANGE_PARAMS, "-");
131-
if (currentPosition.length()>0) {
132-
result._currentPosition = Integer.valueOf(currentPosition);
133-
134-
// Get final position
135-
String startStr = CONTENT_RANGE_PARAMS + currentPosition + "-";
136-
String duration = Utils.getSubString(text, startStr, "/");
137-
result._duration = Integer.valueOf(duration);}
138-
else {result._currentPosition=0; result._duration=0;}
135+
if(text.contains(CONTENT_RANGE_PARAMS)==false)
136+
{
137+
result._currentPosition=0;
138+
if(text.contains(CONTENT_LENGTH_PARAMS)==true) {
139+
String duration = Utils.getSubString(text, CONTENT_LENGTH_PARAMS, "\r\n");
140+
try {
141+
result._duration = Integer.valueOf(duration)-1;
142+
} catch (Exception e) {
143+
result._duration=0;
144+
e.printStackTrace();
145+
}
146+
} else {result._duration=0;}
147+
} else {
148+
String currentPosition=Utils.getSubString(text,CONTENT_RANGE_PARAMS, "-");
149+
try {
150+
result._currentPosition = Integer.valueOf(currentPosition);
151+
// Get final position
152+
String startStr = CONTENT_RANGE_PARAMS + currentPosition + "-";
153+
String duration = Utils.getSubString(text, startStr, "/");
154+
result._duration = Integer.valueOf(duration);
155+
} catch (Exception e) {
156+
result._currentPosition=0; result._duration=0;
157+
e.printStackTrace();
158+
}
159+
}
160+
161+
139162
} catch (Exception ex) {
140163
Log.e(TAG, Utils.getExceptionMessage(ex));
141164
}
@@ -146,7 +169,6 @@ private List<byte[]> getHttpBody(String beginStr,String endStr,byte[] source,int
146169
if((headerBufferLength+length)>=headerBuffer.length){
147170
clearHttpBody();
148171
}
149-
150172
System.arraycopy(source, 0, headerBuffer, headerBufferLength, length);
151173
headerBufferLength+=length;
152174

src/com/immortalplayer/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
6464

6565

6666
public static class PlaceholderFragment extends Fragment {
67-
static private final long BUFFER_SIZE= 700*1024*1024;
67+
static private final int BUFFER_SIZE= 700;
6868
private HttpGetProxy proxy;
6969
private String videoUrl ="http://master255.org/res/%d0%9a%d0%bb%d0%b8%d0%bf%d1%8b/D/DJ%20Snake%20&%20Lil%20Jon/DJ%20Snake%20&%20Lil%20Jon%20-%20Turn%20Down%20for%20What.mp4";
7070
private String file1="";

src/com/immortalplayer/player.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.media.MediaPlayer.OnCompletionListener;
99
import android.media.MediaPlayer.OnErrorListener;
1010
import android.media.MediaPlayer.OnInfoListener;
11+
import android.os.Environment;
1112
import android.util.AttributeSet;
1213
import android.util.Log;
1314
import android.view.KeyEvent;
@@ -17,7 +18,10 @@
1718
import android.view.accessibility.AccessibilityEvent;
1819
import android.view.accessibility.AccessibilityNodeInfo;
1920
import android.widget.MediaController;
21+
import android.widget.Toast;
2022
import android.widget.MediaController.MediaPlayerControl;
23+
24+
import java.io.File;
2125
import java.io.IOException;
2226

2327
/**
@@ -317,6 +321,25 @@ public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
317321
new MediaPlayer.OnBufferingUpdateListener() {
318322
public void onBufferingUpdate(MediaPlayer mp, int percent) {
319323
mCurrentBufferPercentage = percent;
324+
//Only for Demo
325+
if (mCurrentBufferPercentage==0) {
326+
327+
Toast toast = Toast.makeText(getContext(), "For save file to cache watch (or rewind forward) video fully.", Toast.LENGTH_LONG);
328+
toast.show();
329+
}
330+
if (mCurrentBufferPercentage==100) {
331+
release();
332+
Toast toast = Toast.makeText(getContext(), "File save to sdcard. Start play video from file.", Toast.LENGTH_LONG);
333+
toast.show();
334+
335+
String path1=Environment.getExternalStorageDirectory()
336+
.getAbsolutePath() + "/ProxyBuffer/DJ Snake & Lil Jon - Turn Down for What.mp4";
337+
if (new File (path1).exists()==true) {
338+
path=path1;
339+
}
340+
openVideo();
341+
342+
}//
320343
if (mCurrentState == STATE_ERROR) {
321344
mSeekWhenPrepared=mp.getCurrentPosition()+1000;
322345
if (skip==mSeekWhenPrepared) {mSeekWhenPrepared=mSeekWhenPrepared+skip1;

0 commit comments

Comments
 (0)