mp4 video uploaded from android not play on ios - objective-c

We have an app that allows user to upload videos and images to server.
if i upload a mp4 video from android , it is not playing in ios devices.
this is my code. player status shows up readytoplay but it doesn't load up and no error comes up. vieo is playing fine with web and android.
here is video url : https://justgolive.net/assets/uploads/newrecordings/20919/19777983355a28e15326df6.mp4
NSURL *streamURL = [NSURL URLWithString:_videoUrl];
player = [AVPlayer playerWithURL:streamURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame =self.previewView.frame;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height/1.2);
[self.previewView.layer addSublayer:playerLayer];
// [self.playerContainer setUserInteractionEnabled:NO]; //to avoid touch events when activity is on
[self.player addObserver:self forKeyPath:#"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];

I have tried with your url of Video and working with below code:
header file like this:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *playerView;
#end
Implementation file like this:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (nonatomic) AVPlayer *player;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"https://justgolive.net/assets/uploads/newrecordings/20919/19777983355a28e15326df6.mp4"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
CALayer *superlayer = self.playerView.layer;
self.player.volume = 20.0;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
[playerLayer setFrame:self.playerView.bounds];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
playerLayer.frame = self.playerView.layer.bounds;
[superlayer addSublayer:playerLayer];
[self.player seekToTime:kCMTimeZero];
[self.player play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Related

Seamless Loop Video

I have seen lots of examples to seamless loop a video in iOS, but I haven't found much that I can get working in MacOS. I am just experimenting with AVPlayer looping a video in a window. I can get the video to play and loop, but I can't get it to be seamless.
I have an example here that plays a 3 second video on loop and you can clearly see it isn't a seamless loop.
I know there is an AVPlayerLooper but I'm very new and I don't know how to implement that.
#import "AppDelegate.h"
#import "VideoWindow1.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#interface AppDelegate ()
#property (weak) IBOutlet AVPlayerView *playerView;
#property (weak) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSWindow *VideoWindow;
#end
#implementation AppDelegate
AVPlayer *player;
BOOL loopPlayer;
- (void) movieEndDetected:(NSNotification *) note
{
if (loopPlayer) {
[player seekToTime:kCMTimeZero];
[player play];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
loopPlayer = YES;
// set up player
NSBundle *mb = [NSBundle mainBundle];
NSURL *demoURL = [mb URLForResource:#"Video2" withExtension:#"mp4"];
player = [[AVPlayer alloc] initWithURL:demoURL];
self.playerView.player = player;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(movieEndDetected:)
name:#"AVPlayerItemDidPlayToEndTimeNotification"
object:player.currentItem];
[player play];
[_VideoWindow setAspectRatio:NSMakeSize(16, 9)];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
I was able to get this to loop seamless with avlooper.
#import "VideoWindow.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#interface VideoWindow ()
#property (strong) IBOutlet AVPlayerView *playerView;
#property (strong) IBOutlet NSWindow *aspectView;
#end
#implementation VideoWindow
{
AVPlayerItem *_playerItem;
AVQueuePlayer *_player;
AVPlayerLooper *_playerLooper;
AVPlayerLayer *_playerLayer;
}
- (void)windowDidLoad {
[super windowDidLoad];
NSString *videoFile = [[NSBundle mainBundle] pathForResource:#"Video2" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoFile];
_playerItem = [AVPlayerItem playerItemWithURL:videoURL];
_player = [AVQueuePlayer queuePlayerWithItems:#[_playerItem]];
_playerLooper = [AVPlayerLooper playerLooperWithPlayer:_player templateItem:_playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = self.playerView.bounds;
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.playerView.layer addSublayer:_playerLayer];
[_aspectView setAspectRatio:NSMakeSize(16, 9)];
[_player play];
}
- (void)windowWillClose:(NSNotification *)aNotification {
[_player pause];
[_player seekToTime:kCMTimeZero];
}
#end

playerItemDidReachEnd moves to wrong ViewController

My app has a start page and a second page with buttons that play videos. When a video is finished the AVPlayer is dismissed with playerItemDidReachEnd. But for some reason it shows the start page and not the second page when the player is dismissed. How can I get it to show the second page instead?
#import "ViewController.h"
#import AVKit;
#import AVFoundation;
#interface ViewController ()
#property(nonatomic, readonly) AVPlayerItem *currentItem;
#end
#implementation ViewController
AVPlayerViewController *playerViewController;
- (void)viewDidLoad {
[super viewDidLoad];
playerViewController = [[AVPlayerViewController alloc] init];
}
-(void)playerItemDidReachEnd:(NSNotification *) notification {
//remove the player
[self dismissViewControllerAnimated:NO completion:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)playVideo:(id)sender {
NSURL *url = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
UIScreen *mainScreen = [UIScreen mainScreen];
CGRect viewRect = mainScreen.bounds;
playerViewController.view.frame = viewRect;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
/* When the player item has played to its end time we'll dismiss the controller */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerViewController.player.currentItem];
}
#end
If I understand your code correctly, your "second page" is ViewController.
You are adding AVPlayerViewController by [self.view addSubview:playerViewController.view];, so you need to remove this view by calling
[playerViewController.view removeFromSuperview].
After [self dismissViewControllerAnimated:NO completion:nil];, you dismiss whole ViewController, not only AVPlayerViewController.

Impossible to play a video with AVPlayer

I've been through all the posts in StackOverflow but I can't get a full example of how to get a video from the bundle and reproduce it in a controller.
I've tried many things but my screen appeared blank or black.
In the last amend, my video remains like this:
no error is thrown on the log. This is my code:
.h (please ignore the name of the controller, it will be finally a camera)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#interface NativeCameraViewController : UIViewController
#end
.m
#import "NativeCameraViewController.h"
#interface NativeCameraViewController ()
#property (nonatomic, retain) AVPlayerViewController *avPlayerViewcontroller;
#end
#implementation NativeCameraViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = self.view;
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"video" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:stringPath];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:fileURL];
self.avPlayerViewcontroller = playerViewController;
[self resizePlayerToViewSize];
[view addSubview:playerViewController.view];
view.autoresizesSubviews = TRUE;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) resizePlayerToViewSize
{
CGRect frame = self.view.frame;
NSLog(#"frame size %d, %d", (int)frame.size.width, (int)frame.size.height);
self.avPlayerViewcontroller.view.frame = frame;
}
#end
and that's what I've got so far.
My video is correctly on the Bundle, called video.mov:
I've tried both linking and copying. Same result.
What am I doing wrong??
Thank you very much in advance.
I'd like to avoid using MPMoviePlayerController since it's deprecated in iOS9
It was actually my fault...
I was trying to reproduce an alias.
When you have 20.000 videos and 30.000 resources this things happen.
Instead of using AVPlayer, use MPMoviePlayerController class:
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];

AVPlayer plays sound but view is not shown in mac application

I'd like to play local movie in AVPlayer on macintosh app. I've created simple code but movie doesn't want to show played movie. It plays sound of the movie but there is no view visible. What am I missing?
It's my first playing simple app.
My code:
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSView *containerView = [[NSView alloc] initWithFrame:CGRectMake(0.0f, 0, 390.0f, 400.0f)];
NSString *videoPath = [[NSBundle mainBundle] pathForResource: #"Fatality.mp4" ofType:nil];
NSLog(#"%#", videoPath);
NSURL *fileURL = [NSURL fileURLWithPath:videoPath];
self.player = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];
[self.player play];
}
#end
And AppDelegate:
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet MasterViewController *masterViewController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
self.masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.window.contentView addSubview:self.masterViewController.view];
self.masterViewController.view.frame = ((NSView*)self.window.contentView).bounds;
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
You have to add your containerView as a subview of the master view controllers view
[self.view addSubView:containerView];
A trick I often do while debugging is to set the background colour of a view I am interested in to something bright (red, yellow, cyan....) and then make sure that the view shows up where I expect it to be.

How to Open Youtube video in MPMoviePlayerController and Play it

i use this method before 1 time. but Now i can not get video from this URL that direct-play YouTube video in MPMoviePlayerController.
URL = http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded
- (void)viewDidLoad
{
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded"]]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play movie---
[player prepareToPlay];
[player pause];
player.view.frame = CGRectMake(0, 0, 320, 367);
[self.view addSubview:player.view];
[super viewDidLoad];
}
and My movieFinishedCallback is as Under ...
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}
Create a custom uiwebview: please follow instruction
1) Create a interface .h file named "YouTubeView.h"
#import <UIKit/UIKit.h>
#interface YouTubeView : UIWebView
{
}
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType;
#end
2) in to .m file
#import "YouTubeView.h"
#implementation YouTubeView
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType
{
NSString *strMimeType;
if([mimeType length]>0)
{
strMimeType = mimeType;
}
else
{
strMimeType =#"x-shockwave-flash"; //#"x-shockwave-mp4";
}
if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];
// HTML to embed YouTube video
NSString *youTubeVideoHTML = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/%#\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString,strMimeType, frame.size.width, frame.size.height];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
#end
3) Import the youtubeview file in the controller where you want to play you tube video and create uiview like the code below
YouTubeView *videoVw = [[YouTubeView alloc] initWithStringAsURL:[NSString
stringWithFormat:#"%#",strNormalVdoURL] frame:CGRectMake(0,0,320,480) mimeSubType:#"x-shockwave-
flash"];
[self.view addSubview:videoVw];
[videoVw release];
videoVw = nil;
Note: Video will play in device only so check in device
if possible to open your URL into UIWebview then take a button in your class and redirect it on to the WebsiteViewController class may be it helps u
WebsiteViewController.h ///----
#interface WebsiteViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *webView;
}
#property(nonatomic,retain) IBOutlet UIWebView *webView;
WebsiteViewController.m ///---
-(void)viewDidLoad {
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:#"url"];
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
[request release];
}