MPMovieViewController下一个/上一个按钮事件

时间:2015-04-11 13:30:55

标签: ios swift

我是Swift的新手 我正在使用MPMovieviewcontroller 当我点击下一个或上一个按钮时,没有事件发生 这是代码

import UIKit
import MediaPlayer
class ViewController: UIViewController {
var i : Int = 0

var movieplayer : MPMoviePlayerController!

var arr = ["Akbar","Serial","ak"]
override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()
    startplaying()

}

override func canBecomeFirstResponder() -> Bool {
    return true
}
override func remoteControlReceivedWithEvent(event: UIEvent) {
    if event.subtype == UIEventSubtype.RemoteControlNextTrack
    {
        i++
        startplaying()
      println(event.subtype)
    }
}
func startplaying()
{
 if i <= 2
 {
 let path = NSBundle.mainBundle().pathForResource(arr[i], ofType: "mov")
let url = NSURL.fileURLWithPath(path!)

movieplayer = MPMoviePlayerController(contentURL: url)
}

movieplayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
movieplayer.view.sizeToFit()

movieplayer.scalingMode = MPMovieScalingMode.AspectFill
movieplayer.fullscreen = true

movieplayer.controlStyle = MPMovieControlStyle.Fullscreen

movieplayer.movieSourceType = MPMovieSourceType.File
movieplayer.prepareToPlay()

self.view.addSubview(movieplayer.view)
}

当我调试此代码时,remoteControlReceivedWithEvent函数未执行

我从here

获得帮助

2 个答案:

答案 0 :(得分:1)

试试这个

func startplaying() {
    if i <= 2 {
        let path = NSBundle.mainBundle().pathForResource(arr[i], ofType: "mov")
        let url = NSURL.fileURLWithPath(path!)
    movieplayer.contentURL = NSURL.fileURLWithPath(url)
}

了解详情:Play MP4 using MPMoviePlayerController() in Swift

答案 1 :(得分:0)

这里我使用按钮进行下一个/上一个手动操作 我希望这是下一个/上一个操作的正确方法

import UIKit
import MediaPlayer
class ViewController: UIViewController {
var movieplayer : MPMoviePlayerController!
var arr = ["ak","Serial","Akbar"]
var i = 0
override func viewDidLoad() {
    super.viewDidLoad()
    startplaying()
}
@IBOutlet var views: UIView!
var flag = 0
@IBOutlet var next: UIButton!
@IBOutlet var pre: UIButton!
@IBOutlet var play: UIButton!
func startplaying()
{
if i >= 0 && i <= 2
{
 let path = NSBundle.mainBundle().pathForResource(arr[i], ofType: "mov")
 let url = NSURL.fileURLWithPath(path!)
 movieplayer = MPMoviePlayerController(contentURL: url)


    movieplayer.view.frame = CGRect(x: 0, y: 0, width: 320, height: 450)
    movieplayer.view.sizeToFit()

    movieplayer.scalingMode = MPMovieScalingMode.AspectFill
    movieplayer.fullscreen = true

    movieplayer.controlStyle = MPMovieControlStyle.Embedded

    movieplayer.movieSourceType = MPMovieSourceType.File
    movieplayer.repeatMode = MPMovieRepeatMode.One

    movieplayer.prepareToPlay()

   // self.view.addSubview(movieplayer.view)
    self.views.addSubview(movieplayer.view)
  }
  else
  {
    println("No Video Avaliable")
    movieplayer.stop()
  }
}


@IBAction func Previous(sender: AnyObject) {
    if i >= 0
    {
      i--
      startplaying()
    }
    else
    {
      println("No Video Avaliable")
      movieplayer.stop()
    }

}

@IBAction func Pause(sender: AnyObject) {
    if flag == 0
    {
     play.setTitle("Pause", forState: UIControlState.Normal)
     movieplayer.pause()
      flag = 1
    }
    else
    {
        play.setTitle("Play", forState: UIControlState.Normal)
        movieplayer.play()
        flag = 0
    }
}

@IBAction func next(sender: AnyObject) {
    if i <= 2
    {
     i++
     startplaying()
    }
    else
    {
      println("No Video Avaliable")
        movieplayer.stop()
    }
}