playing a youtube movie from url in objective C - objective-c

I have a tableview which contains youtube urls.
http://www.youtube.com/v/M67PNWvKdg0&autoplay=1
I am trying to play the video file with the following piece of code. I also imported the media framework.
Video *video = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *urlString=[NSURL URLWithString:video.url];
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
//pathForResource:#"Video1" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:urlString];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
What it does for now is it pops up a movie player but then immediately goes back to my tableview. Does anybody knows what the problem is?
Thanks in advance

Are you working on iOS 6? I found difficulty in playing Youtube videos through MPMoviePlayerController, so I found out this to be very useful.
This has MPMoviePlayerController only...
And this would work well for Versions < iOS 6 too.

You can't play youtube videos via Media Player. You have only two solutions: open the link via external app (just open it and iOS will run Safari or YouTube.app if installed) or you can implement your own view with UIWebView and open the link inside WebView.

May be this link would help you, and one more thing is the video plays only on device cant test on a simulator.

There's another way but you have to be a little adventurous.
You can use a third party rtsp framework of which there are a few.
Then you need to link to the m.youtube.com, this is the rtsp version of youtube that Android and other phones will get you to.
If you try to open this link in safari on the iphone the page redirects to the h264 links.
You can open the url in an uitableview and change the user-agent tag. Or simply copy the links you want into a table.
Are all youtube videos automatically converted to h264 now, at one time I remember they were a subset of the rtsp pages.
If you have an Android device you can access this page in a webview quite easily.
here check this link out.
http://streammore-tv.tumblr.com/post/34794206547/welcome

Related

Facebook share functionality is not working in iOS8

