1

AvPlayerをAvAudioPlayerのように動作させるにはどうすればよいですか。私が言いたいのは、私はまったく仕事をする時間が取れないようだということです。AvPlayerに変換する必要があるものは次のとおりです。

import UIKit
import AVKit
import AVFoundation


class ModalViewController: UIViewController {

    var audioPlayer = AVAudioPlayer()
    let varSend = VarSend.sharedInstance
    var timer:NSTimer!
    var toggleState = 2


    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var sermonImage: UIImageView!
    @IBOutlet weak var sermont: UILabel!
    @IBOutlet weak var sermond: UILabel!
    @IBOutlet weak var sermonE: UILabel!
    @IBOutlet weak var sermonL: UILabel!
    @IBOutlet weak var play: UIButton!
    @IBOutlet var layer: UIView!


    override func viewDidLoad() {

        super.viewDidLoad()
        let url = varSend.url
        print("Setting up.")



        do {
           let data1 =  NSData(contentsOfURL: NSURL(string:url)!)
            audioPlayer = try AVAudioPlayer(data: data1!)
            audioPlayer.prepareToPlay()
            audioPlayer.volume = 1.0
            audioPlayer.play()
        } catch {
            print("Error getting the audio file")
        }



        slider.maximumValue = Float(audioPlayer.duration)
        timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateSlider"), userInfo: nil, repeats: true)
        slider.setThumbImage(UIImage(named: "circle"), forState: .Normal)
        slider.setThumbImage(UIImage(named: "circle"), forState: .Highlighted)


        let title = varSend.sermonName
        self.sermont.text = title

        let date = varSend.sermonDate
        self.sermond.text = date

        let image = varSend.sermonPic

        ImageLoader.sharedLoader.imageForUrl(image, completionHandler:{(image: UIImage?, url: String) in
            self.sermonImage.image = image!
        })
    }

    @IBAction func ChangeAudioTime(sender: AnyObject) {
        audioPlayer.stop()
        audioPlayer.currentTime = NSTimeInterval(slider.value)
        audioPlayer.prepareToPlay()
        audioPlayer.volume = 1.0
        audioPlayer.play()

    }



    func updateSlider() {
        slider.value = Float(audioPlayer.currentTime)
        let currentTime = Int(audioPlayer.currentTime)
        let minutes = currentTime / 60
        let seconds = currentTime - minutes * 60
        let con = Int(audioPlayer.duration)
        let currentItem = con - currentTime
        let minutesU = Int(currentItem / 60)
        let secondsU = Int(currentItem % 60)
        sermonE.text = NSString(format: "%02d:%02d", minutes,seconds) as String
        let timeLeft = NSString(format: "%02d:%02d", minutesU,secondsU) as String
        sermonL.text = "-\(timeLeft)"

        if currentItem == 1 {
            //audioPlayer.pause()
            toggleState = 1
            print(toggleState)
            play.setImage(UIImage(named:"play.png"),forState:UIControlState.Normal)
        }
    }


    @IBAction func playPauseButton(sender: AnyObject) {
        let playBtn = sender as! UIButton
        if toggleState == 1 {
            audioPlayer.play()
            toggleState = 2
            playBtn.setImage(UIImage(named:"pause.png"),forState:UIControlState.Normal)

        } else {
            audioPlayer.pause()
            toggleState = 1
            playBtn.setImage(UIImage(named:"play.png"),forState:UIControlState.Normal)
        }
    }


    @IBAction func play(sender: AnyObject) {
        audioPlayer.play()
    }

    @IBAction func pause(sender: AnyObject) {
        audioPlayer.pause()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func close(sender: AnyObject) {
       self.dismissViewControllerAnimated(true, completion: nil)
        audioPlayer.stop()
    }

}

そして、これが私がすでに試したことです:

import UIKit
import Alamofire
import SwiftyJSON
import AVKit
import AVFoundation
import CoreMedia

class test: UIViewController {
    var audioPlayer:AVPlayer!
    var playerItem:AVPlayerItem!

   var timer:NSTimer!

    @IBOutlet weak var sermonE: UILabel!
    @IBOutlet weak var sermonL: UILabel!
    @IBOutlet weak var slider: UISlider!

    override func viewDidLoad() {
        super.viewDidLoad()


        //let playerItem:AVPlayerItem!;
        let playerURL = "example.com"
        let steamingURL:NSURL = NSURL(string:playerURL)!
        audioPlayer = AVPlayer(URL: steamingURL)


         timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateSlider"), userInfo: nil, repeats: true)






    }

     func updateSlider() {
        let item = audioPlayer?.currentItem

            let durationInSeconds = CMTimeGetSeconds(item!.duration)
        print(durationInSeconds)

    }

    @IBAction func ChangeAudioTime(sender: AnyObject) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    @IBAction func Play(sender: AnyObject) {
        audioPlayer.play()
    }

}

私は何日も検索してきましたが、Apple のドキュメントを理解するのは非常に困難です。私も試してみました

self.player.currentItem.asset.duration

from: AVPlayer (AVAudioPlayer ではない) から期間を取得する方法は?

どんな助けでも大歓迎です。

4

1 に答える 1