Самый простой способ, которым я нашел прокрутку RecyclerView
, выглядит следующим образом:
// Define the Index we wish to scroll to.
final int lIndex = 0;
// Assign the RecyclerView's LayoutManager.
this.getRecyclerView().setLayoutManager(this.getLinearLayoutManager());
// Scroll the RecyclerView to the Index.
this.getLinearLayoutManager().smoothScrollToPosition(this.getRecyclerView(), new RecyclerView.State(), lIndex);
Это сработало для меня на iPad, когда я слушаю MPMoviePlayerWillExitFullscreenNotification
.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
И метод выбора:
-(void)doneButtonClick:(NSNotification*)aNotification{
NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([reason intValue] == MPMovieFinishReasonUserExited) {
// Your done button action here
}
}
проверить перечисление внутри уведомления словарь userInfo
NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([reason intValue] == MPMovieFinishReasonUserExited) {
// done button clicked!
}
Выбранный ответ с тех пор интегрировал мой ответ. Пожалуйста, обратитесь выше.
Проверьте и удалите наблюдателя с более ранними уведомлениями, если таковые имеются для MPMoviePlayerPlaybackDidFinishNotification
.
- (void)playVideo:(NSString*)filePath
{
// Pass your file path
NSURL *vedioURL =[NSURL fileURLWithPath:filePath];
MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Set the modal transition style of your choice
playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Present the movie player view controller
[self presentViewController:playerVC animated:YES completion:nil];
// Start playback
[playerVC.moviePlayer prepareToPlay];
[playerVC.moviePlayer play];
}
Контроллер увольнения
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
// Obtain the reason why the movie playback finished
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
// Dismiss the view controller ONLY when the reason is not "playback ended"
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
{
MPMoviePlayerController *moviePlayer = [aNotification object];
// Remove this class from the observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Готово!
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doneButtonClick:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
- (void)doneButtonClick:(NSNotification*)aNotification
{
if (self.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
{
NSLog(@"done button tapped");
}
else
{
NSLog(@"minimize tapped");
}
}
Версия Swift, для всех, кто интересуется:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerDoneButtonClicked:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
Обработчик уведомлений:
func moviePlayerDoneButtonClicked(note: NSNotification) {
let reason = note.userInfo?[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
if (MPMovieFinishReason(rawValue: reason as! Int) == MPMovieFinishReason.UserExited) {
self.exitVideo()
}
}
У Apple очень плохая документация по этому вопросу. Он предлагает прослушивать MPMoviePlayerDidExitFullscreenNotification (или WillExit ...), а НЕ MPMoviePlayerDidFinishNotification, так как он не срабатывает, когда пользователь нажимает «Готово». Это совсем не так! Я только что проверил это на Xcode 6.0 с iPad Simulator (iOS 7.1 + 8.0) и только MPMoviePlayerDidFinishNotification запускается при нажатии DONE.
Мои комплименты пользователю523234, который правильно понял один из комментариев выше.
Зарегистрировать следующего наблюдателя
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackStateChanged:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_mpc];
Ух ты, так много неправильных подходов. Ответ прост:
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[self.navigationController presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
Проверьте эту ссылку, если у вас есть минута: https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/UIViewController_MediaPlayer_Additions /index.html
Удачного кодирования! З.