Xcode 6-SWIFT-Cast CMTime as Floating

var songs = MPMediaQuery() var localSongs = songs.items songList = NSMutableArray(array: localSongs) tableView.reloadData() var song = MPMediaItem(coder: songList[0] as NSCoder) var currentItem = AVPlayerItem(URL: song.valueForProperty(MPMediaItemPropertyAssetURL) as NSURL) player.replaceCurrentItemWithPlayerItem(currentItem) player.play() var songTitle: AnyObject! = song.valueForProperty(MPMediaItemPropertyTitle) songName.text = songTitle as? String sliderOutlet.value = Float(player.currentTime()) // <<-Error here 

I create a music player and I want the slider to show the duration of the song, but I get this error

Could not find an overload for "init" that takes the supplied arguments

I think the problem is converting CMTime to Float.

+6
source share
2 answers

CMTime is a structure containing a value , timescale and other fields, so you cannot just "distinguish" it to a floating point value.

Fortunately, there is a CMTimeGetSeconds() conversion function:

 let cmTime = player.currentTime() let floatTime = Float(CMTimeGetSeconds(player.currentTime())) 

Update: with Swift 3, player.currentTime returns TimeInterval , which is a type alias for Double . Therefore, conversion to Float simplified to

 let floatTime = Float(player.currentTime) 
+15
source

CMTime is a structure containing value, time frames, flags, and era. That way you can't just "distinguish" it to a floating point value.

You can use this value by writing directly

 sliderOutlet.value = Float(player.currentTime().value) 

But this will only give the value of the player, which is in milliseconds. To get the value in seconds, you use this:

 sliderOutlet.value = Float(CMTimeGetSeconds(player.currentTime())) 

Remember that this will also not be the correct way that you should have the value of your slider.

0
source

Source: https://habr.com/ru/post/975432/


All Articles