Go Back   iPod touch Fans forum - iPad, iPhone, and iPod users > iPhone OS / iOS Ecosystem > iPhone OS Development

What are you waiting for? Be member #461203!

Discuss the latest apps and accessories, or post your question on the forums! All visitors must register before they can post and answer questions and participate in our lively community, so register for free today!
Reply
 
Thread Tools Search this Thread
  #1  
Old 02-09-2009
SkylarEC's Avatar
Super Moderator Emeritus
Join Date: Sep 2007
 
None
Default [Sample App/Code] Track Info/Album Art/Track Control --PocketTouch.framework w/Xcode



First of all, make sure you have the most current version of the PocketTouch framework. It will always be posted here: http://www.touchrepo.com/source/Pock....framework.zip on the TouchRepo. Take that and add it to all your iPhone SDKs in Xcode, in the PrivateFrameworks directory.

Link to Xcode project: http://www.touchrepo.com/SampleCode/...ource_Code.zip

Link to Precompiled Application (just drop it into /Applications) so you can see the app before you download the Xcode project: http://www.touchrepo.com/SampleCode/...pplication.zip





Source Code:

TrackInfoView.h
Objective C Code:
#import <UIKit/UIKit.h>


@interface TrackInfoView : UIView {
    UILabel  *album;
    UILabel  *artist;
    UILabel  *song;
}

@property (nonatomic, retain) UILabel *album;
@property (nonatomic, retain) UILabel *artist;
@property (nonatomic, retain) UILabel *song;

@end


TrackInfoView.m
Objective C Code:
#import "TrackInfoView.h"


@implementation TrackInfoView

@synthesize album, artist, song;


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self layoutSubviews];
    }
    return self;
}


- (void)layoutSubviews {
    self.bounds = CGRectMake(self.bounds.origin.x, self.superview.bounds.origin.y, self.bounds.size.width, self.superview.bounds.size.height);
    /*- Alternatively, set the bounds in Interface Builder.  It. by default, reduces the height 12 px and offsets the origin.y 6 px -*/
   
    CGRect frame = self.frame;
    CGRect artistRect   = CGRectMake(0, 0, frame.size.width, frame.size.height / 3);
    CGRect songRect  = CGRectMake(0, frame.size.height / 3, frame.size.width, frame.size.height / 3);
    CGRect albumRect    = CGRectMake(0, (frame.size.height / 3) * 2, frame.size.width, frame.size.height / 3);
   
    album = [[UILabel alloc] initWithFrame:albumRect];
    album.backgroundColor = [UIColor clearColor];
    album.font = [UIFont boldSystemFontOfSize:12];
    album.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
    album.shadowOffset = CGSizeMake(0, -1);
    album.text = @"Album";
    album.textAlignment = UITextAlignmentCenter;
    album.textColor = [UIColor lightGrayColor];
   
    artist = [[UILabel alloc] initWithFrame:artistRect];
    artist.backgroundColor = [UIColor clearColor];
    artist.font = [UIFont boldSystemFontOfSize:12];
    artist.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
    artist.shadowOffset = CGSizeMake(0, -1);
    artist.text = @"Artist";
    artist.textAlignment = UITextAlignmentCenter;
    artist.textColor = [UIColor lightGrayColor];
   
    song = [[UILabel alloc] initWithFrame:songRect];
    song.backgroundColor = [UIColor clearColor];
    song.font = [UIFont boldSystemFontOfSize:12];
    song.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
    song.shadowOffset = CGSizeMake(0, -1);
    song.text = @"Song";
    song.textAlignment = UITextAlignmentCenter;
    song.textColor = [UIColor whiteColor];
   
    [self addSubview:album];
    [self addSubview:artist];
    [self addSubview:song];
}


- (void)dealloc {
    [album release];
    [artist release];
    [song release];
    [super dealloc];
}


@end


TEST_MEDIACONTROL_APPViewController.h
Objective C Code:
#import <PocketTouch/PocketTouch.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>

#import "TrackInfoView.h"

@interface TEST_MEDIACONTROL_APPViewController : UIViewController {
    IBOutlet UIBarButtonItem    *nextButton;
    IBOutlet UIBarButtonItem    *prevButton;
    IBOutlet UIImageView        *albumArt; 
         UIImageView        *reflectionView;
    IBOutlet UINavigationBar    *controlBar;
    IBOutlet TrackInfoView    *infoView;
   
    PTMusicController         *musicController;
}

@property (nonatomic, retain) IBOutlet UIBarButtonItem *nextButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *prevButton;
@property (nonatomic, retain) IBOutlet UIImageView *albumArt;
@property (nonatomic, retain) IBOutlet UINavigationBar *controlBar;
@property (nonatomic, retain) IBOutlet TrackInfoView *infoView;

- (IBAction)nextButtonPressed:(id)sender;
- (IBAction)prevButtonPressed:(id)sender;

- (void)manageArtwork;
- (void)updateNavigationBarTrackInfo;

#pragma mark Reflection Methods


CGImageRef CreateGradientImage(int gradientWidth, int gradiendHeight);
- (UIImage *)reflectionImage:(UIImageView *)fromImage withHeight:(NSUInteger)height;

