Unable to play MP4 video file from mainBundle - ios

So I'm trying to play a simple intro animation video file that I've dragged into my project in XCode and therefore should be able to play from my mainBundle, right?
With this code:
NSURL *urlString = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"introvideo" ofType:#"mp4"]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player play];
I get this error message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
Any help would be great!

This means your code can't find your introvideo.mp4 file. Make sure you have successfully add that file to your bundle. You can check in your project's setting: Copy Bundle Resource.

Your code isn't correct. Please change accordingly to the following. Still copy your video into your bundle resources.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"MacBook Pro with Retina Display" ofType:#"m4v"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release];
playercontroller = nil;
NSLog(#"Video is Playing");

Xcode 9
Here is an updated image.
Go to Build Phases > Copy Bundle Resources and see if your file is there. If it isn't you can add it by pressing the "+" button.

It seems that pathForResource:ofType: returns nil. Check that
this file is indeed added to "copy resources" build phase;
you didn't make mistake in the name of file - paths on device are case-sensitive.

You need To Check whether That Video Available in your Application's Resource Bundle.
As You mentioned
you getting this error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter.
It simply indicate that problem is in the resourcePath,means there is no such file exist there in Resource Bundle.that's Why that pathForResource returns nil path.
You need to put That Video File Again and Make Sure that file Exist in In Resource Bundle.
Then You should Go AHead with Code as Rehan Also posted.
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov" inDirectory:#""]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];
I hope It may worth full to you.
Thanks

NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov" inDirectory:#""]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];

I had put an entire "Sounds" directory into the project, and added it to the "Copy bundle resources" section. It was working fine, but then it started crashing.
The fix was to prepend the directory name to the filenames. Eg, this didn't work:
SKAction.playSoundFileNamed("crash.caf", waitForCompletion: false)
... but this did work:
SKAction.playSoundFileNamed("Sounds/crash.caf", waitForCompletion: false)

Related

MPMoviePlayerController set unknown text

I use the MPMoviePlayerController for playing video. It works, but it displays unknown strings which I think it is related to its localization files:
The code for playing video is here:
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"m4v"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[moviplayer prepareToPlay];
[moviplayer.view setFrame: self.view.bounds];
[self.view addSubview: moviplayer.view];
moviplayer.fullscreen = YES;
moviplayer.shouldAutoplay = YES;
moviplayer.repeatMode = MPMovieRepeatModeNone;
moviplayer.movieSourceType = MPMovieSourceTypeFile;
[moviplayer play];
How to solve this problem?
After looking to all part of my project, I find that the below class (category) which is used to set the default language for the application, yields to this bug:
https://github.com/cmaftuleac/BundleLocalization
Use this forked class solved my problem by not use swizzling method for UIKit bundle:
https://github.com/jbolter/BundleLocalization
https://github.com/jbolter/BundleLocalization/commit/4eedcf45a315a4a5f3ddaf9521f3cadec6632bcc

Error with a button performing multiple actions

I am trying to play audio and switch views with the same button..
- (IBAction)yourbuttonClicked:(UIButton *)sender
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"tap2" ofType: #"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
//for Switch view
ViewController *nextView = [[ViewController alloc] initWithNibName:#"ViewController2"
bundle: Nil];
[self.navigationController pushViewController:nextView animated:YES];
}
2014-03-02 19:07:46.817 Balloon Tap[847:70b] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string
parameter'
When I launch my simulator and click on the play button that performs the action it crashes and in my log it comes up with this error above..
How do I go about fixing this and editing my code?
It appears soundFilePath is Nil
Try this
NSURL *url = [NSURL fileURLWithPath:stringPath];
This removes the alloc, which may cause you some issues here. Also I'm sure you've checked, but is there an issue with the filename? Does it actually exist in your bundle? I.e is it being compiled with the rest of your code? Make sure that you add that file to your project, and it appears in a "Copy Bundle Resources" build phase in your executable's target in Xcode.

MPMoviePlayerController not playing local video

