playing video in Xcode after version 4.2.1 - xcode

It seems like after Xcode version 4.2.1 my video code won't work?
objective C code:
NSURL *url = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"h" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller =
[[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
// [playercontroller release];
playercontroller = nil;

Try instead of
[self presentMoviePlayerViewControllerAnimated:playercontroller];
[[self navigationController]presentMoviePlayerViewControllerAnimated:playercontroller];
If you are using navigation controller.

Related

Trying to add movie in xcode 4.6.1 but crash

I'm trying to add a video in xcode but it crashes: [NSURL initFileURLWithPath:]: nil string
(IBAction)Play:(id)sender {
/////
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:videoUrl]];
[player play];
}
Maybe if you try:
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
NSURL *url = [NSURL urlWithString:videoUrl];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:url];

xcode cannot get sound file to play with AVAudioPlayer

I am new to iOS and trying to add sound to my app. I have followed several tutorials about adding a simple sound file to my project, but ALL with the same result. I get no errors, but no sound plays. Any ideas on what I am doing wrong? Any help would be appreciated!
- (IBAction)playSound:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = 1;
[player play];
}
**UPDATE***
So I never had any luck with the above code which is very discouraging, but I did end up getting it to work using the AudioToolbox Framework instead, using this:
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:#"test" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
Can anyone explain to me the difference, why this one works and the other does not?? I am curious more than anything. Would love some clarification.
Finally figured it out. I needed to create a property for AVAudioPlayer in my header file in order to get it to work. Here is what I did:
// .h file
#interface ThirdViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void) playSound //method in .m file
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer play];
}
Simplify your code by doing the follow. Also drop your test.mp3 to the root of your project directory in xcode. Your mp3 placement and location is very important. Also ensure volume is up, and vibrate is off of your device, and it's unmuted.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
Make sure you install quartz.h, or a specific framework that supports audio players. Just double check.
Had the same problem a while back and the framework must be installed properly.

XCode - UIWebView Not Loading

I have two local .html files in the Resources folder. I'm trying to load them the following way, but only the final page loads. What am I doing wrong?
File = please_wait.html
This one does not work.
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"please_wait" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
//Big "do-while" loop here. It works fine so I omitted it.
File = update_graph.html
This one does not work
path = [[NSBundle mainBundle] pathForResource:#"update_graph" ofType:#"html"];
htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
//Lots of code removed. All works correctly and doesn't touch webview
This last one works perfectly. Google displays.
string = #"http://google.com";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
It appears from your comment that your UIWebView loads just fine, but it does not get a chance to refresh itself on the screen until you exit your method. It is not enough to set a break point inside the method and wait for the view to load: you must exit the method before iOS realizes that it needs to call UIWebView's drawRect method.
To fix this, split your method in three parts, A B and C, and set UIWebView's delegate in A to invoke B on webViewDidFinishLoad:, and the delegate in B to call C.
Here is how to implement this: start with a delegate that can call a selector when the loading has completed:
#interface GoToNext : NSObject <UIWebViewDelegate> {
id __weak target;
SEL next;
}
-(id)initWithTarget:(id)target andNext:(SEL)next;
-(void)webViewDidFinishLoad:(UIWebView *)webView;
#end
#implementation GoNext
-(id)initWithTarget:(id)_target andNext:(SEL)_next {
self = [super init];
if (self) {
target = _target;
next = _next;
}
return self;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:next];
#pragma clang diagnostic pop
}
#end
Now split your method into three parts - loading the first page, loading the second page, and loading the third page:
-(void)loadPleaseWait {
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"please_wait" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:#selector(loadUpdateGraph)];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
// big do-while loop
}
-(void)loadUpdateGraph {
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"update_graph" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:#selector(loadGoogle)];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
// Lots of code removed
}
-(void)loadGoogle {
string = #"http://google.com";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}

Can I add sounds to Xcode's Storyboard mode?

I'm new to creating apps for iOS and I'm using storyboard mode. I wanted to know how can I add sounds. Also, does it require coding?
You can use this for playback of simple sounds. It uses the AVFoundation framework, which will require you to add AVFoundation to your project, and use #import <AVFoundation/AVFoundation.h>
- (IBAction) myPlayAction {
AVAudioPlayer *data;
NSString *name = [[NSString alloc] initWithFormat:#"SoundName"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:#"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data prepareToPlay];
[data play];
}

View video in Landscape mode

I have a video in my application, but it's in portrait. I want to display it in landscape mode, but I don't have any idea how to do that.
I used this code to make my video:
- (IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"BAZO" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
- (IBAction)playSecondMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"00 01. Welcome" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
-(void) tableView: (UITableView*) tableView
didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"BAZO" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
/*-(void)loadVideo:(NSString *)name ofType:(NSString *)type{
NSURL *url=[NSURL fileURLWithPath:[mainBundle pathForResource:#"BAZO" ofType:#"m4V"]]
if (!mp) mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
[mp play];
}*/
}
Maybe somebody can give me a hint or a method so I can put this to landscape?
I used this code:
[MPMoviePlayerController setOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
but it gives me the warning :
MPMoviePlayerController may not respond to -setOrientation:animated:
What's happening?
Had this problem as well,
I was running it in the appDelegate file, which meant I had to force the window to change to landscape so if the original is like this:
-(void)playMovie {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"intro"
ofType:#"m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player setFullscreen:YES animated:YES];
[player setControlStyle:MPMovieControlStyleNone];
[window addSubview:player.view];
[window bringSubviewToFront:player.view];
[player setFullscreen:YES animated:YES];
[player setControlStyle:MPMovieControlStyleNone];
[player play];
}
all that is needed is to add these lines:
[window setBounds:CGRectMake( 0, 0, 480, 320)];
[window setCenter:CGPointMake(160, 240];
[window setTransform:CGAffineTransformMakeRotation(M_PI/ 2)];
before the [player play], and at the movie finish call, add
struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;
window =[[UIWindow alloc] initWithFrame:rect];
[window makeKeyAndVisible];
to reset the view.
Not sure if it's the right way to do it, but it was the only thing worked for me.
Use MPMoviePlayerViewController instead of MPMoviePlayerController. It will handle orientation, control style, etc. I referred this answer for the same.
MPMoviePlayerViewController *playerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:playerView];
and in the AppDelegate.m, declare the following function which will restrict the orientation to portrait in all cases and will only change when a video is playing
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
//NSLog(#"orientation should change");
return UIInterfaceOrientationMaskPortrait;
}
}

Resources