0

I am trying to play a video inside a subclass of JSQMessagesViewController, but it doesn't start playing onClick event. I am downloading the video from firebase, and then I write the data on the device by appending a unique reference -LK7WSGtGAQ2anpDxkCQ. Please see below print out statement for the complete path.

  //response to collection view tap events
   //called every time we type on a message
   override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
    let message = messages[indexPath.item]

    print("message type is \(message.type) and message media uid is \(message.mediaMessage?.mediaUID)")

    if message.type == MessageType.image {
      let jsqMessage = jsqMessages[indexPath.item]
      let mediaItem = jsqMessage.media as! JSQPhotoMediaItem
      let photos = IDMPhoto.photos(withImages: [mediaItem.image])
      let browser = IDMPhotoBrowser(photos: photos)
      self.present(browser!, animated: true, completion: nil)
}

 if message.type == MessageType.video {

    let jsqMessage = jsqMessages[indexPath.item]
        if jsqMessage.isMediaMessage {

            if let mediaItem = jsqMessage.media as? JSQVideoMediaItem {

                print("mediaItem.fileURL is \(mediaItem.fileURL)")
               let player = AVPlayer(url: mediaItem.fileURL)
                let playerViewController = AVPlayerViewController()
                playerViewController.player = player
                self.present(playerViewController, animated: true) {
                    playerViewController.player!.play()
                }
            }
        }
    }

  }
}//end of extension

mediaItem.fileURL is Optional(file:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ)

Update
The video can by played now after appending .MOV to the url above, but there is no sound when it's played. How to fix this?

bibscy
  • 2,598
  • 4
  • 34
  • 82
  • Perhaps try calling `playerViewController.player!.play()`before presenting the other view? I have a feeling that this isn't able to run this line correctly within the completion block. – KSigWyatt Aug 19 '18 at 02:13
  • I can play it now, see my answer, but there is no sound.. – bibscy Aug 19 '18 at 09:54
  • Do you have `do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) }` in your AppDelegate? – KSigWyatt Aug 20 '18 at 18:03
  • No, why would I call these functions in AppDelegate? – bibscy Aug 20 '18 at 18:06
  • 1
    In order for your application to play sound, while the device is in silent mode you will need to perform these actions. I believe that may be why you aren't getting any audio during playback. See: https://stackoverflow.com/a/35988158/6448167. – KSigWyatt Aug 21 '18 at 00:17
  • 1
    Thank you for your explanation. It works:) – bibscy Aug 21 '18 at 09:59

1 Answers1

1

The problem was that when I was saving the data on the disk, I did not append .MOV extension to the end of the path, thus AVPlayer, could not play the video.

Before: mediaItem.fileURL is file:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ

After: file:///Users/bogdanbarbulescu/Library/Developer/CoreSimulator/Devices/7FB42206-997D-4AC2-B0BD-CEE2E22DAFBE/data/Containers/Data/Application/8F17DF31-16B1-4B27-ACB2-015AA55D8979/Documents/-LK7WSGtGAQ2anpDxkCQ.MOV

bibscy
  • 2,598
  • 4
  • 34
  • 82