カスタムサイズの画面でビデオを再生-iPhoneで表示
-
07-07-2019 - |
質問
ユーザーがボタンをタップし、ビデオの再生が開始されたとします。ビデオが再生されると、常に全画面モードになります。
ビデオはポートレートモードで再生する必要があります(ただし、通常、ビデオはランドスケープモードで再生されます)。どうすればいいですか?
解決
@interface MPMoviePlayerController (extend)
-(void)setOrientation:(int)orientation animated:(BOOL)value;
@end
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUR];
[moviePlayer setOrientation:UIDeviceOrientationPortrait animated:NO];
if (moviePlayer)
{
[self.moviePlayer play];
}
ムービープレーヤーのsetOrientationはプライベートAPIであるため、このソリューションはAppleによって拒否されます。注意する必要がありますが、JailbrokeのiPhoneで動作する可能性があります。
他のヒント
最新のiPhone SDK 3.2+では、プログラマーが任意のサイズと向きでビデオを表示できるようになりました。MPMoviePlayerControllerのプロパティである新しいMPMoviePlayerViewが提供され、このビューにはビデオがあります。ビューにサブビューとして追加できます。
文書化されたドキュメントから、組み込みのメディアプレーヤーを使用してこれが可能だとは思わない
これを試してください。 新しいものを見つけました。
@interface MPMoviePlayerController (extend)
-(void)setOrientation:(int)orientation animated:(BOOL)value;
@end
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUR];
[moviePlayer setOrientation:UIDeviceOrientationPortrait animated:NO];
if (moviePlayer)
{
[self.moviePlayer play];
}
これが私がしたことです。 NSNotificationを追加して、ビデオのプリロードが完了したときに通知します。
- (void)playVideoUrl:(NSString *)videoUrl {
NSURL *url = [NSURL URLWithString:videoUrl];
MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
//MPMoviePlayerContentPreloadDidFinishNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedPreloading:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
}
コールバックセレクター:
-(void)myMovieFinishedPreloading:(NSNotification*)aNotification {
NSArray *windows = [[UIApplication sharedApplication] windows];
UIWindow *moviePlayerWindow = nil;
if ([windows count] > 1)
{
moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
}
CGAffineTransform transform = CGAffineTransformMakeScale(0.5, 0.5);
transform = CGAffineTransformRotate(transform, -90.0f*M_PI/180.0f);
[moviePlayerWindow setTransform:transform];
}
所属していません StackOverflow