Search code examples
iosswiftvideophasset

How to access the video in iPhone synched through iTunes from PC


I'm trying to access the video in my iPhone which is imported from my PC through iTunes but I cannot.

I tried the following code but it doen't work.

let fetchOptions = PHFetchOptions()
fetchOptions.includeAssetSourceTypes = .TypeiTunesSynced
let assets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Video, options: fetchOptions)

I also tested the following code. I can access the videos in Camera roll, but not the video synched through iTunes from PC.

let fetchOptions = PHFetchOptions()
let assets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Video, options: fetchOptions)

I'd really appreciate if you can give me some advice.

UPDATE The following code works. Thanks for your advice!

import MediaPlayer

and

let mediaTypeNumber = MPMediaType.AnyVideo.rawValue
let query = MPMediaQuery()
query.addFilterPredicate(MPMediaPropertyPredicate(value: mediaTypeNumber, forProperty: MPMediaItemPropertyMediaType))

let items: [MPMediaItem] = query.items! as [MPMediaItem]
for item in items {
    // If you want to get title.
    let title = item.valueForProperty(MPMediaItemPropertyTitle) as! String
}

Solution

  • You can't access those videos using PhotoKit. PhotoKit only allows access to media stored in the central Photo Library - in short: everything that is shown in the Photos App.

    Videos synced by iTunes are part of the Music(iPod)-Library. You can access these through the MPMediaLibrary:

    https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMediaLibrary_ClassReference/index.html#//apple_ref/occ/cl/MPMediaLibrary