Objc - how to add a video player inside a UIView like this? - objective-c

This player only have 2 buttons,play/pause and fullscreen,it looks like a system player,so fast and simple.
I used UIWebView and HTML5 inside the UIView,but UIWebView is so slow and can not play without fullscreen.
If this player is a UIObject or some UIView things,how can I add it?
thanks~

The MPMoviePlayerController is deprecated from iOS 9.
You can insert a video into iOS project with AVPlayerViewController now.
For example:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
...
#property (nonatomic, retain) AVPlayerViewController *playerViewController;
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *stringVideoName = #"video.m4v";
NSString *stringVideoPath = [[NSBundle mainBundle] pathForResource:stringVideoName ofType:nil];
NSAssert(stringVideoPath, #"Expected not nil video file");
NSURL *urlVideoFile = [NSURL fileURLWithPath:stringVideoPath];
NSAssert(urlVideoFile, #"Expected not nil video url");
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = YES;
[self.view addSubview:_playerViewController.view];
self.view.autoresizesSubviews = YES;
}

You can add a video player into your UIView using MPMoviePlayerController.
Here is Apple sample code which does just like this.
It can stream a video from internet as well as play a local movie file.
You can change the playback controls by changing the moviePlayer controlStyle property.
You can find the sample code from the link below
http://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html

Improts Following frameworks
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <UIKit/UIKit.h>
Add following code in UIView Or UIViewController Class
videoView is UIView
// Url of Video
NSURL *videoURL = [NSURL URLWithString:#"urlOfVideo"];
// Init Player
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
// Add Player in AVPlayerViewController
playerViewController.player = player;
playerViewController.showsPlaybackControls = true;
// Set Frame accoding to view
playerViewController.view.frame = _videoView.bounds;
//Used to Play On start
[playerViewController.player play];
// Add Video in UIView
[_videoView addSubview:playerViewController.view];
I hope it will work for you...

Related

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.

Custom Segue doesn't Play Sound

I made a custom segue and I want it to play sound when changing views. Unfortunately, it doesn't. Method playSound works fine when I use it in View Controller. But i don't want to export it in every View Controller i need. How can i improve my code and why segue doesn't want to play sound?
#import "CustomSegue.h"
#import AVFoundation;
#interface CustomSegue()
#property (nonatomic, strong) AVAudioPlayer *player;
#end
#implementation CustomSegue
#synthesize player;
- (void) perform
{
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[src.navigationController pushViewController:dst animated:NO];
}
completion:NULL];
[self playSound];
}
- (void) playSound
{
int r = arc4random_uniform(7) + 1;
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%i", r] ofType:#"caf"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[self.player prepareToPlay];
[self.player play];
}
I bet your custom segue is released (by ARC) before the sound has a chance to play, and your audio player is released with it. The Life Cycle of a Segue is such that it is deallocated immediately after -performis executed.
What you need to do is keep the audio player around for a longer time. One way to do this is to add a (strong) reference to your AVAudioPlayer from an object that is not going to be deallocated for at least as long as the sound will play. For example, place the player in your app delegate, which sticks around as long as the app is running.
Try moving the player property and the -playsound method into your app delegate. The property should look something like:
#property (strong, nonatomic) AVAudioPlayer *player;
Then, in this segue, in place of [self playsound]; make a call to the app delegate with something like:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate playsound];

Sound not playing in iOS Simulator. No errors

I have constructed some code that is supposed to play audio when a button is clicked. There are no errors in the code, but when I open the iOS Simulator and press the button, no sound plays. The button does the tap animation, but nothing happens.
I have both the AVFoundation and AudioToolbox frameworks.
I also have my audio file in resources.
I am not using breakpoints.
The button is set to use the 'playAudio:' first responder upon Touch Up Inside.
Here is my 'ViewController.h' file:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
And here is my 'ViewController.m' file:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
sleep(5);
};
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The audio segment is about 3 seconds.
Any help is much appreciated!
Thanks!
I am quite new to ios but I had the same problem and was able to fix it.
I simply declared my AVAudioPlayer variable as a global variable and called
[audioPlayer prepareToPlay];
before
[audioPlayer play];
Just calling prepareToPlay didnt fix it, I had to declare the global variable to for it to work.
Not sure why that makes a difference to the Simulator but it works now.
Play sound/music file in objective-c,
I had the same problem and was able to fix it with below code.
.h file
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)actionOnPlayButton:(id)sender;
- (IBAction)actionOnStopButton:(id)sender;
- (void)playSound ;
#end
.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
AVAudioPlayer *audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Play Music";
}
-(void)playSound{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound10"
ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (IBAction)actionOnPlayButton:(id)sender {
[self playSound];
}
****Used above lines of code, it will work fine...
Note:- Don't forget to import following framework:-**
#import
#import

How to change video url for MPMoviePlayerViewController?

I am developing for iOS 6 using Xcode 4.6.1 (Target is iPad)
I am trying to change the video URL when the user swipes the screen.
This is my ViewController.m file:
#import "ViewController.h"
#interface experiencesViewController () {
MPMoviePlayerViewController *playerViewController;
MPMoviePlayerController *player;
}
#end
...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *url = [[NSBundle mainBundle] pathForResource:"video" ofType:#"mp4"];
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
player = [playerViewController moviePlayer];
[player setMovieSourceType:MPMovieSourceTypeFile];
player.fullscreen = YES;
[player play];
...
}
...
Now, I want to change the video on a swipe event. I tried doing this:
- (IBAction)didSwipeLeft:(UITapGestureRecognizer *)recognizer {
NSLog(#"Left");
player.movieSourceType = MPMovieSourceTypeFile;
player.contentURL = [NSURL URLWithString:#"video2.mp4"];
[player prepareToPlay];
[player play];
}
However, this doesn't work. The video stops playing and nothing happens after that. Any help would be highly appreciated.
Thanks :)
remove the [player play] because the video is not yet ready to play.