-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a new handler for dealing download mp3 from remote server.
- Loading branch information
1 parent
d5cbe4d
commit 87867b6
Showing
6 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="weian.cheng.mediaplayerwithexoplayer"/> | ||
package="weian.cheng.mediaplayerwithexoplayer"> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | ||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | ||
</manifest> |
99 changes: 99 additions & 0 deletions
99
...layerwithexoplayer2/src/main/java/weian/cheng/mediaplayerwithexoplayer/DownloadHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package weian.cheng.mediaplayerwithexoplayer | ||
|
||
import android.util.Log | ||
import java.io.BufferedInputStream | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
class DownloadHandler(private val url: String, filePath: String, private val eventListener: ExoPlayerEventListener.PlayerEventListener? = null) : Thread() { | ||
private val tag = "DownloadHandler" | ||
private val bufferSize = 2048 | ||
private val tempTrackPath = "/storage/emulated/0/Download/temp_track.mp3" | ||
private var totalSize: Int = -1 | ||
private var path: String = "" | ||
private var file: File? = null | ||
private var listener: ExoPlayerEventListener.PlayerEventListener? = null | ||
|
||
init { | ||
when (filePath.isEmpty()) { | ||
true -> path = tempTrackPath | ||
false -> path = filePath | ||
} | ||
listener = eventListener | ||
} | ||
|
||
override fun run() { | ||
super.run() | ||
Runnable { | ||
if (!isRemoteAvailable()) { | ||
listener?.onDownloadTrack(false) | ||
Thread.currentThread().interrupt() | ||
} | ||
|
||
if (!createFile()) { | ||
listener?.onDownloadTrack(false) | ||
Thread.currentThread().interrupt() | ||
} | ||
|
||
if (!downloadFile()) { | ||
listener?.onDownloadTrack(false) | ||
Thread.currentThread().interrupt() | ||
} else { | ||
listener?.onDownloadTrack(true) | ||
} | ||
|
||
}.run() | ||
} | ||
|
||
private fun isRemoteAvailable(): Boolean { | ||
var result = false | ||
Log.i(tag, "url is $url") | ||
val httpURLConnection = URL(url).openConnection() as HttpURLConnection | ||
HttpURLConnection.setFollowRedirects(false) | ||
httpURLConnection.connect() | ||
httpURLConnection.requestMethod = "GET" | ||
if (httpURLConnection.responseCode == HttpURLConnection.HTTP_OK) | ||
result = true | ||
|
||
totalSize = httpURLConnection.contentLength | ||
httpURLConnection.disconnect() | ||
return result | ||
} | ||
|
||
private fun createFile(): Boolean { | ||
file = File(path) | ||
file?.takeIf { it.exists() }.let { | ||
it?.createNewFile() | ||
} | ||
|
||
return true | ||
} | ||
|
||
private fun downloadFile(): Boolean { | ||
val fileOutputStream = FileOutputStream(file) | ||
val inputStream = BufferedInputStream(URL(url).openStream()) | ||
|
||
var downloadSize = 0 | ||
|
||
val buffer = ByteArray(bufferSize) | ||
var bufferLength: Int | ||
while (true) { | ||
bufferLength = inputStream.read(buffer) | ||
if (bufferLength <= 0) | ||
break | ||
fileOutputStream.write(buffer, 0, bufferLength) | ||
downloadSize += bufferLength | ||
} | ||
|
||
fileOutputStream.flush() | ||
fileOutputStream.close() | ||
inputStream.close() | ||
if (downloadSize != totalSize) { | ||
Log.e(tag, "file was not complete") | ||
return false | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters