Use MPMediaPicker selections as AVAudioPlayer inputs - iphone

I'm programming an application for the hearing-impaired. I'm hoping to take tracks from the iTunes library, and in my app have a slider for panning. I don't want to use OpenAL (this isn't a game - I repeat this is a media player). So since AVAudioPlayer has the easy pan method, can I take selections from the MPMediaPicker and feed them into the AVAudioPlayer so I can pan them?

I dont do a lot of iOS development, but I believe there are two ways.
Method #1
You need to add /System/Library/Frameworks/AVFoundation.framework to your target in Xcode and #import AVAudioPlayer.h as well as You need to add MediaPlayer.framework to your target in Xcode and #import .
For this operation, you need MPMediaPicker to pass the song data to AVAMedia Player. That can be accomplished like this:
#interface MusicPlayerDemoViewController : UIViewController <MPMediaPickerControllerDelegate> {
...
}
...
// This action should open the media picker
- (IBAction)openMediaPicker:(id)sender;
#end
// MusicPlayerDemoViewController.m
- (IBAction)openMediaPicker:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO; // this is the default
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
}
// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
(Code Continues Below, the blanks are for you to fill in)
AT THIS POINT, CALL THE AVAAUDIOPLAYER CLASS AND TELL IT TO PLAY mediaItemCollection . REMEMBER TO STOP AUDIO BEFORE PLAYING, AS IT WILL PLAY MULTIPLE SONGS AT ONCE.
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
// User did not select anything
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
}
NOW, ONCE THIS IS DONE THE USER NEEDS TO SELECT A NEW SONG. YOU COULD EITHER CREATE A WHILE LOOP AROUND THE WHOLE THING, WHERE THE CONDITIONAL IS CURRENT TIME >= DURATION (FROM AVAAUDIO PLAYER),
ALTERNATIVELY, YOU COULD CREATE A BUTTON TO OPEN THE PICKER
For more questions checkout:
http://oleb.net/blog/2009/07/the-music-player-framework-in-the-iphone-sdk/ (I used much of their code)
http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
Good Luck!
Derek

