PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/helper/M3UWriter.kt

89 lines
3.3 KiB
Kotlin
Raw Normal View History

2020-06-09 18:04:22 +00:00
/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
2020-06-09 18:04:22 +00:00
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
2020-06-09 18:04:22 +00:00
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
2020-10-06 08:46:04 +00:00
*
2020-06-09 18:04:22 +00:00
*/
package code.name.monkey.retromusic.helper
2020-09-05 18:51:44 +00:00
import code.name.monkey.retromusic.db.PlaylistWithSongs
import code.name.monkey.retromusic.db.toSongs
2020-06-09 18:04:22 +00:00
import code.name.monkey.retromusic.model.Playlist
2020-09-05 18:51:44 +00:00
import code.name.monkey.retromusic.model.Song
2021-11-17 11:22:39 +00:00
import java.io.*
2020-06-09 18:04:22 +00:00
object M3UWriter : M3UConstants {
@JvmStatic
@Throws(IOException::class)
fun write(
dir: File,
playlist: Playlist
2021-11-27 08:36:49 +00:00
): File {
2020-06-09 18:04:22 +00:00
if (!dir.exists()) dir.mkdirs()
val file = File(dir, playlist.name + "." + M3UConstants.EXTENSION)
val songs = playlist.getSongs()
2020-08-21 18:01:52 +00:00
if (songs.isNotEmpty()) {
2020-06-09 18:04:22 +00:00
val bw = BufferedWriter(FileWriter(file))
bw.write(M3UConstants.HEADER)
for (song in songs) {
bw.newLine()
bw.write(M3UConstants.ENTRY + song.duration + M3UConstants.DURATION_SEPARATOR + song.artistName + " - " + song.title)
bw.newLine()
bw.write(song.data)
}
bw.close()
}
return file
}
2020-09-05 18:51:44 +00:00
@JvmStatic
@Throws(IOException::class)
fun writeIO(dir: File, playlistWithSongs: PlaylistWithSongs): File {
if (!dir.exists()) dir.mkdirs()
val fileName = "${playlistWithSongs.playlistEntity.playlistName}.${M3UConstants.EXTENSION}"
val file = File(dir, fileName)
2021-11-12 15:44:18 +00:00
val songs: List<Song> = playlistWithSongs.songs.sortedBy {
it.songPrimaryKey
}.toSongs()
2020-09-05 18:51:44 +00:00
if (songs.isNotEmpty()) {
val bufferedWriter = BufferedWriter(FileWriter(file))
bufferedWriter.write(M3UConstants.HEADER)
songs.forEach {
bufferedWriter.newLine()
bufferedWriter.write(M3UConstants.ENTRY + it.duration + M3UConstants.DURATION_SEPARATOR + it.artistName + " - " + it.title)
bufferedWriter.newLine()
bufferedWriter.write(it.data)
}
bufferedWriter.close()
}
return file
}
2021-11-17 11:22:39 +00:00
fun writeIO(outputStream: OutputStream, playlistWithSongs: PlaylistWithSongs) {
val songs: List<Song> = playlistWithSongs.songs.sortedBy {
it.songPrimaryKey
}.toSongs()
if (songs.isNotEmpty()) {
val bufferedWriter = outputStream.bufferedWriter()
bufferedWriter.write(M3UConstants.HEADER)
songs.forEach {
bufferedWriter.newLine()
bufferedWriter.write(M3UConstants.ENTRY + it.duration + M3UConstants.DURATION_SEPARATOR + it.artistName + " - " + it.title)
bufferedWriter.newLine()
bufferedWriter.write(it.data)
}
bufferedWriter.close()
}
outputStream.flush()
outputStream.close()
}
2020-10-06 08:46:04 +00:00
}