user3222991 user3222991 - 3 months ago 17
iOS Question

How to display Pause Button if user click the Play button in AVAudioPlayer in ios

Hi i am developing AVAudioplayer according to my project requirement.I am creating audioplayer programatically. I want to display pause button if user clicks the play button.I wrote some code.But pause button should not displayed if i click the play please help me anybody. Thanks in advance.


playButton=[[UIButton alloc]init];

playButton.frame=CGRectMake(125, 120, 29, 29);

playButton.backgroundColor=[UIColor clearColor];

playbackgroundImage=[UIImage imageNamed:@"play.png"];

[playButton setImage:playbackgroundImage forState:UIControlStateNormal];

[playButton addTarget:self action:@selector(playOrPauseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:playButton];


[playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

// Here Pause.png is a image showing Pause Button.

NSError *err;

AVAudioSession *audioSession=[AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

NSData *data=[NSData dataWithContentsOfURL:url];

audioPlayer=[[AVAudioPlayer alloc]initWithData:data error:&err];

[audioPlayer prepareToPlay];


[audioPlayer play];



else if (playing==YES)


[playButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

[audioPlayer pause];





The problem is that you are setting Setimage first time and on playOrPauseButtonPressed you are setting SetBackgroundImage. so replace this method in viewDidLoad

 [playButton setBackgroundImage:playbackgroundImage forState:UIControlStateNormal];