how to play video using video data in iOS - ios

I am getting video data from server. i am using signalR Framework. first of all i am capturing video from iPhone photo library. Then next i am converting encoding format (base64EncodedStringwithOptions) method. next i am sending video encrypted data to signalR server. and next i am receiving video data and decrypted and play the video data using AVPlayer. but my problem video does not played. but i received video data from signalR. For decrypting i am using this method. (NSDataBase64DecodingIgnoreUnknownCharacters)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *videoFile=[documentDirectory stringByAppendingPathComponent:post.mediaName];
NSURL *url;
if ([[NSFileManager defaultManager] fileExistsAtPath:videoFile]) {
url=[NSURL URLWithString:videoFile];
}
NSLog(#"Video URL is:%#",url);
AVPlayer *player = [AVPlayer playerWithURL:url];
cell.videoPlayerController.player = player;
[player play];
i got Video URL like this:
Video URL is:/Users/Library/Developer/CoreSimulator/Devices/EEC96DE7-6D0F-4D80-82B8-3C24E4F6B3EF/data/Containers/Data/Applicatication video0006.mp4

You can use MPMoviePlayerController to play video from url' but make sure that you have to declare property for that like,
#property MPMoviePlayerController *videoController;
don't create local object of MPMoviePlayerController.
you can use it something like,
self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:tempView.videoURL]; //here your url
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.videoController.view];
// close or cancel button if you want to put
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 20, 60, 30)];
[closeButton addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Cancel" forState:UIControlStateNormal];
[self.videoController.view addSubview:closeButton];
// add observer to notify when playing will be completed if you want to put
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoController];
[self.videoController play];
Update :
You must use file url like,
NSURL *url = [NSURL fileURLWithPath:videoFile];
instead of
[NSURL URLWithString:videoFile];

Related

loading video to a view from Library/Caches

for my app I want the user to record a video which will be stored in the caches directory for repeated use throughout the application.
NSString *DestFilename = # "output.mov";
//Set the file save to URL
NSLog(#"Starting recording to file: %#", DestFilename);
NSString *DestPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
DestPath = [paths objectAtIndex:0];
DestPath = [DestPath stringByAppendingPathComponent:DestFilename];
NSURL* saveLocationURL = [[NSURL alloc] initFileURLWithPath:DestPath];
[MovieFileOutput startRecordingToOutputFileURL:saveLocationURL recordingDelegate:self];
The NSLog says:
url = file:///var/mobile/Containers/Data/Application/BD41ABB0-77BA-4872-B447-07906A3C6FA7/Library/Caches/output.mov
How can I load or possibly embed this video into one of the ViewControllers in my storyboard?
Basically, your video URL is there, so you can load the video into your view controller and play it in 2 possible approaches: using MPMoviePlayerController or AVPlayer
For example from your view controller
//The first approach
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:saveLocationURL];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController play];
//The second approach
self.playerItem = [AVPlayerItem playerItemWithURL:saveLocationURL];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer:layer];
[self.player play];
Hope this will help you.

I am trying to play a video from URL which is playing in VLC media player but not playing in native player i.e MPMoviePlayer

It is showing like this in browser,when I click on play it doesn't do anything.
But the same URL is working fine in Android native player.
Here is my code and URL of that video stored in server, format of video is MP4.
I tried with some other URLs like
NSURL *url=[[NSURL alloc] initWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
NSURL *urll = [[NSURL alloc]initWithString:#"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];
These are working fine but below one is not working
NSURL *url = [[NSURL alloc]initWithString:#"http://****IP****.com:8888/alias_1440247177838"];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 20, self.videoPlayView.frame.size.width, self.videoPlayView.frame.size.height * (1.0f/3.0f)-20);
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
[self.videoPlayView addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
Unfortunately MPMoviePlayer can only play video from a direct link. (similar to the ones that you mentioned in your question.
You can either:
use a direct link
Or switch to UIWebView instead of MPMoviePlayer
You can use a UIWebView in following way:
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];
webView.allowsInlineMediaPlayback = YES;
NSURL* url = [NSURL URLWithString:#"http://****IP****.com:8888/alias_1440247177838"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Hope this helps!

How to play video player one after another in ios?

i want to play video in ios one after another..I have two video files .m3u8 format. if video one completed then video two start automatically.. after video two complete then again video one should played automatically. and again video two should start automatically. how can i do this thing….I am using Mediaplayer framework. plz give me any idea…..Thanks in advance.
-(void)viewDidLoad
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"buyTutorial" ofType:#“m3u8”];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self setController:moviePlayerController];
}
You should be able to use this notification:
https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/data/MPMoviePlayerPlaybackDidFinishNotification
And then look for this constant PlaybackEnded:
https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/tdef/MPMovieFinishReason
Then just start the next movie in your playback queue.
Here's some examples on how to register to the notification:
MPMoviePlayerPlaybackDidFinishNotification being called again in iPhone 4.3 simulator when setting contentURL

iOS: HTTP Live Streaming to app, Video won't play

I'm have some video files ready for streaming on my server. Here's the code I'm trying to use to play them in an iOS app:
Video *vid = [videos objectAtIndex:index];
NSURL *vidUrl = [NSURL URLWithString:vid.videoUrl];
NSLog(#"%#",vid.videoUrl);
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:vidUrl];
player.controlStyle=MPMovieControlStyleEmbedded;
[player.view setFrame:self.view.bounds];
[self.view addSubview:player.view];
[player play];
If I copy and paste the URL the NSLog spits out into Safari, the video plays fine. So I know the URL is good. But in my app, I just get a black screen. What's wrong with my code?
MPMoviePlayerController needs to be a strong property declared as so #property (strong, nonatomic) MPMoviePlayerController *mvpc;
Then whenever you want to play a movie you would write:
self.mvpc = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"movieURL"]];
self.mvpc.shouldAutoplay = YES; //Optional
self.mvpc.controlStyle = MPMovieControlStyleEmbedded;
[self.mvpc prepareToPlay];
[self.mvpc.view setFrame:self.view.bounds];
[self.view addSubview:self.mvpc.view];
[self.mvpc play];

MPMoviePlayerController video not playing

I used MPMoviePlayerController in my application.
I got the right URL but MPMoviePlayer does not play video it shows only black screen.
My code is as below:
[video setImage:[UIImage imageNamed:#"video_active.png"] forState:UIControlStateNormal];
NSString *filename=[NSString stringWithFormat:#"%#.mp4",[appdel.TempVideoUrl valueForKey:tag]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:filePath] ;
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(106, 18, 216, 235);
[uploadview addSubview:theMovie.view];
// Play the movie.
[theMovie pause];
I got the URL like this:
file://localhost/var/mobile/Applications/6E1D4CB7-A08D-438B-9FE7-E2FD9B7B5EEC/Documents/136_1355227629.mp4
Add [theMovie prepareToPlay]; before calling [theMovie play];.
prepareToPlay
Prepares a movie player for playback. (required)
- (void)prepareToPlay
Discussion
If a movie player is not already prepared to play when you call the
play method, that method automatically calls this method. However, to
minimize playback delay, call this method before you call play.
Calling this method may interrupt the movie player’s audio session.
For information on interruptions and how to resond to them, see Audio
Session Programming Guide. Availability
Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h
Also you can use isPreparedToPlay for checking whether a movie player is ready to play.
Please refer this link for more.

Resources