Fakhar uz zaman
Fakhar uz zaman

Reputation: 341

Play audio file stored in google drive (remote URL) - Swift

I'm using AVPlayer to play audios which are stored in google drive. Problem is that as public link of files stored in google drive don't have extension mentioned, that's why AVPlayer doesn't play audio because cannot get its extension from URL. Following is code i'm using.

let url = URL(string: "https://drive.google.com/file/d/1n6GeWY11Txvou5K-Tl7Ju39fQsWQTDvR/view?usp=sharing")

        let playerItem:AVPlayerItem = AVPlayerItem(url: url!)

        player = AVPlayer(playerItem: playerItem)

        player!.play()

For example, in case of following url

https://drive.google.com/file/d/1n6GeWY11Txvou5K-Tl7Ju39fQsWQTDvR/view?usp=sharing

AVPlayer isn't playing it and showing it's duration to 0.

But when i play audio file like following

https://www2.cs.uic.edu/~i101/SoundFiles/BabyElephantWalk60.wav

it plays without any problem because of file extension available.

So, could someone please comment how to play audio when extension not available in remote URLs.

Thanks

Upvotes: 1

Views: 1312

Answers (1)

wades
wades

Reputation: 292

Just got to modify the URL, Use this:

https://drive.google.com/uc?export=open&id=1n6GeWY11Txvou5K-Tl7Ju39fQsWQTDvR

Here's what I did,

Sharing an audio file looks like this:

https://drive.google.com/file/d/XXXXXXXXXXXXXXXXXX/view?usp=sharing

XXXXXXXXXXXXXXXXXX is the file ID. You can access the direct file by getting rid of everything after the ID and replacing file/d/ with uc?export=open&id=

Keep in mind, if Google decide to change the way their file sharing works then this solution may break.

Upvotes: 3

Related Questions