Powered By Blogger

Tuesday, October 30, 2012

how to check iPhone volume programmatically?

Hi everyone,
Below is the trick to check the iPhone/iPad/iPod Touch (iOS Device) volume level. Please take a look..

#import <AudioToolbox/AudioToolbox.h>

    Float32 volume;
    UInt32 dataSize = sizeof(Float32);
    AudioSessionGetProperty (
                             kAudioSessionProperty_CurrentHardwareOutputVolume,
                             &dataSize,
                             &volume
                             );
    NSLog(@"Volume Level : %f",volume);
    if (volume <= 0.25)
    {
              NSLog(@"Volume is below %d \%",(volume*100));
    }

Thanks,
Nilesh M. Prajapati

how to create UUID in iPhone?

Hi friends,
Here is the example for creating UUID in your iPhone/iPad/iPod Touch application.

            NSString *deviceUuid = nil;
            CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
            deviceUuid = (NSString *)cfUuid;
            NSLog(@"deviceUuid : %@",deviceUuid);
            CFRelease(cfUuid);

Now , you can use this "deviceUuid" as UUID.

Thanks,
Nilesh M. Prajapati

Saturday, October 20, 2012

how to implement MPMoviePlayerViewController in iPhone?

Hi every one,
Please take a look at MPMoviePlayerController integration for local/live video playing in iPhone/iPad/iPod Touch (iOS 4.0 and later..).

Step 1 : Add "MediaPlayer.framework" into your project resource.
Step 2 :  Import <MediaPlayer/MediaPlayer.h> into your class where you want to use MediaPlayer for playing videos (local/live).
Step 3 : Place below code into the controller class where you want to integrate player.

-(IBAction)btnVideoClicked:(id)sender
{
    @try
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        GetVideos *obj_video = [arrVideos objectAtIndex:[sender tag]];
        MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:obj_video.VideoPath]];
        [moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
        [moviePlayerViewController.moviePlayer setShouldAutoplay:YES];
        [moviePlayerViewController.moviePlayer setFullscreen:NO animated:YES];
        [moviePlayerViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [moviePlayerViewController.moviePlayer setScalingMode:MPMovieScalingModeNone];
        [moviePlayerViewController.moviePlayer setUseApplicationAudioSession:NO];
        // Register to receive a notification when the movie has finished playing. 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlaybackStateDidChange:) 
                                                     name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                                   object:moviePlayerViewController];
        // Register to receive a notification when the movie has finished playing. 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlayBackDidFinish:) 
                                                     name:MPMoviePlayerPlaybackDidFinishNotification 
                                                   object:moviePlayerViewController];
        [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
        moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
        [moviePlayerViewController release];
        [pool release];
    }
    @catch (NSException *exception) {
        // throws exception
    }
}


#pragma mark -----------------------
#pragma mark MPMoviePlayer Notification Methods


-(void)moviePlaybackStateDidChange:(NSNotification *)notification
{

    MPMoviePlayerViewController *moviePlayerViewController = [notification object]; 

    if (moviePlayerViewController.moviePlayer.loadState == MPMovieLoadStatePlayable &&
        moviePlayerViewController.moviePlayer.playbackState != MPMoviePlaybackStatePlaying)
    {
        [moviePlayerViewController.moviePlayer play];
    }
   
    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                               object:moviePlayerViewController];
    moviePlayerViewController = nil;
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification

    MPMoviePlayerViewController *moviePlayerViewController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:moviePlayerViewController]; 
    [self dismissMoviePlayerViewControllerAnimated];
    moviePlayerViewController = nil;


Thanks ,
Nilesh M. Prajapati

Tuesday, October 16, 2012

How to integrate live audio streaming in iPhone?

Hi everyone,
I think, all of you heard about live audio/video streaming. I'm going to share "Live Audio Streaming" example with you.
Please check this :

Step 1: First of all, you have to download these two files(AudioStreamer.h, AudioStreamer.m) from http://code.google.com/p/audiostreamer-meta/source/browse/trunk/Classes

Step 2: Add necessary frameworks and above two files into your project.

Step 3:
Implement below code to your project file.

@class AudioStreamer;

@interface CallerTuneListViewController : UIViewController
{
    AudioStreamer *streamer;
}

#import "CallerTuneListViewController.h"

#import "AudioStreamer.h"
#import <QuartzCore/CoreAnimation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <CFNetwork/CFNetwork.h>
#import <AudioToolbox/AudioToolbox.h>

@implementation CallerTuneListViewController

#pragma mark ---------------------------------
#pragma mark View LifeCycle

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([streamer isPlaying]) {
        [self destroyStreamer];
    }
}

#pragma mark ---------------------------------
#pragma mark Memory Management

- (void)dealloc
{
    [self destroyStreamer];
    [super dealloc];
}

#pragma mark ---------------------------------
#pragma mark UIButton Actions

