[Sample App/Code] Track Info/Album Art/Track Control --PocketTouch.framework w/Xcode

Discussion in 'iOS Development' started by SkylarEC, Feb 9, 2009.

  1. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129

    Please Register or Log in to view images




    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/PocketTouch.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/TEST_MEDIACONTROL_APP_Source_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/TEST_MEDIACONTROL_APP_Application.zip





    Source Code:

    TrackInfoView.h
    [OBJC]#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
    [/OBJC]


    TrackInfoView.m
    [OBJC]#import "TrackInfoView.h"


    @implementation TrackInfoView

    @synthesize album, artist, song;


    - (id)initWithFrame

    Please Register or Log in to view images

    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[/OBJC]


    TEST_MEDIACONTROL_APPViewController.h
    [OBJC]#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

    Please Register or Log in to view images

    id)sender;
    - (IBAction)prevButtonPressed

    Please Register or Log in to view images

    id)sender;

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

    #pragma mark Reflection Methods


    CGImageRef CreateGradientImage(int gradientWidth, int gradiendHeight);
    - (UIImage *)reflectionImage

    Please Register or Log in to view images

    UIImageView *)fromImage withHeight

    Please Register or Log in to view images

    NSUInteger)height;

    @end[/OBJC]


    TEST_MEDIACONTROL_APPViewController.m
    [OBJC]#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

    Please Register or Log in to view images

    NSString *)nibNameOrNil bundle

    Please Register or Log in to view images

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


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



    - (void)viewWillAppear

    Please Register or Log in to view images

    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

    Please Register or Log in to view images

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


    - (IBAction)nextButtonPressed

    Please Register or Log in to view images

    id)sender {
    [musicController playNextSong];
    [self updateNavigationBarTrackInfo];
    [self manageArtwork];
    }


    - (IBAction)prevButtonPressed

    Please Register or Log in to view images

    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

    Please Register or Log in to view images

    "twist"];
    [animation setSubtype:kCATransitionFromTop];
    [animation setFillMode: @"extended"];
    [animation setRemovedOnCompletion:NO];
    [[self.albumArt layer] addAnimation:animation forKey

    Please Register or Log in to view images

    "twistAnimation"];
    ///[[reflectionView layer] addAnimation:animation forKey

    Please Register or Log in to view images

    "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

    Please Register or Log in to view images

    UIImageView *)fromImage withHeight

    Please Register or Log in to view images

    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

    Please Register or Log in to view images

    NSSet *)touches withEvent

    Please Register or Log in to view images

    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

    Please Register or Log in to view images

    "tubey"];
    [animation setSubtype:kCATransitionFromTop];
    [animation setFillMode: @"extended"];
    [animation setRemovedOnCompletion:NO];
    [[self.albumArt layer] addAnimation:animation forKey

    Please Register or Log in to view images

    "tubeyAnimation"];
    }
    }

    @end[/OBJC]


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

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    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. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    What errors do you get? Post a screenshot of them.
  4. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317

    Please Register or Log in to view images

  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    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
  6. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Whoops, that was my mistake. I meant to select device. *D'oh!*
  7. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Ohhh, this is NIIIIIICCE!!!! Me makey an app that mimics MobileMusicPlayer....but in White

    Please Register or Log in to view images



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

    Please Register or Log in to view images



    CT
  8. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Just theme the MobileMusicPlayer and save hours of work...
  9. veeloc

    veeloc New Member

    Joined:
    Sep 6, 2008
    Messages:
    815
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    lol. maybe he wants some practice in coding it...

    cuz i kno i do...
  10. SiMS

    SiMS New Member

    Joined:
    Jan 30, 2008
    Messages:
    2
    Likes Received:
    0
    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!

    Please Register or Log in to view images



    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

Share This Page