I have implemented Facebook sharing functionality using social framework. I have configured the Facebook account using device setting. When I installed the Facebook app on device, sharing is not working as expected.
When share window appears, it only has the image for sharing and no initial text and URL.
iOS8 - Share functionality is working properly if I remove the Facebook app from device.
iOS7 - Share functionality is working properly even the Facebook app is on device.
I am not sure where it is a framework issue or something is missing?
SLComposeViewController *facebookShareVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
NSString *imagePath = [NSString stringWithFormat:#"%#/%#_%#_%#.gif", [[STInteractionManager sharedManager] getAppLibraryCacheDirectory], article.journalID, article.volume, article.issueNumber];
UIImage* img = [[STInteractionManager sharedManager] imageNamedFromDocuments:imagePath];
[facebookShareVC setInitialText:self.articleTitle];
[facebookShareVC addURL:[NSURL URLWithString:[self.url stringByReplacingOccurrencesOfString:#"?dispform=ios-device" withString:#""]]];
[facebookShareVC addImage:img];
[self presentViewController:facebookShareVC animated:YES completion:nil];
If you want to pre-fill the fields, you shouldn't use the Facebook SDK at all. Instead, use the Social framework provided by Apple. With only a few lines of code, you can trigger the SLComposeViewController.

objective c iphone musicplayer crashing

I am creating a non appstore jailbreak tweak and I came across a problem that causes my app to crash.
I am using musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
the music player has complete functionality, play/pause, next, previous. it displays the title of the song, the artist, the artwork and everything.
the problem seems to be the loading and unloading of the musicplayer.
for example, if i am running the app, and i close the iphone's Music app, it will cause my app to crash. also if i load the app without the Music app running in the background it will crash.
as long as the music app is running in the background my app will not crash.
can anyone help me with this?
is something to be set to nil? or how do i go about releasing them?
should they be synthesize/property?
I was having this problem also for my tweak, and I couldn't figure out a way to fix it. I ended up using the AVPlayer Method, It seems to work well for me. Granted I only need to play one song, so if you need to play a playlist of some sorts you are out of luck.
I used it like the following in the mediapickerclass:
NSURL *url = [[mediaItemCollection.items objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL]
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
If you need you need to reference it from somewhere else you can just write the url to a .plist

AVQueuePlayer playing items simultaneously rather than in sequence

I am trying to put together a simple app to play a pre-roll video followed by some content video.
Currently, I'm trying to use the AVQueuePlayer class to get this done. Unfortunately, it seems to want to play the videos properly in sequence.
For example, the pre-roll plays by itself for a few seconds, then (prior to the pre-roll being complete) it starts to try and play the content video. For a few seconds, the player seems to be at war with itself and switches back and forth between the two videos (neither playing very well at all). Finally, when the pre-roll is finished, the content video then plays the rest of the way normally.
I've looked through the documentation for the AVQueuePlayer and I don't see anything obvious that I'm missing.
My code is pretty basic:
AVPlayerItem *preRollItem = [AVPlayerItem playerItemWithURL: preRollUrl];
AVPlayerItem *contentItem = [AVPlayerItem playerItemWithURL: contentUrl];
self.player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:preRollItem, contentItem, nil]];
[self.player play];
What is the trick to getting the videos to play in sequence.
Make sure you are actually testing on the device. From my experience the iOS5 simulator has big problems with AVQueuePlayer and does bizarre things.
I found the iOS4.3 simulator is doing a much better job when it comes to testing AVFoundation.

Go to iPhone maps app from another app

I'm writing an app where I want the user to be able to get directions. I know about the google API, and I have that implemented, but I was wondering about ways to jump directly to the native Maps app and give the user directions instead. I think I've seen it done before, but I could be mistaken.
Any help is much appreciated.
You can open the maps app by using regular URL that points to Google Maps. From the Apple URL Scheme reference:
The maps URL scheme is used to show geographical locations and to generate driving directions between two points. If your application includes address or location information, you can use map links to forward that information to the Maps application on iOS and the Google Maps website on other platforms.
Unlike some schemes, map URLs do not start with a “maps” scheme identifier. Instead, map links are specified as regular http links but are targeted at the Google Maps servers. The following examples show the strings you would use in Safari and in a native application to show a map of the city of Cupertino, California.
HTML link:
Cupertino
Note that on a device that does not have maps installed (not sure if those even exist), the link will open in a regular browser, since it is simply an HTTP link.
To open the URL, you can present the link in a UIWebView, or you can use code to open it. for example:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://maps.google.com/maps?q=Cupertino"]];
If you wanted to pass in a custom location and open it when the user taps a button, you can make a method like this:
- (IBAction) openMapsAppAndShowLocation:(NSString *)locationToShow{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", locationToShow]]];
}
If you wanted, you could refactor the method to take latitude/longitude pair too. That might look like this:
- (IBAction) openMapsAppAndShowLatitude:(double)latitude andLongitude:(double)longitude{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://maps.google.com/maps?q=\"%f,%f\"", latitide, longitude]]];
}
You can use either this:
NSString *latlong = #"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?ll=%#",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
this,
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: #"http://maps.google.com/maps?q=London"]];
or this
[someUIApplication openURL:[[NSURL alloc] initWithString: #"http://maps.google.com/maps?q=London"]]
You can even specify the zoom level using the z flag (values between 1-19):
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://maps.google.com/maps?z=8"]];
That should invoke the Google Maps app. I think that these instructions only work on the actual device and NOT in the simulator. Good luck and comment below if you have any problem with the snippets :)
Very simple
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString:#"http://maps.google.com/maps?q=London"]];
Easy as a kiss
#Moshe
Note that on a device that does not have maps installed (not sure if those even exist)
Take an iPhone simulator for example.

Is it possible to play multiple video files simultaneously on an iPad in either HTML5 or Native App?

In either HTML5 or written natively in Objective-c, I need to generate a grid of video thumbnails that are automatically playing when the page or view loads on an iPad. I have been reading on multiple forums and StackOverflow. Some people indicate this is possible with AVController. Others including Apple state,
"Note: Although you can create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time can play its movie."
Reference: MPMoviePlayerController
In HTML5, it works on a Macbook where all 9 videos are playing:
<video class="movie" src="videos/aerials.m4v" autoplay controls width="200" height="110"></video>
Paste this 9x with the proper links and a nice grid of videos starts playing no problem. On the iPad however, the HTML5 loaded into a webview yields the same 9 grid but with no videos playing immediately. Only 1 video is playable at a time.
Now I took the objective-c path and tried it with a different approach to test out Apple's statement referenced earlier:
moviePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
moviePlayer1.view.frame = CGRectMake(0, 0, 200, 110);
[self.view addSubview:moviePlayer1.view];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer1];
[moviePlayer1 play];
moviePlayer2 = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
moviePlayer2.view.frame = CGRectMake(0, 300, 200, 110);
[self.view addSubview:moviePlayer2.view];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer2];
[moviePlayer2 play];
This indeed does show both video files on a view but again the same issue as with HTML5 and only 1 video playable at a time.
I believe this is because of a hardware limitation with decoding potentially that Apple is enforcing on the iPad to prevent CPU usage from sky rocketing and maintaining yet another strict level of control over the media portion in the framework. Am I right or wrong? If wrong, please help provide the code segment that will enable my task. Thanks!
You actually answered your own question already by the given quote. But just to make it clear to everyone, once again from my answer to a very similar question.
That is not possible. Only one movie/stream can be played at a time when using MPMoviePlayerController / MPMoviePlayerViewController.
From MPMoviePlayerController Class Reference
Note: Although you may create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time may play its movie.