@end


TEST_MEDIACONTROL_APPViewController.m
Objective C Code:
#import "TEST_MEDIACONTROL_APPViewController.h"

#define kDefaultReflectionFraction 0.25
#define kDefaultReflectionOpacity 0.75

@implementation TEST_MEDIACONTROL_APPViewController

@synthesize nextButton, prevButton, albumArt, controlBar, infoView;


- (void)dealloc {
    [musicController release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    musicController = [[PTMusicController alloc] init];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
    CGRect reflectionRect = self.albumArt.frame;
    reflectionRect.size.height = reflectionRect.size.height * kDefaultReflectionFraction;
    reflectionRect = CGRectOffset(reflectionRect, 0, self.albumArt.frame.size.height);   
    reflectionView = [[UIImageView alloc] initWithFrame:reflectionRect];
    reflectionView.alpha = kDefaultReflectionOpacity;         

    [self.view addSubview:reflectionView];
    [self manageArtwork];
   
    [self updateNavigationBarTrackInfo];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ? NO : YES);
}


- (IBAction)nextButtonPressed:(id)sender {
    [musicController playNextSong];
    [self updateNavigationBarTrackInfo];
    [self manageArtwork];
}


- (IBAction)prevButtonPressed:(id)sender {
    [musicController playPreviousSong];
    [self updateNavigationBarTrackInfo];
    [self manageArtwork];
}

- (void)manageArtwork {
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setDuration:0.66];
    [animation setTimingFunction:UIViewAnimationCurveEaseInOut];
    [animation setType:@"twist"];
    [animation setSubtype:kCATransitionFromTop];
    [animation setFillMode: @"extended"];
    [animation setRemovedOnCompletion:NO];
    [[self.albumArt layer] addAnimation:animation forKey:@"twistAnimation"];
    ///[[reflectionView layer] addAnimation:animation forKey:@"twistAnimation"];
   
    self.albumArt.image = [musicController getAlbumArtWithSize:CGSizeMake(320, 320)];
    reflectionView.image = [self reflectionImage:self.albumArt withHeight:self.albumArt.bounds.size.height * kDefaultReflectionFraction];
}

- (void)updateNavigationBarTrackInfo {
    NSArray *infoArray = [NSArray arrayWithArray:[musicController readTrackInfo]];
   
    infoView.artist.text = [infoArray objectAtIndex:0];
    infoView.album.text = [infoArray objectAtIndex:2];
    infoView.song.text = [infoArray objectAtIndex:1];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark Reflection Methods


CGImageRef CreateGradientImage(int gradientWidth, int gradiendHeight) {
    CGImageRef theCGImage = NULL;
   
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef gradientBitmapContext = CGBitmapContextCreate(nil, gradientWidth, gradiendHeight, 8, 0, colorSpace, kCGImageAlphaNone);
   
    CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};
    CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
    CGColorSpaceRelease(colorSpace);
    CGPoint gradientStartPoint = CGPointZero;
    CGPoint gradientEndPoint = CGPointMake(0, gradiendHeight);
    CGContextDrawLinearGradient(gradientBitmapContext, grayScaleGradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsAfterEndLocation);
   
    theCGImage = CGBitmapContextCreateImage(gradientBitmapContext);
    CGContextRelease(gradientBitmapContext);
   
    return theCGImage;
}


