Permalink
Browse files

Initial import from filesystem — got iPod library export working!

  • Loading branch information...
1 parent 8696107 commit e751b713cf0343a26f993d6b545fdf0487506972 agillesp@gmail.com committed Jun 19, 2010
@@ -0,0 +1,22 @@
+//
+// iPodLibraryAccessAppDelegate.h
+// iPodLibraryAccess
+//
+// Created by Art Gillespie on 6/11/10.
+// Copyright Glaresoft 2010. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class iPodLibraryAccessViewController;
+
+@interface iPodLibraryAccessAppDelegate : NSObject <UIApplicationDelegate> {
+ UIWindow *window;
+ iPodLibraryAccessViewController *viewController;
+}
+
+@property (nonatomic, retain) IBOutlet UIWindow *window;
+@property (nonatomic, retain) IBOutlet iPodLibraryAccessViewController *viewController;
+
+@end
+
@@ -0,0 +1,88 @@
+//
+// iPodLibraryAccessAppDelegate.m
+// iPodLibraryAccess
+//
+// Created by Art Gillespie on 6/11/10.
+// Copyright Glaresoft 2010. All rights reserved.
+//
+
+#import "iPodLibraryAccessAppDelegate.h"
+#import "iPodLibraryAccessViewController.h"
+
+@implementation iPodLibraryAccessAppDelegate
+
+@synthesize window;
+@synthesize viewController;
+
+
+#pragma mark -
+#pragma mark Application lifecycle
+
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
+
+ // Override point for customization after application launch.
+
+ // Add the view controller's view to the window and display.
+ [window addSubview:viewController.view];
+ [window makeKeyAndVisible];
+
+ return YES;
+}
+
+
+- (void)applicationWillResignActive:(UIApplication *)application {
+ /*
+ Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
+ Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
+ */
+}
+
+
+- (void)applicationDidEnterBackground:(UIApplication *)application {
+ /*
+ Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
+ If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
+ */
+}
+
+
+- (void)applicationWillEnterForeground:(UIApplication *)application {
+ /*
+ Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
+ */
+}
+
+
+- (void)applicationDidBecomeActive:(UIApplication *)application {
+ /*
+ Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
+ */
+}
+
+
+- (void)applicationWillTerminate:(UIApplication *)application {
+ /*
+ Called when the application is about to terminate.
+ See also applicationDidEnterBackground:.
+ */
+}
+
+
+#pragma mark -
+#pragma mark Memory management
+
+- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
+ /*
+ Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
+ */
+}
+
+
+- (void)dealloc {
+ [viewController release];
+ [window release];
+ [super dealloc];
+}
+
+
+@end
@@ -0,0 +1,21 @@
+//
+// iPodLibraryAccessViewController.h
+// iPodLibraryAccess
+//
+// Created by Art Gillespie on 6/11/10.
+// Copyright Glaresoft 2010. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import <MediaPlayer/MediaPlayer.h>
+#import <AVFoundation/AVFoundation.h>
+
+@interface iPodLibraryAccessViewController : UIViewController <MPMediaPickerControllerDelegate> {
+ AVPlayer* player;
+}
+
+- (IBAction)pickSong:(id)sender;
+- (void)exportAssetAtURL:(NSURL*)assetURL;
+
+@end
+
@@ -0,0 +1,171 @@
+//
+// iPodLibraryAccessViewController.m
+// iPodLibraryAccess
+//
+// Created by Art Gillespie on 6/11/10.
+// Copyright Glaresoft 2010. All rights reserved.
+//
+
+#import "iPodLibraryAccessViewController.h"
+
+@implementation iPodLibraryAccessViewController
+
+
+
+/*
+// The designated initializer. Override to perform setup that is required before the view is loaded.
+- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
+ if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
+ // Custom initialization
+ }
+ return self;
+}
+*/
+
+/*
+// Implement loadView to create a view hierarchy programmatically, without using a nib.
+- (void)loadView {
+}
+*/
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+ if ([keyPath compare:@"status"] == 0) {
+ AVAssetExportSession* export = (AVAssetExportSession*)object;
+ switch (export.status) {
+ case AVAssetExportSessionStatusUnknown:
+ NSLog(@"AVAssetExportSessionStatusUnknown");
+ break;
+ case AVAssetExportSessionStatusExporting:
+ NSLog(@"AVAssetExportSessionStatusExporting");
+ break;
+ case AVAssetExportSessionStatusCompleted:
+ NSLog(@"AVAssetExportSessionStatusCompleted");
+ break;
+ case AVAssetExportSessionStatusFailed:
+ NSLog(@"AVAssetExportSessionStatusFailed");
+ break;
+ case AVAssetExportSessionStatusCancelled:
+ NSLog(@"AVAssetExportSessionStatusCancelled");
+ break;
+ case AVAssetExportSessionStatusWaiting:
+ NSLog(@"AVAssetExportSessionStatusWaiting");
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+ //AVAudioSession* avSession = [AVAudioSession sharedInstance];
+
+ //try exporting an mp3 from the bundle
+ NSURL* assetURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Epistrophy" ofType:@"mp3"]];
+ [self exportAssetAtURL:assetURL];
+}
+
+/*
+// Override to allow orientations other than the default portrait orientation.
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
+ // Return YES for supported orientations
+ return (interfaceOrientation == UIInterfaceOrientationPortrait);
+}
+*/
+
+/*
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+ NSLog(@"observeValueForKeyPath: %@", keyPath);
+ if ([keyPath compare:@"status"] == 0) {
+ NSLog(@"status of playerItem changed");
+ AVPlayerItem* playerItem = (AVPlayerItem*)object;
+ if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
+ NSLog(@"playerItem ready to play");
+ [player play];
+ }
+ }
+}
+*/
+
+- (void)progressTimer:(NSTimer*)timer {
+ AVAssetExportSession* export = (AVAssetExportSession*)timer.userInfo;
+ switch (export.status) {
+ case AVAssetExportSessionStatusExporting:
+ NSLog(@"progress: %f", export.progress);
+ break;
+ case AVAssetExportSessionStatusCancelled:
+ case AVAssetExportSessionStatusCompleted:
+ case AVAssetExportSessionStatusFailed:
+ [timer invalidate];
+ break;
+ default:
+ break;
+ }
+}
+
+- (void)exportAssetAtURL:(NSURL*)assetURL {
+ NSDictionary * options = [[NSDictionary alloc] init];
+ AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetURL options:options];
+ //create an export session to write the asset as an m4a
+ AVAssetExportSession* export = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
+ //set the export session's outputURL to <Documents>/test.m4a
+ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *documentsDirectory = [paths objectAtIndex:0];
+ NSURL* outURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"test.m4a"]];
+ [[NSFileManager defaultManager] removeItemAtURL:outURL error:nil];
+ export.outputURL = outURL;
+ //set the output file type
+ export.outputFileType = AVFileTypeAppleM4A;
+ [export addObserver:self forKeyPath:@"status" options:0 context:nil];
+ [export addObserver:self forKeyPath:@"progress" options:0 context:nil];
+ [NSTimer scheduledTimerWithTimeInterval:.25 target:self selector:@selector(progressTimer:) userInfo:export repeats:YES];
+ [export exportAsynchronouslyWithCompletionHandler:^(void) {
+ if (export.status == AVAssetExportSessionStatusFailed) {
+ NSLog(@"export.error: %@", export.error);
+ }
+ [export release];
+ }];
+}
+
+- (void)mediaPicker:(MPMediaPickerController *)mediaPicker
+ didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
+ [self dismissModalViewControllerAnimated:YES];
+ for (MPMediaItem* item in mediaItemCollection.items) {
+ NSString* title = [item valueForProperty:MPMediaItemPropertyTitle];
+ NSURL* assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
+ NSLog(@"title: %@, url: %@", title, assetURL);
+ [self exportAssetAtURL:assetURL];
+ }
+}
+
+- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
+ [self dismissModalViewControllerAnimated:YES];
+}
+
+- (void)didReceiveMemoryWarning {
+ // Releases the view if it doesn't have a superview.
+ [super didReceiveMemoryWarning];
+
+ // Release any cached data, images, etc that aren't in use.
+}
+
+- (void)viewDidUnload {
+ // Release any retained subviews of the main view.
+ // e.g. self.myOutlet = nil;
+}
+
+- (void)showMediaPicker {
+ MPMediaPickerController* mediaPicker = [[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic] autorelease];
+ mediaPicker.delegate = self;
+ [self presentModalViewController:mediaPicker animated:YES];
+}
+
+- (IBAction)pickSong:(id)sender {
+ [self showMediaPicker];
+}
+
+- (void)dealloc {
+ [super dealloc];
+}
+
+@end
Oops, something went wrong.

0 comments on commit e751b71

Please sign in to comment.