Puzzling Video Problem
I am makingan application that plays a random video when I make a sound. It works perfectly when I am using the iphone simulator on the Mac. However it does not work correctly on my ipodtouch basically when I make a sound it will play a random video but it will not play another video when I make a sound. But this problem does not occur on the simulator.
Does anyone have any ideas as to why? the code can be seen below
and merry christmas
Code:
#import <MediaPlayer/MediaPlayer.h>
#import "AskViewController.h"
@implementation AskViewController
@synthesize imageView;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else
NSLog([error description]);
}
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.65)
NSLog: [self micHadNoise];
//// micHadNoise is essential
}
- (void) micHadNoise {
int r = arc4random() % 9;
NSBundle *bundle = [NSBundle mainBundle];
NSString* movieName = [NSString stringWithFormat:@"me%d", r];
NSString *moviePath = [bundle pathForResource:movieName ofType:@"m4v"];
NSURL *movieURL;
if (moviePath)
{
movieURL = [NSURL fileURLWithPath:moviePath];
}
if (movieURL != nil) {
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.movieControlMode = MPMovieControlModeHidden;
moviePlayer.backgroundColor = [UIColor clearColor];
[moviePlayer play];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (void)moviePlayBackDidFinish:(NSNotification*)aNotification{
MPMoviePlayerController* theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie release];
}
@end