iOS AVPlayer does not start playing - ios

Its simple: My video player shows up, but never plays anything. The file I am trying to play exist already (checked).
I am just starting with a new "Single View" project.
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController ()
#property (nonatomic, strong) MPMoviePlayerController *player;
#end
#implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filepath = [NSString stringWithFormat:#"%#/Videos/fileSequence0.ts", documentsDirectory];
NSLog(#"!! %i", [[NSFileManager defaultManager] fileExistsAtPath:filepath]);
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[_player.view setFrame: CGRectMake(0, 0, 500, 500)];
[self.view addSubview:_player.view];
[_player play];
}
#end

Use below written code just before executing play statement.
_player.numberOfLoops=0;
_player.delegate=self;
[_player prepareToPlay];

.ts extension not works with ios. try some other extension videos ie. .mp4 . if problem arises then try to use [_player prepareToPlay];

try this one- Convert your url into NSdata
NSString* resourcePath = self.audioStr; //your video file formet should be .mp4
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error = [[NSError alloc] init];
NSLog(#"url is %#",resourcePath);
_player = [[MPMoviePlayerController alloc] initWithData:_objectData error:&error];
[_player.view setFrame: CGRectMake(0, 0, 500, 500)];
[_player prepareToPlay];
_player.controlStyle = MPMovieControlStyleFullscreen;
_player.shouldAutoplay = NO;
[_player setFullscreen:YES animated:YES];
[self.view addSubview:_player.view];
[_player play];
hope it will help u

Related

AVPlayerview comes from top left corner

I'm using AVPlayer to play videos. I have an issue on fullscreen mode. Video player comes from top left corner. It looks odd.
NSString *filePath = [self.video_Array objectAtIndex:index];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: nil];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
currentItem = [AVPlayerItem playerItemWithURL:url];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.videoGravity = AVLayerVideoGravityResize;
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = NO;
_video=[AVPlayer playerWithURL:url];
_video = [AVPlayer playerWithPlayerItem:currentItem];
_playerViewController.player = _video;
[_playerViewController.player play];
[self.view addSubview:_playerViewController.view];
Please refer my code which is working fine in my application,
-(void)videoPlayer:(NSString *)filePath{
playerViewController = [[AVPlayerViewController alloc] init];
self.viewPlayerContainer.frame = CGRectMake(0, 74, self.view.bounds.size.width, self.view.bounds.size.width);
NSURL *url = [NSURL fileURLWithPath:filePath];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;
[playerViewController.view setFrame:CGRectMake(0, 0, self.viewPlayerContainer.bounds.size.width, self.viewPlayerContainer.bounds.size.height)];
[playerViewController addObserver:self forKeyPath:#"videoBounds" options:NSKeyValueObservingOptionNew context:nil];
playerViewController.showsPlaybackControls = YES;
[self.viewPlayerContainer addSubview:playerViewController.view];
[player play];
}
You can use AVPlayerViewController to play fullscreen video
AVPlayerViewController+Fullscreen.h
#import <AVKit/AVKit.h>
#interface AVPlayerViewController (Fullscreen)
-(void)goFullscreen;
#end
AVPlayerViewController+Fullscreen.m
#import "AVPlayerViewController+Fullscreen.h"
#implementation AVPlayerViewController (Fullscreen)
-(void)goFullscreen {
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:self animated:YES completion:nil];
}
#end

MPMoviePlayerController Not playing Local file

