2

had to edit this because I wrote it in a confused way.

I'm looking to create an Array, where each entry can be used to replace a function's parameters like this:

  func playMe(inputfile: String, inputtype: String)

with

  playMe(videoArray[0])

and that would then replace

  inputfile: String, inputtype: String

with

  inputfile: "file", inputtype: "mp4"

How would I go about creating it? I've tried

   var videoArray = [""Nameofvideo", inputtype: "mp4"", ""Nameofvideo2", inputtype: "mp4""] 

but it simply does not want to work. What am I missing? Am I trying to do this in a too complex fashion? I was hoping to emerge with

   playMe(videoArray[1])

and then follow it with audioArray[1] for the followup mp3. Hmm?

Here is my playMe code:

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "itemDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
    }

    func playMe(inputfile: String, inputtype: String) {
        let path = NSBundle.mainBundle().pathForResource(inputfile, ofType:inputtype)!
        let videoURL = NSURL(fileURLWithPath: path)
        let playerItem = AVPlayerItem(URL: videoURL)
        let player = AVPlayer(playerItem: playerItem)
        playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        player.play()
    }

    func itemDidFinishPlaying(notification: NSNotification) {
        playerLayer.removeFromSuperlayer()
    }
6
  • 1
    also considering the limited number of types you might want to create an enumeration with the player accepted types Commented Jan 1, 2016 at 20:27
  • 1
    You should also post your method playMe code Commented Jan 1, 2016 at 20:31
  • 1
    @LeoDabus Added playMe code and the required notifiers to return back to menu after playback stops. Commented Jan 1, 2016 at 20:38
  • 1
    you are declaring your AVPlayer inside viewDidLoad method so it will go out of existence almost immediately. You have to declare the player out of that method, inside your view controller class Commented Jan 1, 2016 at 22:01
  • 1
    try to keep your code as readable as possible. Close your brackets the next line as the Swift convention Commented Jan 1, 2016 at 22:37

4 Answers 4

4

You can do this:

var videoArray2 = [
    ["name": "Nameofvideo", "inputType": "mp4"],
    ["name": "Nameofvideo1", "inputType": "mp4"],
    ["name": "Nameofvideo2", "inputType": "mp4"]
]

or better:

struct Video {
    let name: String
    let inputType: String
}

var videoArray = [
    Video(name: "Nameofvideo", inputType: "mp4"),
    Video(name: "Nameofvideo2", inputType: "mp4"),
    Video(name: "Nameofvideo3", inputType: "mp4"),
    Video(name: "Nameofvideo4", inputType: "mp4")
]

if you can, consider using an enum type for inputType. like this:

enum InputType: String {
    case MP4 = "mp4"
    case MP3 = "mp3"
    case MKV = "mkv"
}

struct Video {
    let name: String
    let inputType: InputType
}

var videoArray = [
    Video(name: "Nameofvideo",  inputType: .MP4),
    Video(name: "Nameofvideo2", inputType: .MP4),
    Video(name: "Nameofvideo3", inputType: .MP4),
    Video(name: "Nameofvideo4", inputType: .MKV)
]

to call playMe:inputtype: write this:

let video = videoArray[1]
playMe(video.name, inputtype: video.inputType.rawValue) {

or update your playMe method to:

func playMe(video: Video) {
    let path = NSBundle.mainBundle().pathForResource(video.name, ofType: video.inputType.rawValue)!
    ...
}

and call it like this:

self.playMe(videoArray[1])
Sign up to request clarification or add additional context in comments.

9 Comments

Very good. But VideoObject should be renamed as Video since a Struct is not an object ;) Anyway you have my upvote.
@DevranCosmoUenal: Another refinement: the properties in Video should be declared as let. Infact, IMHO, they are not supposed to change.
@appzYourLife I almost mentioned that
@DevranCosmoUenal make your enum InputType: String and give them a string value "mp4"
@appzYourLife: but this works: dropbox.com/s/z6tanip3umklbxv/… ? appzYourLife: but this works: dropbox.com/s/z6tanip3umklbxv/… ? LeoDabus: updated, thanks :)
|
2

To add Devran's answer, here is what you can do

enum MediaType: String {

    case mp4
    case mp3

    var description: String {
        return rawValue
    }
}

struct Media {

    var title: String
    var type: MediaType
}

let medias = [
    Media(title: "Game Of Thrones", type: .mp4),
    Media(title: "Harry Potter", type: .mp4),
    Media(title: "Coldplay Clocks", type: .mp3),
    Media(title: "In the End", type: .mp3),
]


func playMedia(media: Media) {

    if let path = NSBundle.mainBundle().pathForResource(media.title, ofType: media.type.description) {
        // play media at path
    }
}

12 Comments

But Media and MediaType are way better names :)
Thanks @DevranCosmoUenal
I would just change var mediaType: MediaType to var type: MediaType
besides naming you should use URLForResource(withExtension:) to get the fileURL instead of the filePath
Thanks @LeoDabus for suggestions. I have adhered some of the suggestion and removed the playMedia code as this might not be relevant for the quesion.
|
2

You can use an array of tuples to feed parameters as long as you match your function's call signature exactly:

let arrayOfMedia = 
[ 
 ( "Game Of Thrones", inputtype: ".mp4"),
 ( "Harry Potter",    inputtype: ".mp4"),
 ( "Coldplay Clocks", inputtype: ".mp3"),
 ( "In the End",      inputtype: ".mp3")     
]

playMe(arrayOfMedia[0])

if you want to be explicit for the first parameter name in your array of tuples, you need to change your function's signature to :

func playMe(inputfile inputfile: String, inputtype: String)

Comments

1

You can use an array of dictionaries:

 var videoArray = [["videoName":"Nameofvideo", "inputtype": "mp4"], ["videoName":"Nameofvideo2", "inputtype": "mp4"]]

eventually, instead of repeating "videoName" and "inputtype", you could define two constants like:

static  let VideoName = "videoName"
static  let InputType = "inputtype"

and use them to define and access values contained within dictionaries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.