- (UIImage *)reflectionImage:(UIImageView *)fromImage withHeight:(NSUInteger)height {
   
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext = CGBitmapContextCreate (nil, fromImage.bounds.size.width, height, 8, 0, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
    CGColorSpaceRelease(colorSpace);
   
    CGFloat translateVertical= fromImage.bounds.size.height - height;
    CGContextTranslateCTM(bitmapContext, 0, -translateVertical);
   
    CALayer *layer = [fromImage layer];
    [layer renderInContext:bitmapContext];
   
    CGImageRef bitmapImage = CGBitmapContextCreateImage(bitmapContext);
    CGContextRelease(bitmapContext);
   
    CGImageRef gradientMaskImage = CreateGradientImage(1, height);
    CGImageRef reflectionImage = CGImageCreateWithMask(bitmapImage, gradientMaskImage);
    CGImageRelease(bitmapImage);
    CGImageRelease(gradientMaskImage);
   
    UIImage *theImage = [UIImage imageWithCGImage:reflectionImage];
   
    CGImageRelease(reflectionImage);
   
    return theImage;
}


#pragma mark Touch Methods


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
   
    NSArray *allTouches = [touches allObjects];
   
    if (CGRectContainsPoint([self.albumArt frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {
        [musicController playOrPauseSong];
        CATransition *animation = [CATransition animation];
        [animation setDelegate:self];
        [animation setDuration:0.66];
        [animation setTimingFunction:UIViewAnimationCurveEaseInOut];
        [animation setType:@"tubey"];
        [animation setSubtype:kCATransitionFromTop];
        [animation setFillMode: @"extended"];
        [animation setRemovedOnCompletion:NO];
        [[self.albumArt layer] addAnimation:animation forKey:@"tubeyAnimation"];
    }
}

@end


All other source is unchanged from the source created with Xcode's "View-Based Application" template.
__________________

Last edited by SkylarEC; 07-21-2009 at 10:08 AM..
  #2  
Old 02-10-2009
spoonforknife's Avatar
Super Moderator
Join Date: Nov 2007
 
None
Hmm... I tried this, and put the PocketTouch.framework in every PrivateFrameworks folder, opened it in Xcode, and the framework was recognized (as in that it saw the framework existed within Xcode) but none of the code would recognize the PocketTouch headers.
  #3  
Old 02-10-2009
SkylarEC's Avatar
Super Moderator Emeritus
Join Date: Sep 2007
 
None
What errors do you get? Post a screenshot of them.
Sponsored Links
  #4  
Old 02-10-2009
spoonforknife's Avatar
Super Moderator
Join Date: Nov 2007
 
None
Quote:
Originally Posted by SkylarEC View Post
What errors do you get? Post a screenshot of them.
  #5  
Old 02-10-2009
SkylarEC's Avatar
Super Moderator Emeritus
Join Date: Sep 2007
 
None
That means you don't have the framework in the right place.

These are the locations of where I have PocketTouch.framework installed:
  • /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/PrivateFrameworks/PocketTouch.framework
  • /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/System/Library/PrivateFrameworks/PocketTouch.framework

Are you sure you have the framework in the PrivateFrameworks directories?


EDIT: The answer: I don't bother with the Simulator, as there is no music/music player on the Simulator.
The framework would need to be recompiled to make it "work" on the Simulator. You will need
to compile for the Device. If you just want to compile, you'll need to add the framework into the Simulator's SDKs.

EDIT II If you insist on compiling for the Simulator, just delete the "#import <PocketTouch/PocketTouch.h> line and add an @class PTMusicPlayer

Last edited by SkylarEC; 02-10-2009 at 02:04 PM..
  #6  
Old 02-10-2009
spoonforknife's Avatar
Super Moderator
Join Date: Nov 2007
 
None
Quote:
Originally Posted by SkylarEC View Post
That means you don't have the framework in the right place.

These are the locations of where I have PocketTouch.framework installed:
  • /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/PrivateFrameworks/PocketTouch.framework
  • /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/System/Library/PrivateFrameworks/PocketTouch.framework

Are you sure you have the framework in the PrivateFrameworks directories?


EDIT: The answer: I don't bother with the Simulator, as there is no music/music player on the Simulator.
The framework would need to be recompiled to make it "work" on the Simulator. You will need
to compile for the Device. If you just want to compile, you'll need to add the framework into the Simulator's SDKs.

EDIT II If you insist on compiling for the Simulator, just delete the "#import <PocketTouch/PocketTouch.h> line and add an @class PTMusicPlayer
Whoops, that was my mistake. I meant to select device. *D'oh!*
  #7  
Old 02-11-2009
cocotutch's Avatar
Multi-Touch Lover
Join Date: Oct 2008
 
iPhone 3GS (White) 16GB
3.1.2 jailbroken
Ohhh, this is NIIIIIICCE!!!! Me makey an app that mimics MobileMusicPlayer....but in White

Oh, and I'm gonna have to use this as a base btw..

CT
  #8  
Old 02-11-2009
Steaps's Avatar
Multi-Touch Maniac
Join Date: Oct 2007
 
iPod touch 16GB
3.0 jailbroken
Quote:
Originally Posted by cocotutch View Post
Ohhh, this is NIIIIIICCE!!!! Me makey an app that mimics MobileMusicPlayer....but in White

Oh, and I'm gonna have to use this as a base btw..

CT
Just theme the MobileMusicPlayer and save hours of work...
  #9  
Old 02-18-2009
veeloc's Avatar
Multi-Touch Lover
Join Date: Sep 2008
 
iPhone 3GS (Black) 32GB
3.0 jailbroken
Quote:
Originally Posted by Stephen.4 View Post
Just theme the MobileMusicPlayer and save hours of work...
lol. maybe he wants some practice in coding it...

cuz i kno i do...
  #10  
Old 04-16-2009
Multi-Touch Amateur
Join Date: Jan 2008
 
None 32GB
3.0 jailbroken
Thanks for the framework! It's awesome!
I have noticed in the log that you output some id and the files path.
Is it the songs id in the musiclibrary and how do I access that information from the framework in code? I'm mainly interested in the path.

Thanks again!

Code:
Ex:
Apr 16 23:02:13 iPhone TownTracks[1458]: Number Of Tracks:	565
Apr 16 23:02:13 iPhone TownTracks[1458]: id is:	-995506019201651568
Apr 16 23:02:13 iPhone TownTracks[1458]: Current playing song: Artist: Pacific, Title: Runway To Elsewhere, file path: /var/mobile/Media//iTunes_Control/Music/F05/AGEK.mp3
Reply

Tags
tutorial

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -7. The time now is 08:13 PM.

Recent blog posts Recent threads




Powered by vBulletin®
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 2007 - 2010 Vigorous Media LLC - All Rights Reserved.
Page generated in 0.19476 seconds with 8 queries