NSString *path = [[NSBundle mainBundle] pathForResource:#"PTCL" ofType:#"mp4"];
NSURL *videoURL = [NSURL URLWithString:path];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayerController.view setFrame:self.view.frame];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.controlStyle=MPMediaTypeAnyVideo;
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
NSString *path = [[NSBundle mainBundle] pathForResource:#"PTCL" ofType:#"mp4"];
path=[NSString stringWithFormat:#"file:/%#",path];
NSURL *videoURL = [NSURL URLWithString:path];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayerController.view setFrame:self.view.frame];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.shouldAutoplay=YES;
moviePlayerController.controlStyle=MPMediaTypeAnyVideo;
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
file:///Users/utkal/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F0B3AD63-7E46-4069-8845-8B0C05425CD2/CosMos.app/PTCL.mp4
I am getting this videURL path for first code
file:///Users/utkal/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F0B3AD63-7E46-4069-8845-8B0C05425CD2/CosMos.app/PTCL.mp4
I am getting this videURL path for second code.
I have alos use
NSURL *videoURL = [[NSURL alloc]init];
videoURL = [[NSBundle mainBundle] URLForResource:#"PTCL" withExtension:#"mp4"];
But my video player always show that file is loading and nothing happen.
I know some where I am doing mistake,but hard luck of mine.
please correct my mistake or tell me if any another way to play local video file.Please.
maybe is too late but i would try something like:
MPMoviePlayerViewController *movieVC = [[MPMoviePlayerViewController alloc] initWithContentURL:file.location];
movieVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
movieVC.moviePlayer.fullscreen = YES;
movieVC.moviePlayer.allowsAirPlay = YES;
movieVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:movieVC];
notice the "movieVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;"
I used this simple solution instead:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"]];
MPMoviePlayerViewController* viewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:viewController];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"CapturedMedia"];
NSString *filePath = [dataPath stringByAppendingPathComponent:#"/itemVideo.mp4"];
_moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[_moviePlayer moviePlayer] prepareToPlay];
[[_moviePlayer moviePlayer] setShouldAutoplay:YES];
[[_moviePlayer moviePlayer] setControlStyle:2];
[[_moviePlayer moviePlayer] setRepeatMode:MPMovieRepeatModeNone];
[self presentMoviePlayerViewControllerAnimated:_moviePlayer];
you set the property of the MPMoviePlayerViewController in .h file.
MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
Hope this Helps..
Your code for movie player is looking fine . There is something wrong with the path . Just try to go to the path explicitly . And check whether that file is present there or not. You can see following SO question for your reference :
How to play video using MPMoviePlayerController?
A little late in the game but here's my complete code
In *.h
#interface v1SupportTable : UITableViewController
{
MPMoviePlayerController *moviePlayer1;
}
In *.m
- (IBAction) playVideoBtn
{
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sample_movie" ofType:#"mp4"]];
NSLog(#"videoURL: %# ...", videoURL);
moviePlayer1 =[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[moviePlayer1 view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [moviePlayer1 view]];
[moviePlayer1 setFullscreen:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[moviePlayer1 play];
}
-(void)playMediaFinished:(NSNotification*)theNotification
{
moviePlayer1=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer1];
[moviePlayer1.view removeFromSuperview];
//when finished dimiss window
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
-(void)doneButtonClicked
{
//[self.navigationController setNavigationBarHidden:NO animated:NO];
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}

Video Player Not Loading the File

I am trying this to load the video from the Project directory , can any one suggest , what exactly i am missing .
NSURL *myURL =[[NSBundle mainBundle] URLForResource:#"US_Very_High_Dive_Boudia_US_44_x264"
withExtension:#"mp4"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player prepareToPlay];
[player shouldAutoplay];
[player allowsAirPlay];
[self.view addSubview:player.view];
[player setFullscreen:YES animated:YES];
player.controlStyle=MPMovieControlStyleEmbedded;
Try this code
In .h file add the following
#property (nonatomic, strong) MPMoviePlayerController *controller;
In .m file
-(IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"buyTutorial" ofType:#"mov"];
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];
}
Seems like You are not setting URL properly - try this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"filename" ofType:#"mp4"]];

IOS: AVAudioPlayer management memory

I have this method to play an mp3:
- (void) playSound:(NSString*)sound{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
sound];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[player prepareToPlay];
[player play];
}
everytime I call this method I get a new string "sound" and then I must alloc everytime this AVAudioplayer "player"; I want release it when I stop it or when it finish...is it possible?
Try the below instruction
yourViewController.h
#interface yourViewController : UIViewController <AVAudioPlayerDelegate>
yourViewController.m file for add function
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
//play finished
}
Declare the AVAudioPlayer Object below code
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"audio" ofType:#"mp3"]] error:NULL];
audioPlayer.delegate = self;
audioPlayer.currentTime=0.0f;
[audioPlayer prepareToPlay];
[audioPlayer play];
Thanks..!
Take a look at AVAudioPlayerDelegate.
It has the methods audioPlayerDidFinishPlaying:successfully: and audioPlayerDecodeErrorDidOccur:error: that should let you do what you want.

Playing wav sound file with AVFoundation

I am using AVFoundation to play wav files. But i could not make it play. Also no errors or warnings showed up.
XCode is 4.2 and device is iOS 5.
- (IBAction) playSelectedAlarm:(id)sender {
UIButton *button = (UIButton *)sender;
int bTag = button.tag;
NSString *fileName = [NSString stringWithFormat:#"%d23333", bTag];
NSLog(#"%#", fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
theAudio.volume = 1.0;
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];
}
Here is the solution;
I set AVAudioPlayer in header file..
#property (nonatomic, retain) AVAudioPlayer *theAudio;
i guess 'retain' is solved my problem. Because after i sending "play", it sends "release" to balance the alloc. When AVAudioPlayer is deallocated it stops playing audio.
I have had the same issue with Xcode 4.5. Making the AVAudioPlayer into a strongly typed property made it play.
So the following code needs added to the #interface:
#property (nonatomic, strong) AVAudioPlayer *theAudio;
The #implementation would then become:
- (IBAction) playSelectedAlarm:(id)sender {
UIButton *button = (UIButton *)sender;
int bTag = button.tag;
NSString *fileName = [NSString stringWithFormat:#"%d23333", bTag];
NSLog(#"%#", fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
self.theAudio.volume = 1.0;
self.theAudio.delegate = self;
[self.theAudio prepareToPlay];
[self.theAudio play];
}

Resources