iosの使用-MPMoviePlayerControllerおよびMPMoviePlayerViewController



Use Ios Mpmovieplayercontroller



これらの2つのクラスは非推奨になりましたが、ここにそれらの使用の簡単な記録があります

まず、これら2つのクラス、MPMoviePlayerViewControllerには独自のインターフェイスがあり、非常にシンプルに使用します。



//1, get the URL address NSURL * url = [[NSBundle mainBundle]URLForResource:@'test.mp4' withExtension:nil] //2, create a player with view MPMoviePlayerViewController * mpVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url] //3, the modal view pops up [self presentViewController:mpVC animated:YES completion:nil]
MPMoviePlayerControllerを使用している場合、手順は次のとおりです。ここで、MPMoviewPlayerControllerをプロパティとして定義する必要があることに注意してください。この設定の意味は、このDoを渡して、必要なインターフェイスを作成できることです。 //1, get the URL address NSURL * url = [[NSBundle mainBundle]URLForResource:@'test.mp4' withExtension:nil] //2, create a controller without view self.mpController = [[MPMoviePlayerController alloc]initWithContentURL:url] //3, set the frame of the view self.mpController.view.frame = CGRectMake(0, 0, 300, 300) //4, to add to the view [self.view addSubview:self.mpController.view] //5, ready to play [self.mpController prepareToPlay] //6, start playing [self.mpController play] //7, control mode self.mpController.controlStyle = MPMovieControlStyleFullscreenまた、ビデオによっては[完了]ボタンをクリックする必要がある場合もあります。前の曲と次のボタンを使用して対応する操作を実行する場合、通知メソッドを監視してステータスを監視し、対応する処理を実行できます。実際にMPMoviePlayerControllerクラスをクリックして確認すると、通知名が多数あります。以下に示すように

次の図に示すように、ここでは2つだけを使用します。




実際、MPMoviePlayerPlaybackDidFinishNotificationの通知を監視することにより、呼び出しメソッドの通知のuserInfoのキーは実際にはMPMoviePlayerPlaybackDidFinishReasonUserInfoKeyであり、これは上で丸で囲んだ2番目のキーです。これであるキーに対応する値の意味を検索すると、実際には列挙型であることがわかります。以下は内部の列挙型の値です。




[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil] -(void)moviePlayerPlaybackDidFinishNotification:(NSNotification *)notification { /** MPMoviePlayerPlaybackDidFinishReasonUserInfoKey Enumeration value of MPMovieFinishReason MPMovieFinishReasonPlaybackEnded, MPMovieFinishReasonPlaybackError, MPMovieFinishReasonUserExited */ NSLog(@'%@',notification.userInfo) //1. Get the status of the notification end NSInteger finishReasonUserInfoKey = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]integerValue] //2, go to the logic of writing code according to different states switch (finishReasonUserInfoKey) { case MPMovieFinishReasonPlaybackEnded: //Click the previous song, when the next song, and when the playback is complete NSLog(@'Play End') break case MPMovieFinishReasonPlaybackError: NSLog(@'Play Error') break case MPMovieFinishReasonUserExited: //When you click done NSLog(@'Quit playing') break } }