Try having AVAMediaPlayer play the variable mediaItemCollection. This was assigned by the picker to be the song location by the code above. If this does not work make sure that AVAMediaPlayer uses the same input variable type (format, like an ID or a folder location) as MpMediaPicker.
That error message sounds like a technical issue. The only thing I can think of is that AVAAudio player or MPmedia player is looking for a Volume variable (it is required?) and can't find one. I can't really answer this one as I don't do iPhone Development, try there forums or website for some help.
Sounds like you are doing a good job! If you are interested, (I don't know if you are staying at DA) Mr. Cochran (the dean of students at the Upper School) is teaching a iPhone Development Class and a AP Computer Science Class (I am in). If you want to take it further, or you want to just ask questions I know he is more than happy too!
Good Luck! Tell me when it is finished so I can test the results!

Related

UIImagePickerController CameraDevice picks wrong device every other time (using ARC)

My app takes a video (using a custom overlay, in case that's relevant) where I have the cameraDevice set to front facing camera. 3 videos need to be taken, though the cameraDevice alternates between front and rear each time the UIImpagePickerController is called to take the video. On each occasion the
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
is used within the code, though as mentioned the app ignores this command every other time.
I see this problem has been asked before, though I am using ARC so the previously suggested solutions of releasing the UIImagePickerController is not an option for me.
Thanks in advance, Jim.
You cannot release the picker in ARC but you can still create a brand new one like this:
picker = nil;
picker = [[UIImagePickerController alloc] init];
// configure picker
Notice that in Apple's sample code "Using UIImagePickerController to Select Pictures and Take Photos" they call a method each time which does exactly this.

Reopening AVCaptureSession

I have an application which takes some pictures.
My whole application is based on the AVCam sample code from WWDC 2010.
I've messed with it a lot and yet, up until now I can't figure out how to release the camera view properly which releases the camera session...
All i'm trying to do is the following:
Open camera view controller
Take some photos
Close camera view Controller
Open it again
The second time I push the viewController the session is lost, preview is not available and capturing is not available as well. I've published full example code on github.
My workaround for the issue was not to release the camera at all so the Camera View Controller acts as a Singleton, which I think is not the right way. moreover, with this behavior I couldn't figure out a way to support camera when application went to the background (phone call for example).
Please advice. How do I destruct the camera session? and is it important to do so?
I've added the following message to AVCamCaptureManager
- (void) destroySession {
if ([delegate respondsToSelector:#selector(captureManagerSessionWillEnd:)]) {
[delegate captureManagerSessionWillEnd:self];
}
// remove the device inputs
[session removeInput:[self videoInput]];
[session removeInput:[self audioInput]];
// release
[session release];
// remove AVCamRecorder
[recorder release];
if ([delegate respondsToSelector:#selector(captureManagerSessionEnded:)]) {
[delegate captureManagerSessionEnded:self];
}
}
I'm calling destroySession when the viewController holding the camera get close (on my example it's -closeCamera: of AVCamViewController).
For the full working example, you're welcome to download AVCam-CameraReleaseTest on github.com
G
I think that may help you have a look .
http://red-glasses.com/index.php/tutorials/ios4-take-photos-with-live-video-preview-using-avfoundation/

iPhone Datepicker plays 'tick' sound

Does any of you know what event I can tap into on the DatePicker when the wheel moves.
I want to play a sound (got sound code) as the wheel spin. Just like the timer set picker in Apple's clock app.
All that is there is a single event for ValueChanges which only fires once, at the end of a wheel spin.
Thanks in advance
I'm not sure this is possible, as the sounds seem to be linked to the Keyboard Clicks in the phone settings. Maybe someone else has a solution, but it doesn't seem to be documented anywhere.
There is an undocumented method for doing this:
Disable UIPickerView sound
#import <UIKit/UIKit.h>
#interface SilentUIPickerView: UIPickerView
{ }
- (void) setSoundsEnabled: (BOOL) enabled;
#end
use this subclass and call [view setSoundsEnabled: NO]
the reason behind this is when your controller is load the datepickr is scrolling so in view did load
write this code.
NSDate *d = [[NSDate alloc] init];
[objDatePicker setDate:d animated:NO];

Callback for camera shutter open event

I have been working around in UIImagePickerController and am struck with a problem where I need to get the precise moment when the camera shutter opens in UIImagePickerController when the source type is set to camera (UIImagePickerControllerSourceTypeCamera).
I have done some googling around and have realized that no one had such strange requirement!
I looked around the docs of UIImagePickerController and UIImagePickerControllerDelegate hoping to get some delegate method / callback indicating the camera shutter open event, but did not find any.
Any suggestions?
Thanks for any help,
Raj Pawan
Subscribe to:
AVCaptureSessionDidStartRunningNotification
This is when the iris open animation begins. If you add a cameraOverlayView during this time, it will be properly covered up by the iris. It is posted at the same time as that PL… private notification. This is a documented approach that does not risk app rejection.
The solution is to observe for a private notification, just as discussed in this thread:
Detecting when camera's iris is open on iPhone.
My sugestion (didn't try it myself but don't see why it wouldn't work) :
Subclass the UIImagePickerController class into your own class, and override the viewWillAppear and viewDidAppear : those methods will be triggered when the UIImagePickerController is about to appear or does appear (if you trigger its display properly using presentModalController for instance) .
#interface MyUIImagePickerController : UIImagePickerController {
}
#end
#implementation MyUIImagePickerController
-(void)viewWillAppear:(BOOL)animated {
NSLog(#"the picker is about to appear");
[super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated {
NSLog(#"the picker did appear");
[super viewDidAppear:animated];
}
#end

iPhone: taking a picture programmatically

I'm trying to use the UIImagePickerController interface from OS 3.1, with the cameraOverlayView and takePicture, but I've clearly failed to understand how this works, and so I'm not getting the behaviour I want.
What I want to do is open the camera and take a picture automatically without having to having the user interact with the picker or edit the image. So I subclass UIImagePickerController (similar to the example in http://github.com/pmark/Helpful-iPhone-Utilities/tree/master/BTL%20Utilities/) and turn off all of the controls:
- (void)displayModalWithController:(UIViewController*)controller animated:(BOOL)animated {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.toolbarHidden = YES;
// Setting the overlay view up programmatically.
ipView = [[ImagePickerView alloc] init];
self.cameraOverlayView = ipView;
[controller presentModalViewController:self animated:NO];
}
In the overlayView, I've managed to force the takePicture method of UIImagePickerController to fire (I know this, because I can NSLog it, and I hear the sound of the camera taking a picture). The overlayView shows up just fine. However, the delegate method didFinishPickingMediaWithInfo: never gets called, and imagePickerControllerDidCancel doesn't get called either.
So, how do I either get the delegate methods to get called, or save the picture by overriding the takePicture method? (I have no idea how to capture the picture data here, and Google seems to have failed me). I can't help feeling that I've failed to understand how the guts of UIImagePickerController works, but the docs aren't overly helpful:
e.g.:
"You can provide a custom overlay view to display a custom picture-taking interface and you can initiate the taking of pictures from your code. Your custom overlay view can be displayed in addition to, or instead of, the default controls provided by the image picker interface."
or from showCameraControls:
"If you set this property to NO and provide your own custom controls, you can take multiple pictures before dismissing the image picker interface." - How do I dismiss the picker interface?
Note: the delegate is set properly in IB, so that's not the problem.
Thanks for any help you can provide!
I've found that you just have to wait "long enough" before calling takePicture, or it just silently fails. I don't have a good answer for how to determine the minimum value of "long enough" that will always work, but if you set a timer and wait five or ten seconds you should be okay. It would be nice if it returned some kind of an "I'm not ready to take a picture yet, sorry" error either directly from takePicture or through the delegate, but as far as I know it doesn't.
As an update to my own question: It turns out that I was trying to use takePicture too early. When I moved the action to a button on the overlay and sent takePicture from that button (once the picker was presented modally), the delegate methods fired as they should. I don't know if what I wanted is achievable - taking the image without having to press that button, automatically - but if it is, it will probably have to be done by sending takePicture sometime after I was trying to use it.
-(void)imageMethod:(id)sender{
imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePopover=[[UIPopoverController alloc]initWithContentViewController:imagePickerController];
[imagePopover presentPopoverFromRect:importButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}