alexiscn / SynologyKit

Synology File Station SDK for Swift
MIT License
36 stars 8 forks source link

play an mp3 file #12

Closed gameo1 closed 4 years ago

gameo1 commented 4 years ago

Hi, If you have a mp3 file with its path, how would you play it? I mean, what URL/path would you use for it? simply the filename path?

alexiscn commented 4 years ago

In I my own app.. I downloaded the music from NAS and played locally

gameo1 commented 4 years ago

Do you think there might be a way to stream audio instead of downloading it?

alexiscn commented 4 years ago

Maybe this libray cloud be helpful, StreamingKit. SynologyKit is just a wrapper of FileStation API, and will not support streaming audio or movie.

gameo1 commented 4 years ago

Maybe this libray cloud be helpful, StreamingKit. SynologyKit is just a wrapper of FileStation API, and will not support streaming audio or movie.

this is fine. Thanks for the hint!! My question was: with your wonderful kit, I can navigate through the filestation and get the relative path of a track, like "/home/music/folder/file.mp3". I would be able to stream if I would get the absolute path or http path like "http://mynas/home/music/folder/file.mp3" instead of downloading it. is this doable?

alexiscn commented 4 years ago

I do not know. Synology FileStation API document does not offer an api to do that.

gameo1 commented 4 years ago

done with your kit. Dirty way but works. somewhere where the AVPlayer is

  func getFileUrl(path:String) -> URL  {
        let url = client?.getMP3StreamURL(path)
        return url ?? URL.init(string: "nil")!
  }

in the Synology class

/// Get MP3 Sream for a given file
/// - Parameter path: path of the file to stream.

public func getMP3StreamURL(_ path: String) -> URL? {
    var params: Parameters = [:]
    params["path"] = path
    var request = SynologyBasicRequest(baseURLString: baseURLString(), api: .mp3stream, method: .stream, params: params)
    request.version = 2
    request.path = SynologyCGI.mp3
    let str = requestToUrl(request: request, path: path)
    let url = URL.init(string: str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
    return url
}

///Helper
func requestToUrl(request: SynologyBasicRequest, path: String) -> String {
    var str = request.baseURLString + "webapi/"
    str += request.path + "?method=" + request.method.rawValue + "&path=" + path + "&api=SYNO.AudioPlayer.Stream&version=2"
    return str
}

Thanks a lot for your work. Happy Xmas