-(IBAction)playLiveStream:(id)sender
{
            if (![streamer isPlaying])
                {
                    [self createStreamer:AudioFilePath];
                    [streamer start];
                }
                else
                {
                    [streamer stop];
                }
}


#pragma mark ---------------------------------
#pragma mark AudioStreamer Methods

// Removes the streamer, the UI update timer and the change notification
- (void)destroyStreamer
{
    if (streamer)
    {
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:ASStatusChangedNotification
         object:streamer];
      
        [streamer stop];
        [streamer release];
        streamer = nil;
    }
}

// Creates or recreates the AudioStreamer object.
- (void)createStreamer:(NSString *)audioPath
{
    if (streamer)
    {
        return;
    }
  
    [self destroyStreamer];
  
    NSURL *url = [NSURL URLWithString:audioPath];
    streamer = [[AudioStreamer alloc] initWithURL:url];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playbackStateChanged:)
     name:ASStatusChangedNotification
     object:streamer];
}

// Invoked when the AudioStreamer
// reports that its playback status has changed.
- (void)playbackStateChanged:(NSNotification *)aNotification
{
    if ([streamer isWaiting])
    {
    }
    else if ([streamer isPlaying])
    {
    }
    else if ([streamer isIdle])
    {
        [self destroyStreamer];
    }
}

I hope this will be useful to all...

Thanks ,
Nilesh M. Prajapati

How to make paging in iPhone?

Hi,
Here is the example of horizontal paging using UIScrollView and its property pagingEnabled.

Look at the below code :

- (void)viewDidLoad
{
    [super viewDidLoad];
    pageCount = 0;
    scrollView.pagingEnabled = YES;
}

#pragma mark ---------------------
#pragma mark User-Defined Methods

-(void)GenerateVideoThumbnail
{
    for (UIView *videoView in self.scrollView.subviews)
    {
        [videoView removeFromSuperview];
        videoView = nil;
    }

    CGFloat X = 12.5,Y = 5.0; // Button X and Y position
    CGFloat Width = 90.0,Height = 90.0;
    CGFloat XDiff = 12.5,YDiff = 5.0;
    NSInteger MaxIconPerRow = 3;  
    NSInteger MaxIconPerPage = 12;

    for (int i=0; i<[arrVideos count]; i++)
    {
        GetVideos *obj_video = [arrVideos objectAtIndex:i];
        UIButton *btnVideo = [UIButton buttonWithType:UIButtonTypeCustom];
        btnVideo.tag = i;
        NSLog(@"frame X: %f, Y: %f",btnVideo.frame.origin.x,btnVideo.frame.origin.y);
        [btnVideo setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:obj_video.Thumnailpath]]] forState:UIControlStateNormal];

        btnVideo.frame = CGRectMake(X, Y, Width, Height);
        [self.scrollView addSubview:btnVideo];
        [btnVideo startLoading];
        btnVideo = nil;
       
        X = X + Width + XDiff;
        if((i+1)%MaxIconPerRow ==0)
        {
            X = (pageCount*320.0) + XDiff;
            Y = Y + Height + YDiff;
        }
        if((i+1)%MaxIconPerPage ==0)
        {
            ++pageCount;
            X = (pageCount*320.0) + XDiff;
            Y = 5.0;
        }
    }
    self.scrollView.contentSize = CGSizeMake((pageCount*self.scrollView.frame.size.width), self.scrollView.frame.size.height);
}

Thursday, October 4, 2012

How to add custom fonts to iPhone application?

Hi,

As of iOS 4.0 and later iOS version came, it has become very easy to add custom fonts to your iPhone/iPad/iPod applications. Here, some steps you have to follow in order to add custom fonts:

    1.    Add your custom font files into your project using XCode as a resource
    2.    Add a key to your info.plist file called UIAppFonts.
    3.    Make this key an array
    4.    For each font you have, enter the full name of your font file (including the extension) as items
           to the UIAppFonts array.
    5.    Save info.plist
    6.    Now in your application you can simply call
           [UIFont  fontWithName:@"CustomFontName" size:FontSize]
           to get the custom font to use with your UILabels and UITextViews, etc…

It’s very simple to integrate!

Monday, October 1, 2012

Mask animation in iPhone

Hi,
Here is the sample code for "Mask Animation" in Objective C and iPhone/iPad/iPod.

#import <UIKit/UIKit.h>

typedef void (^animationCompletionBlock)(void);
#define kAnimationCompletionBlock @"animationCompletionBlock"

@interface ViewController : UIViewController
{
  BOOL animationInFlight;
  IBOutlet UIImageView *imageView;
}
@property (nonatomic) BOOL animationInFlight;

- (IBAction)MaskAnimation:(id)sender;


@end

#import <QuartzCore/QuartzCore.h>
#import "ViewController.h"



@interface ViewController ()

- (void) removePauseForLayer: (CALayer *) theLayer;
@end

