MPMoviePlayerViewController - sound Is heard, but no video is seen - xcode

The QuickTime player appears over my View Controller layer as it should and the video file "Happy" plays completely through, but no video shows. Only audio is heard. And after completion, the QuickTime player transitions back to previous View Controller as it should.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Happy" ofType:#"m4v"]];
MPMoviePlayerViewController * playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playerController.moviePlayer play];
[playerController release];
playerController=nil;

As far as my knowledge we cannot play the m4v files.Convert it to mp4

Related

Imageview And/OR Sound having memory Issue (going in back ground)

My Application is in ARC.
In any view of my application there is one back ground image and on that image some animations are continuously repeating the images.
Problem is : My application is going in background after some time.
I seen and tried Lots of thing on stack, google and on apple doc but didn't got satisfactory solution.
I have Three Question.
(1) How to Stop this ?
(2) Is there any other method to release the memory manually in ARC.
(3) If i use [self.view removeFromSuperview]; for UIView then also some memory didn't released in ARC ?
MyCode for Animation is
imageview = [[UIImageView alloc] init];
imageview.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy08" ofType:#"png"]];
[self.view addSubview:imgBubbleAnimation];
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy01" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy02" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy03" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy04" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy05" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy06" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy07" ofType:#"png"]],
nil];
[imageview setAnimationRepeatCount:0]; // 0 for continuous
imageview.animationDuration =3.0;
[imageview startAnimating];
And Sound files are (played when particular button clicked)
URL = [NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"ect1a" ofType:#"mp3"]];
audioSound = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil];
audioSound.delegate = self;
audioSound.volume = 5.0;
audioSound.numberOfLoops = 0;
[audioSound play];
I have 15 small sound files for one view and 15 animations for one view.
In every particular Animation there are min 15 images.
Any help, suggestion, link or tutorial are welcome.
Not using imageview.animationImages is the first thing you need to do to get your memory usage under control. That API should never be used as it lets you shoot yourself in the foot easily. See imageview-animation-getting-more-memory or unpredictable-crash-due-to-uiview-animations or iphone-app-memory-leak-with-uiimage-animation-problem-testing-on-device.

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 iPhone SDK - Show gif image on app Launch

I currently have a mp4 video converted from a gif playing at app launch, but using a video stops playing music and has problems with airplay devices connected.
Anyways what i really want is showing my gif on app launch. But i can't seem to get the gif to "play" it only shows one frame, how do i make it play the whole gif ?
My current code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Show Animation;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Launch" ofType:#"mp4"]];
LaunchPlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
LaunchPlayer.view.frame = self.view.bounds;
LaunchPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
LaunchPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
LaunchPlayer.view.tag = 1;
[self.view addSubview:LaunchPlayer.view];
[LaunchPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(LaunchFinish)
name:#"MPMoviePlayerPlaybackDidFinishNotification"
object:nil];
}
Although I don't see how the code you posted has anything to do with using a .gif, you its sticking to the first frame because iOS will not run an animated .gif. However, you can export each frame of your animation as a separate image and animate them like this.
-(void) viewDidLoad
{
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImageFrame1.gif"],
[UIImage imageNamed:#"myImageFrame2.gif"],
[UIImage imageNamed:#"myImageFrame3.gif"],
[UIImage imageNamed:#"myImageFrame4.gif"], nil];
imageView.animationDuration = 2.0;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
}
In my little example here. i used a UIWEBVIEW called webView and had spinning-loader.gif added as a resource. The image can me put anywhere in the project as long as its added. This showed the gif as well as made it animate. Hope this helps.
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"spinning-loader" ofType:#"gif"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html><body><img style='width:85;height:85;' src=\"file://%#\" /></body></html>", pathImg];
[webView loadHTMLString:webViewContent baseURL:nil];
webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.opaque=FALSE;
[webView setBackgroundColor:[UIColor clearColor]];

Getting trimmed video file size from UIImagePickerController

After selecting a video using UIImagePickerController I can get the video's file size from its
[defaultRepresentation size].
However, if I enable the picker's trimming option, [defaultRepresentation size] returns the same file size as the original untrimmed video.
Does anyone have a method for retrieving the trimmed video's file size?
Much appreciated...
Yes, I should have included some code. Me bad.
What I'm trying to achieve is have the user choose and trim an existing video, then upload the trimmed version. I would like to have the trimmed version's file size in order to populate the progress bar during the upload. I would also like the trimmed version's duration.
The calling method is:
-(IBAction)selectVideoPressed:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
[videoPicker setDelegate: self];
[videoPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[videoPicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil]];
[videoPicker setVideoQuality:vvi.videoQuality];
[videoPicker setAllowsEditing:YES];
[videoPicker setModalTransitionStyle:gTransitionStyle];
[self presentViewController:videoPicker animated:YES completion:Nil];
}
}
The delegate method which fires after the user has trimmed and pressed Choose is:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[vvi setRawSourceVideoFileURL:[info objectForKey:UIImagePickerControllerMediaURL]];
[vvi setSourceVideoURL:[info objectForKey:UIImagePickerControllerReferenceURL]];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset)
{
// Video metrics info.
[vvi setVideoDuration:[myAsset valueForProperty:ALAssetPropertyDuration]];
[vvi setVideoOrientation:[myAsset valueForProperty:ALAssetPropertyOrientation]];
NSDate *videoDate = [myAsset valueForProperty:ALAssetPropertyDate];
[vvi setVideoDateString:[videoDate descriptionWithLocale:[NSLocale currentLocale]]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:gShortDateFormat];
[vvi setShortVideoDateString:[dateFormatter stringFromDate:videoDate]];
ALAssetRepresentation *myAssetRepresentation = [myAsset defaultRepresentation];
[vvi setVideoAssetSize:[NSNumber numberWithLongLong:[myAssetRepresentation size]]];
NSLog(#"Duration:%#", vvi.videoDuration);
NSLog(#"Size:%#", vvi.videoAssetSize);
};
....
vvi is just my own custom class.
The two NSLog's return the duration and size of the original untrimmed video. The video pointed to with [vvi setRawSourceVideoFileURL:[info objectForKey:UIImagePickerControllerMediaURL]]; does return the properly trimmed video.
Thanks and much!
Something like this should take care finding the file size of a selected image or video returned from the UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
//Error Container
NSError *attributesError;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[videoUrl path] error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
}

How to mute and unmute audio in iPad by tapping a button?

I'd like to know how to make the mute and unmute the audio on my iPad application by tapping a button. I already have a working code in my viewDidLoad method that plays a music when the app is launched. All I need to do is to create a button that will stop the music from playing and also resume it when tapped again.
Here's my code in my viewDidLoad:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"Bored" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
[super viewDidLoad];
}
Just look at the AVAudioPlayer API - it's all there: play, pause, volume, etc.

Resources