In this line
appDelegate.audioPlayer = [[AVPlayer alloc] init];
you seem to be alloc'ing and init'ing a new AVPlayer. As such it's not surprising that you get a "not playing" result. Simply leave out that line.
Question
I have a tableview of songs on the ios device and if I select a row, I push to a new view where AVPlayer starts playing the selected song. Now if I go back and select another row, pushing to the view the app will start playing the new song, while continue to play the one that was running already.
EDIT: I tried using the rate value like this without success, as it will always output "not playing" if i put it in my viewdidload method, even if a song is playing:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
appDelegate = (SimpleTableAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.audioPlayer = [[AVPlayer alloc] init];
if (appDelegate.audioPlayer.rate == 0.0f)
{
NSLog(@"not playing");
} else {
NSLog(@"already playing");
}
}
Solution
In this line
appDelegate.audioPlayer = [[AVPlayer alloc] init];
you seem to be alloc'ing and init'ing a new AVPlayer. As such it's not surprising that you get a "not playing" result. Simply leave out that line.
OTHER TIPS
AVPlayer has a rate
property which indicates the speed of playback as a float. 0.0f
indicates stopped. The play
and stop
methods just change the rate
property.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
appDelegate = (SimpleTableAppDelegate *)[[UIApplication sharedApplication] delegate];
//Stop potential existing audioPlayer prior to creating new player
[appDelegate.audioPlayer stop];
//Create new audio player
appDelegate.audioPlayer = [[AVPlayer alloc] init];
}