@implementation ViewController


@synthesize animationInFlight;

#pragma mark - view lifecycle methods

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


//We only support portrait and portrait upside down orientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}


- (void)viewDidUnload
{
  imageView = nil;
  [super viewDidUnload];
  // Release any retained subviews of the main view.
}

#pragma mark - Instance methods

- (IBAction)MaskAnimation:(id)sender;
{
    animationCompletionBlock theBlock;
    imageView.hidden = FALSE;//Show the image view
   
    //Create a shape layer that we will use as a mask for the  image view
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
   
   
   
    CGFloat maskHeight = imageView.layer.bounds.size.height;
    CGFloat maskWidth = imageView.layer.bounds.size.width;
   
   
    CGPoint centerPoint;
    centerPoint = CGPointMake( maskWidth/2, maskHeight/2);
   
    //Make the radius of our arc large enough to reach into the corners of the image view.
    CGFloat radius = sqrtf(maskWidth * maskWidth + maskHeight * maskHeight)/2;
    //  CGFloat radius = MIN(maskWidth, maskHeight)/2;
   
    //Don't fill the path, but stroke it in black.
    maskLayer.fillColor = [[UIColor clearColor] CGColor];
    maskLayer.strokeColor = [[UIColor blackColor] CGColor];
   
    maskLayer.lineWidth = radius; //Make the line thick enough to completely fill the circle we're drawing
    //  maskLayer.lineWidth = 10; //Make the line thick enough to completely fill the circle we're drawing
   
    CGMutablePathRef arcPath = CGPathCreateMutable();
   
    //Move to the starting point of the arc so there is no initial line connecting to the arc
    CGPathMoveToPoint(arcPath, nil, centerPoint.x, centerPoint.y-radius/2);
   
    //Create an arc at 1/2 our circle radius, with a line thickess of the full circle radius
    CGPathAddArc(arcPath,
                 nil,
                 centerPoint.x,
                 centerPoint.y,
                 radius/2,
                 3*M_PI/2,
                 -M_PI/2,
                 NO);
   
    maskLayer.path = arcPath;
   
    //Start with an empty mask path (draw 0% of the arc)
    maskLayer.strokeEnd = 0.0;
   
   
    CFRelease(arcPath);
   
    //Install the mask layer into out image view's layer.
    imageView.layer.mask = maskLayer;
   
    //Set our mask layer's frame to the parent layer's bounds.
    imageView.layer.mask.frame = imageView.layer.bounds;
   
    //Create an animation that increases the stroke length to 1, then reverses it back to zero.
    CABasicAnimation *swipe = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    swipe.duration = 2;
    swipe.delegate = self;
    [swipe setValue: theBlock forKey: kAnimationCompletionBlock];
   
    swipe.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    swipe.fillMode = kCAFillModeForwards;
    swipe.removedOnCompletion = NO;
    swipe.autoreverses = YES;
   
    swipe.toValue = [NSNumber numberWithFloat: 1.0];
   
    self.animationInFlight = TRUE;
  
    //Set up a completion block that will be called once the animation is completed.
    theBlock = ^void(void)
    {   
        imageView.layer.mask = nil;
        self.animationInFlight = FALSE;
        imageView.hidden = TRUE;
       
        if (self.view.layer.speed == 0)
            [self removePauseForLayer: self.view.layer];
    };
   
    /*
     Install the completion block in the animation using the key kAnimationCompletionBlock
     The completion block will be run by in the animation's animationDidStop:finished delegate method.
     This approach doesn't work for animations that are part of a group, unfortunately, since an animation's
     delegate methods don't get called when the animation is part of an animation group
     */
   
    [swipe setValue: theBlock forKey: kAnimationCompletionBlock];
   
    [maskLayer addAnimation: swipe forKey: @"strokeEnd"];
   
}


- (void) removePauseForLayer: (CALayer *) theLayer;
{
    theLayer.speed = 1.0;
    theLayer.timeOffset = 0.0;
    theLayer.beginTime = 0.0;
}

#pragma mark - CAAnimation delegate methods

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
  animationCompletionBlock theBlock = [theAnimation valueForKey: kAnimationCompletionBlock];
  if (theBlock)
    theBlock();
}


@end

How to move an object using touch event in iPhone?

Hello friends,
Here is the example regarding moving an object in Objective C or iPhone/iPad/iPod development.


#import <UIKit/UIKit.h>

@interface ImageMoveViewController : UIViewController
{
    IBOutlet UIImageView *photo;
}

@end


#import "ImageMoveViewController.h"

@implementation ImageMoveViewController

/*
 Implement viewDidLoad if you need to do additional setup after loading the view.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
   
    if ([touch view] == photo) {
        // move the image view
        photo.center = touchLocation;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}
- (void)dealloc {
    [super dealloc];
}

@end


Thanks,
Nilesh M. Prajapati