How to Play Video from Within Collapsible UITableViewCell? - objective-c

HI All: I have set up collapsible uitableviewcells and am trying to get video to play in the didSelectRowAtIndexPath method. I can get cells to fire a print statement to the console, but no matter what I try to get a video to play nothing works! Here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
printf("hello");
}
else
{
printf("good bye");
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
printf("hello");
}
else
{
printf("good bye again");
}
}
}
Can you please let me know what I need to do to get a local video file to play? Obviously I need help in Objective C please! I have tried to programmatically create an AVPlayerViewController and then use [self presentviewcontroller] method but I always get the following error (which is same error if I use prepareforSegue):
NSString *path = [[NSBundle mainBundle] pathForResource:#"PrelimFig1" ofType:#"m4v"];
_videoURL = [NSURL fileURLWithPath:path];
NSLog(#"I want to log: %#", _videoURL);
AVPlayerViewController *avp = [[AVPlayerViewController alloc]init];
AVPlayer *player = [AVPlayer playerWithURL:_videoURL];
avp.player = player;
[self presentViewController:avp animated:YES completion:nil]
[player play];
Thank you in advance for your help!

You can do like that
NSURL *videoURLLocal = [[NSBundle mainBundle]URLForResource:#"PrelimFig1" withExtension:#"m4v"];
AVPlayerViewController *avp = [[AVPlayerViewController alloc]init];
AVPlayer *player = [AVPlayer playerWithURL:videoURLLocal];
avp.player = player;
[self presentViewController:avp animated:YES completion:^{
[player play];
}];
or
NSURL *videoURLLocal = [[NSBundle mainBundle]URLForResource:#"PrelimFig1" withExtension:#"m4v"];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURLLocal];
// 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;

Related

how to share image on instagram from native app ios 9.2?

i have tried this code but it shows error like
Warning: Attempt to present <_UIDocumentActivityViewController:
0x16acdc00> on which is already
presenting <_UIDocumentActivityViewController: 0x16ae4800>
how to solve this??
-(IBAction)saveToInstagram:(id)sender {
CGRect rect = CGRectMake(0 ,0 , 0, 0);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.ig"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
NSLog(#"JPG path %#", jpgPath);
NSLog(#"URL Path %#", igImageHookFile);
self.docFile.UTI = #"com.instagram.photo";
self.docFile = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.docFile=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://media?id=MEDIA_ID"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
}
else {
NSLog(#"No Instagram Found");
}
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {
}
The problem is that while the first docfile is being presented you try to present the second one
// First presentation
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://media?id=MEDIA_ID"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
// Second presentation
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
}
I don't really understand why do you need to use the same method twice, but if you really do, then for the first presentation use animated: NO. I would suggest some kind of completion code block, but it appears that there is no such method for UIDocumentInteractionController
Don't forget to add instagram in you plist array with key: LSApplicationQueriesSchemes.
#property (nonatomic, strong) UIDocumentInteractionController *docIC;
- (void)sharePhoto:(UIImage *)image caption:(NSString *)caption
{
NSURL *instagramURL = [NSURL URLWithString:#"instagram://"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
NSString *imgPath = [TUtil documentPath:#"Image.igo"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[data writeToFile:imgPath atomically:YES];
NSURL *igImageHookFile = [NSURL fileURLWithPath:imgPath];
self.docIC = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[self.docIC setDelegate:self];
[self.docIC setUTI:#"com.instagram.exclusivegram"];
// Subject
[self.docIC setName:[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleDisplayName"]];
//http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing
//self.docIC.annotation = [NSDictionary dictionaryWithObjectsAndKeys:caption, #"InstagramCaption", nil];
[self.docIC presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
else
{
NSString *iTunesLink = #"itms-apps://itunes.apple.com/app/id389801252?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
}
}

How to record video from iPhone camera in iOS 9 objective c?

hello I need to build app that record video from iPhone camera.
I search in the net and not found notting that works in iOS 9.
I get this code from this git project :
https://github.com/versluis/Video-Recorder
This code open camera but not allow Me to take a video.
- (IBAction)recordButton:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = NO;
NSArray *mediaTypes = [[NSArray alloc]initWithObjects:(NSString *)kUTTypeMovie, nil];
picker.mediaTypes = mediaTypes;
[self presentViewController:picker animated:YES completion:nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:#"I'm afraid there's no camera on this device!" delegate:nil cancelButtonTitle:#"Dang!" otherButtonTitles:nil, nil];
[alertView show];
}
}
- (IBAction)playbackButton:(id)sender {
// pick a video from the documents directory
NSURL *video = [self grabFileURL:#"video.mov"];
// create a movie player view controller
MPMoviePlayerViewController * controller = [[MPMoviePlayerViewController alloc]initWithContentURL:video];
[controller.moviePlayer prepareToPlay];
[controller.moviePlayer play];
// and present it
[self presentMoviePlayerViewControllerAnimated:controller];
}
#pragma mark - Delegate Methods
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
// user hit cancel
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// grab our movie URL
NSURL *chosenMovie = [info objectForKey:UIImagePickerControllerMediaURL];
// save it to the documents directory
NSURL *fileURL = [self grabFileURL:#"video.mov"];
NSData *movieData = [NSData dataWithContentsOfURL:chosenMovie];
[movieData writeToURL:fileURL atomically:YES];
// save it to the Camera Roll
UISaveVideoAtPathToSavedPhotosAlbum([chosenMovie path], nil, nil, nil);
// and dismiss the picker
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSURL*)grabFileURL:(NSString *)fileName {
// find Documents directory
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
// append a file name to it
documentsURL = [documentsURL URLByAppendingPathComponent:fileName];
return documentsURL;
}
Ray gave pretty good tutorial about this. Hope it helped.

objective-c send url according to my menu select

i have a custom UItableView . i am loading data from a API and my main view are
i am loading data from a API .
My problem is,i have a left-side menu, i want to send a url when user select any menu acoording to select menu view will load.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
if(indexPath.row ==0){
appdataModel.newsApiUrl = homePagesUrl;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.revealViewController revealToggleAnimated:YES];
ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:#"ContactsTableViewController" bundle:nil];
[self.navigationController pushViewController:contView animated:YES];
}else if (indexPath.row ==1){
appdataModel.newsApiUrl = jatioNews;
NSLog(#"here 1 :%#",appdataModel.newsApiUrl);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.revealViewController revealToggleAnimated:YES];
ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:#"ContactsTableViewController" bundle:nil];
[self.navigationController pushViewController:contView animated:YES];
}
else if (indexPath.row ==2){
appdataModel.newsApiUrl = jatioNews;
NSLog(#"here 1 :%#",appdataModel.newsApiUrl);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.revealViewController revealToggleAnimated:YES];
ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:#"ContactsTableViewController" bundle:nil];
[self.navigationController pushViewController:contView animated:YES];
}
}
#define homePagesNews #"http://198.72.115.125/~pratidin/api/topnews"
#define jatioNews #"http://198.72.115.125/~pratidin/api/categorynews/4"
here my api link . if user select Home-menu then view will load form homePagesNews API else user select second menu then view will from jatioNews API
from this code i am getting data
-(void)GetHomePageData{
NSString *urlString = [NSString stringWithFormat:#"%#",url];
NSURL *url = [[NSURL alloc]initWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];
}
now i want when user select menu may home-menu or second-menu or third-menu according to user select url link will change and view will load in my ContactsTableViewController
my ContactsTableViewController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
appdataModel = [AppDataModel getInstance];
appdataModel.newsApiUrl = homePagesUrl;
/**** for left side menu ***/
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.sideBarButton setTarget: self.revealViewController];
[self.sideBarButton setAction: #selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
/**** for Contractview***/
self.view.backgroundColor = [UIColor whiteColor];
NSString *path = [[NSBundle mainBundle] pathForResource:#"contacts" ofType:#"plist"];
contactsArray = [NSArray arrayWithContentsOfFile :path];
[self GetHomePageData];
[self.newsDataTableView reloadData];
}
some can tell me how can i solve my problem ... Thanks
Add a url property to ContactsTableViewController:
#interface ContactsTableViewController : UIViewController
#property NSURL *url;
...
#end
and in its viewDidLoad method you can load the data using whatever method you are using:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
dataTaskWithURL:self.url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Whatever
}];
Then set the URL from the tableview delegate method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
if (indexPath.row ==0){
appdataModel.newsApiUrl = homePagesUrl;
} else {
appdataModel.newsApiUrl = jatioNews;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.revealViewController revealToggleAnimated:YES];
ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:#"ContactsTableViewController" bundle:nil];
vc.url = [NSURL URLWithString:appdataModel.newsApiUrl];
[self.navigationController pushViewController:vc animated:YES];
}

webView loading not being called

I am loading a page by pressing a table view cell. I put an if statement :
if ([webViewController webView].loading == YES) {
NSLog(#"Its loading!");
if (act == nil) {
NSLog(#"Its nil");
act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
act.color = [UIColor blackColor];
} else {
act.hidden = NO;
}
act.center = self.view.center;
[act startAnimating];
} else {
[act stopAnimating];
[act setHidden: YES];
}
But the NSLog never shows up so it is never called. But when i deploy it to my iPhone it takes A LONG TIME to load but it is still never called. What is happening here?
UPDATE - SORROUNDING CODE
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"TOUCHED CELL!");
// Push the web view controller onto the navigation stack - this implicitly
// creates the web view controller's view the first time through
[[self navigationController] pushViewController:webViewController animated:YES];
// Grab the selected item
RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]];
// Construct a URL with the link string of the item
NSURL *url = [NSURL URLWithString:[entry link]];
// Construct a request object with that URL
NSURLRequest *req = [NSURLRequest requestWithURL:url];
// Load the request into the web view
[[webViewController webView] loadRequest:req];
if ([webViewController webView].loading == YES) {
NSLog(#"Its loading!");
if (act == nil) {
NSLog(#"Its nil");
act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
act.color = [UIColor blackColor];
act.center = self.view.center;
[self.view addSubview:act];
} else {
act.hidden = NO;
}
act.center = self.view.center;
[act startAnimating];
} else {
[act stopAnimating];
[act setHidden: YES];
}
webViewController = nil;
webViewController = [[WebViewController alloc] init];
}
I'm not sure what you're trying to do here. If you want to show an activity indicator while the web view is loading, you should put that code in the webViewController. (The view controller your code is in won't be visible after you push the web view controller anyway.)
Ensure that the webViewController is delegate of the webView, and then override webViewDidStartLoad: and webViewDidFinishLoad: (and webView:didFailLoadWithError:) to show and hide your activity indicator.

i have two mpmovieplayercontrollers and two separate subviews that they utilze... except that only one video will play at a time

I would like to have both movies playing at once in their two separate sub views. They are both accessing different media.
this is on an ipad with a superview and two little views 320x240 right by eachother on the xib.
-(IBAction)playLeft:(id)sender{
if ([self.playerRight playbackState] == MPMoviePlaybackStatePlaying);
[self.playerRight stop];
[self.playerLeft play];
}
-(IBAction)playRight:(id)sender{
if ([self.playerLeft playbackState] == MPMoviePlaybackStatePlaying);
[self.playerLeft stop];
[self.playerRight play];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
self.playerLeft = [[MPMoviePlayerController alloc] init];
self.playerLeft.contentURL = [self movieURL];
NSLog(#"self.playerLeft %#", self.playerLeft);
self.playerRight = [[MPMoviePlayerController alloc] init];
self.playerRight.contentURL = [self movieURL2];
NSLog(#"self.playerRight %#", self.playerRight);
// START_HIGHLIGHT
self.playerLeft.view.frame = self.leftView.bounds;
self.playerLeft.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
self.playerRight.view.frame = self.rightView.bounds;
self.playerRight.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.rightView addSubview:playerRight.view];
[self.leftView addSubview:playerLeft.view];
//[self.playerRight play];
//[self.playerLeft play];
//[self clickedOpenMovie:nil];
}
-(NSURL *)movieURL
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath =
[bundle
pathForResource:#"720p5994-prores-hq_iPhone_320x240 two"
ofType:#"m4v"];
//NSString *moviePath = [NSString stringWithFormat:#"http://localhost:1935/live/aStream/playlist.m3u8"];
if (moviePath) {
return [NSURL fileURLWithPath:moviePath];
//return [NSURL URLWithString:moviePath];
} else {
return nil;
}
}
-(NSURL *)movieURL2
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath =
[bundle
pathForResource:#"720p5994-prores-hq_iPhone_320x240"
ofType:#"m4v"];
if (moviePath) {
return [NSURL fileURLWithPath:moviePath];
} else {
return nil;
}
}
so the problem is that somewhere in the background MPMoviePlayerController responds to notifications and turns itself off if another MPMoviewPlayerController broadcasts that its going to start. Thus one can not have multiple MPMoviePlayerControllers playing at the same time.
you can see this by printing out the MPMoviePlayerPlaybackStateDidChangeNotification