My video did not start to play in simple project
So I tried some examples to incorporate in the project.
First of all I added two frameworks MediaPlayer and MobileCoreService and imported .h file
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
Then added properties
#property (copy, nonatomic) NSURL *movieURL;
#property (strong, nonatomic) MPMoviePlayerController *player;
And last step was adding to viewDidLoad next code
_movieURL = [NSURL URLWithString:#"Movie.m4v"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:_movieURL];
_player.view.frame = CGRectMake(0 , 0, 400, 300);
_player.controlStyle=MPMovieControlStyleNone;
_player.scalingMode = MPMovieScalingModeAspectFill;
[self.view addSubview:_player.view];
[_player play];
Movie.m4v file in my root folder of application.
Also I'm trying to start it with help of action button, but code is working with
[_player play];
but I get a black rectangle, and if I understand right my addSubview part is working
Searching another examples but almost find same realisation.
http://photokarma.bl.ee/ios/VideoTest.zip archive with src code and file
Try This code..
NSString *path = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v" inDirectory:nil];
NSURL *movieURL = [NSURL fileURLWithPath:path];
player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];
Source Code:http://ishare-edu.blogspot.in/2012/10/how-to-play-audio-and-video-in-iphone.html
Hope it works...
Please resolve this line in Your code.
_movieURL = [NSURL URLWithString:#"Movie.m4v"];
if this is an http url then write complete like http://www.yourserver.com/../../Movie.m4v
or you accessing this video from locally within your iOS App then provide the full path like
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Movie" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
then after finding the movie url you can init your player like
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];
As per your Comment do you have any idea why i get urlpath nil?
Check that:
The Movie file exists in your copy of the source code.Also review that Xcode project has a reference to that file (if it's red, it means the file isn't actually present)
In your target in Xcode, look at "Build Phases" and reveal the "Copy Bundle Resources" build phase. That file should be present. If it isn't, press the + button and select it.
As per your given code i resolved and found this bug as i also discussed.
Tick both
Add To Targets Your Project Name
Copy item into your destination group
So you need to remove the video file and add it again with these both tick.
Try in Simple steps:
Step 1: Import MediaPlayer framework #import <MediaPlayer/MediaPlayer.h>
Step 2: Set delegate in .h file UIViewController<MPMediaPlayback>
Step 3:
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *fileLocation= [bundle pathForResource:#"file_video" ofType:#"m4v"];
NSURL *vedioURL =[NSURL fileURLWithPath:fileLocation];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
-(void)endSeeking
{
[self.navigationController popViewControllerAnimated:NO];
}
-(void)stop
{
[self.navigationController popViewControllerAnimated:NO];
}

AVQueuePlayer only plays my sequence of sounds once when clicking on button the first time

I've setup an AVQueuePlayer named player in my viewDidLoad method. I also have a UIbutton method which plays that sequence.
here is my view did load method
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"first" ofType:#"mp3"]]];
AVPlayerItem *secondSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"second" ofType:#"mp3"]]];
AVPlayerItem *thirdSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"third" ofType:#"mp3"]]];
player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstSound, secondSound, thirdSound, nil]];
and here is my button's method
-(IBAction)sequencePlay:(id)sender {[player play];}
pressing the button only produces the sequence of sounds once, when the view is first loaded. when i click the button again nothing happens. however if i navigate to another view controller and back to this view controller, it will work the first time i press the button again but as before, it only works for the first click.
update:
doing the setup in the button instead of the viewDidLoad method was the key as according to the first answer to solve my problem.
My mp3 files are small, each file is just one word being spoken. I have a UIPickerView setup with 3 columns, and depending on which rows are selected, it will play those specific words in sequence when the button is pressed. Any good advice on how i may accomplish that?
i'm currently trying to do this but get an error
-(IBAction)sequencePlay:(id)sender
{
NSString *sayFirst = _firstRowWord.description;
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sayFirst.description ofType:#"mp3"]]];
i get the following error
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
the description of the firstRowWord is the same exact name as the mp3 file i want to play
You could try using the sequencePlay: method to setup the AVQueuePlayer there.
So:
-(IBAction)sequencePlay:(id)sender {
AVPlayerItem *firstSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"first" ofType:#"mp3"]]];
AVPlayerItem *secondSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"second" ofType:#"mp3"]]];
AVPlayerItem *thirdSound = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"third" ofType:#"mp3"]]];
player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstSound, secondSound, thirdSound, nil]];
[player play];
}
If you are concerned about the performance of this you could use the actionAtItemEnd property to repopulate the Player.
Hope this helped!

iOS App: playing mp4 file from mainBundle crashes

I am trying to play test.mp4 from inside my App, but it crashes with the following error:
2013-06-28 15:02:40.931 framing[9617:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(0x1a5f012 0x176ce7e 0x1a5edeb 0x11839b1 0x118393b 0x28c2 0x1780705 0x6b42c0 0x6b4258 0x775021 0x77557f 0x7746e8 0x6e3cef 0x6e3f02 0x6c1d4a 0x6b3698 0x2ac1df9 0x2ac1ad0 0x19d4bf5 0x19d4962 0x1a05bb6 0x1a04f44 0x1a04e1b 0x2ac07e3 0x2ac0668 0x6b0ffc 0x223d 0x2165)
libc++abi.dylib: terminate called throwing an exception
(lldb)
The following are the codes:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#implementation ViewController
#synthesize moviePlayer,movieScreen,viewVideoTitleBg;
-(IBAction)playTheMovie {
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerController
alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
//[self presentMoviePlayerViewControllerAnimated:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
moviePlayer.view.frame = CGRectMake(64,192,895,415);
[self.view addSubview:moviePlayer.view];
//[moviePlayer setFullscreen:NO animated:YES];
[moviePlayer play];
}
I have got the Frameworks installed too.
What is wrong with my code?
I had faced this issue recently. My work around was to delete the video file from the bundle and drag-drop it again.
Clean and build and then run it again.
But for instance: You should check the spelling and capitalisation of the name of the video file and the one used in your code.
Try using [NSURL URLWithString : path]; instead of [NSURL fileURLWithPath:path];
check if this
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"]
return nil.
if this return nil, you need to add the mp4 file to xcode project and SELECT >ADD TO TARGET< checkbox.

Resources