Search This Blog

Thursday, November 28, 2013

Custom Audio Player for iPhone SDK


Audio Player

.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController
{
    AVAudioPlayer *player;
    IBOutlet UISlider *volumeSlider,*durationSlider;
}

-(IBAction)durationChange:(id)sender;
-(IBAction)volumeChanged:(id)sender;
-(IBAction)stop:(id)sender;
-(IBAction)play:(id)sender;
@end

.m

#import "ViewController.h"
@implementation ViewController


-(IBAction)volumeChanged:(id)sender
{
    [player setVolume:volumeSlider.value];
}


-(IBAction)stop:(id)sender
{
    if(player)
        [player stop];
}
-(IBAction)durationChange:(id)sender;
{
    [player setCurrentTime:durationSlider.value];
}

-(IBAction)play:(id)sender;
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"123-05 No Money No Money" ofType:@"mp3"];
    NSLog(@"file path is %@",filePath);
    
    //converting file Path to the url
    NSURL *url  = [NSURL fileURLWithPath:filePath];
    
    if(!player)
    player = [[AVAudioPlayer allocinitWithContentsOfURL:urlerror:nil];
    
    
    float totalDuration = [player duration];
    [durationSlider setMaximumValue:totalDuration];
    
    float currentDuration = [player currentTime];
    
    
    
    [player setDelegate:self];
    
    //set Volume
    [player setVolume:1];
    
    [player prepareToPlay];
    [player play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"audioPlayerDidFinishPlaying called");

}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"audioPlayerDecodeErrorDidOccur called");
}

@end

No comments:

Post a Comment