- (void)playMethod
{
NSURL *url = [[NSURL alloc] initWithString:#"https://www.rmp-
streaming.com/media///bbb-360p.mp4"];
player = [AVPlayer playerWithURL:url];
controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(15,50,345,300);
controller.player = player;
controller.showsPlaybackControls = YES;
player.closedCaptionDisplayEnabled = NO;
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:#selector(backMethod)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Back" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button1.frame = CGRectMake(50.0, CGRectGetMinY(controller.view.frame)+10, 160.0, 40.0);
button1.tag = 1001;
[self.view addSubview:button1];
[controller.view bringSubviewToFront:button1];
[player pause];
[player play];
}
I want to play the video in landscape mode currently, its getting played in portrait mode. I want when the playMethod is called it should open the video in landscape mode only.
Create your custom class which is inherited from AVPlayerViewController and use this instead of base AVPlayerViewController
import AVKit
class CustomAVPlayerViewController: AVPlayerViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
}
Related
I cannot work this out - it should be simple.
I have:
ShowAVPlayer method
AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:url];
AVQueuePlayer *queue = [[AVQueuePlayer alloc] initWithItems:#[video]];
video = [[AVPlayerItem alloc] initWithURL:url];
[queue insertItem:video afterItem:nil];
AVPlayer *player = [AVPlayer playerWithURL:url];
self.avmovieplayer = [AVPlayerViewController new];
I want to add a button to my overlay - but the button is not responding :
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Do stuff" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 330, 320, 40);
[btn addTarget:self action:#selector(DoSpeech:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:true];
[self.avmovieplayer.contentOverlayView addSubview:btn];
Here is my DoSpeech which is never being called:
- (void) DoSpeech:(UIButton *)button {
//DoSpeech
}
Try this,
[btn setUserInteractionEnabled:YES];
[self.avmovieplayer.contentOverlayView bringSubviewToFront:btn];
Hope it will help.
I'm using AVPlayerViewController and can't showing the Done button, and can't close the player also. maybe you can help me. This is my code in objective-c
UIView *view = self.view;
NSURL *fileURL = [NSURL URLWithString: _detailData[0]];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:fileURL];
self.avPlayerViewcontroller = playerViewController;
[self resizePlayerToViewSize];
[self dismissViewControllerAnimated:YES completion:nil];
[view addSubview:playerViewController.view];
[playerViewController.player play];
view.autoresizesSubviews = TRUE;
Need help guys, thank you very much.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); // set your own position
[view addSubview:button]; // or you can add [playerViewController.view addSubview:button];
-(void)aMethod:(UIButton *)button {
// Remove playerViewController.view
}
thanks for your answer. I found it already :)
We can using this, and done button can shown automatically.
NSURL *fileURL = [NSURL URLWithString: _detailData[0]];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:fileURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;
I am trying to create a player like in a image below, starting from play and pause button. but it's not showing up on a player. how can i create customize panel for my player, custom bar, volume bar etc?
- (IBAction)playMovie:(UIButton *)sender {
NSURL *url = [[NSURL alloc] initWithString:urlStr.text];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:url];
self.movie.controlStyle = MPMovieControlStyleNone;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
[playButton setImage:[UIImage imageNamed:#"apple.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:#selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}
add player on a view with button like your image describes
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:urlVideo];
[moviePlayerController.view setFrame:CGRectMake(0, 170, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];// put it in action
i am creating one iPad application in that i want to open media player to play video. For this i create one button in ViewDidLoad() method. Bellow is my code.
- (void)viewDidLoad
{
[super viewDidLoad]
self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"1.png"]] autorelease];
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
m_btn2000.frame = CGRectMake(180, 330, 130, 200);
[m_btn2000 setImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
m_btn2000.clipsToBounds = YES;
[m_btn2000 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_btn2000];
}
Bellow is my button clicked method code:
- (void)btnClicked:(id)sender
{
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:#"test_video" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath1];
m_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
m_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[m_moviePlayer.view setFrame:CGRectMake(0, 0, 1026, 748)];
[self.view addSubview:m_moviePlayer.view];
[m_moviePlayer play];
}
when i m going to click on button it gives me EXC_BAD_ACCESS error.
But i notice one thing that when i put above whole code which in button clicked method putting in ViewDidLoad() method it play a video in player proper way.
The reason is : you are creating a button with + (factory method) and you are sending autorelease message.
you need to change this line from:
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
to
m_btn2000 = [UIButton buttonWithType:UIButtonTypeCustom];
i have created two UIButtons and only play button is appearing on the UIToolbar. In the play method i want is when play button is pressed it shows pause button and if user presses pause button it pauses the audiofile and then shows the play button.
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:#selector(play:) forControlEvents:UIControlEventTouchUpInside];
playButton.frame = CGRectMake(0, 0, 50, 50);
UIImage *image = [UIImage imageNamed:#"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton addTarget:self action:#selector(pause:) forControlEvents:UIControlEventTouchUpInside];
pauseButton.frame = CGRectMake(0, 0, 50, 50);
UIImage *imge = [UIImage imageNamed:#"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
-(void)play:(id)sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];
}
I need help in implementing this
In the play method
i did
[audioPlayer play];
UIImage *imge = [UIImage imageNamed:#"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
[audioPlayer pause];
UIImage *image = [UIImage imageNamed:#"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
[audioPlayer Play];
I think i m doing wrong way
Please advice .
Thanks for help.
Thanks
Use only one button with 1 image for each state (Selected and Normal):
UIButton *mediaButton = [UIButton buttonWithType:UIButtonTypeCustom];
mediaButton.frame = CGRectMake(0, 0, 50, 50);
[mediaButton setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
[mediaButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[playButton addTarget:self action:#selector(mediaAction:) forControlEvents:UIControlEventTouchUpInside];
-(void)mediaAction:(UIButton *)sender
{
if(sender.selected){
// pause action
} else {
// play action
}
sender.selected = !sender.selected;
}
You do not have to create two separate buttons. Just create one button and one method. Just keep the state in a boolean variable or use player state. Do what you need to do after cheking the state in method.
However if you insist on creating two separate buttons, you can hide and show one of the buttons.
do you mean:
if([audioPlayer isPlaying]) {
UIImage *imge = [UIImage imageNamed:#"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
[audioPlayer pause];
}
else {
UIImage *image = [UIImage imageNamed:#"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
[audioPlayer Play];
}
is correct?