I made an application with tab bar,nav bar and table view .
In the table view, you can choose to listen to some sound.
A new view will appear, and I have some controls, such as: play, pause, volume slider, progress slider, label with the current time.
It works, but not perfect. I can play the sound, I can pause the sound, I can also use the slider to scroll forward or backward. But now:
When I press the back button on the navigation bar, the song continues to play. This is normal, but when I return to viewing again, the timer and slider reset themselves. I can not pause the song, just need to wait until it stops playing.
In addition, when I click on the game, return to the table view, select another file to play, the first file will not stop playback .
Here is the Audio1DetailViewController.h code:
Here is the Audio1DetailViewController.m code:
#import "Audio1DetailViewController.h" @implementation Audio1DetailViewController @synthesize volumeControl, timerLabel, playbackTimer, progressBar, audioPlayer; -(void)playAudio { playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; [audioPlayer play]; } -(void)stopAudio { [playbackTimer invalidate]; [audioPlayer stop]; } -(void)adjustVolume { if (audioPlayer != nil) { audioPlayer.volume = volumeControl.value; } } -(void)updateTime { float minutes = floor(audioPlayer.currentTime/60); float seconds = audioPlayer.currentTime - (minutes * 60); float duration_minutes = floor(audioPlayer.duration/60); float duration_seconds = audioPlayer.duration - (duration_minutes * 60); NSString *timeInfoString = [[NSString alloc] initWithFormat:@"%0.0f.%0.0f / %0.0f.%0.0f", minutes, seconds, duration_minutes, duration_seconds]; timerLabel.text = timeInfoString; [timeInfoString release]; } - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"001Fatiha" ofType:@"MP3"]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; if (error) { NSLog(@"Error in audioPlayer: %@", [error localizedDescription]); } else { audioPlayer.delegate = self; [audioPlayer prepareToPlay]; } playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES]; progressBar.maximumValue = audioPlayer.duration;
Here is AudioTableViewController.h
And AudioTableViewController.m
#import "AudioTableViewController.h" #import "Audio1DetailViewController.h" #import "DEQAppDelegate.h" @implementation AudioTableViewController @synthesize audioArray; @synthesize audio1DetailViewController; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) {