teodorpatras / Jukebox

Player for streaming local and remote audio files. Written in Swift.
MIT License
551 stars 122 forks source link

Play Document directory Song #49

Open hirenspaceo opened 7 years ago

hirenspaceo commented 7 years ago

Hello,

I want to play song from document directory folder. Because in my application i give both support online play and offline play. i set document directory path as URL but it not load assets,

RajChanchal commented 7 years ago

How are you making local file url? Use this code to make the url:

let mp3FileName = "david.mp3"
var fileManager = NSFileManager.defaultManager()
var wayToFile = fileManager .URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var passMusicFileURL: NSURL?
if let documentPath: NSURL = wayToFile.first as? NSURL {
     let musicFile = documentPath.URLByAppendingPathComponent(mp3FileName )
     println(musicFile)
     passMusicFileURL = musicFile
     let filePathURL:URL! = URL.init(fileURLWithPath: filePth, isDirectory: false)
     if (filePathURL != nil){
            let item = JukeboxItem.init(URL: filePathURL, localTitle: "Song Title")
            jukeboxPlaylist.add(item)
     }
 }
hirenspaceo commented 7 years ago

Thanks a lot @RajChanchal.

This code solve my problem. Thank you very much

kodekarim commented 7 years ago

Getting fatal error on xcode 8.2 while playing it from document directory any suggestion

        let mp3FileName = "introduction.mp3"
        let fileManager = FileManager.default
        let wayToFile = fileManager .urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
        var passMusicFileURL: NSURL?
        if let documentPath: NSURL = wayToFile.first as NSURL? {
            let musicFile = documentPath.appendingPathComponent(mp3FileName )
            print(musicFile!)
            passMusicFileURL = musicFile! as NSURL
            let filePathURL:URL! = URL.init(fileURLWithPath: path, isDirectory: false)
            if (filePathURL != nil){
                let item = JukeboxItem.init(URL: filePathURL, localTitle: "Song Title")
                //jukeboxPlaylist.add(item)
                jukebox.append(item: item, loadingAssets: true)
               // jukebox.play()
            }
RajChanchal commented 7 years ago

@abdulKarim002 kindly check if musicFile is not nil

karthickDarin commented 5 years ago

musicfile i am getting like the file:///Users/im038/Library/Developer/CoreSimulator/Devices/5E2B9097-576A-4636-B397-0283EC29D72C/data/Containers/Data/Application/7C9BE57E-BF20-4485-B4A0-8165FCB3A963/Documents/01-PURUSHARTHA.mp3

but my application is crashing at this point

    let mp3FileName = indexData["mp3Url"] as? String

    let fileManager = FileManager.default
    let wayToFile = fileManager.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
    var passMusicFileURL: NSURL?
    if let documentPath: NSURL = wayToFile.first as NSURL? {
        let musicFile = documentPath.appendingPathComponent(mp3FileName!)
        print(musicFile!)
        passMusicFileURL = musicFile as NSURL?
        let filePathURL:URL! = URL.init(fileURLWithPath: "\(String(describing: passMusicFileURL!))", isDirectory: false)
        if (filePathURL != nil){
            let item = JukeboxItem.init(URL: filePathURL!, localTitle: "Song Title")
            jukebox.append(item: item, loadingAssets: true) --- fatel error 
          //  jukebox.play()
        }
    }