0

i create an app with xcode 4.6 on iOS 6.1 with a TabBar and 2 different tableView.

Each table view read a line form a file.plist and when tap on a row, you load a DetailView. The DetailViewController is the same for the 2 TableView but:

If start app tap on a first button i see FirstView with my table and Audio List, if I choose one file on list I load a Detail view and listener my file very well, but if now tap on second view on TabBar i can start playing other audio file from the second view but i listen overlay to firs audio and AVplayer not override.

There my coding:

From TableView loading plist:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([@"detailList" isEqualToString:segue.identifier]) {

        NSIndexPath *index = [self.tableView indexPathForCell:sender];
        [[segue destinationViewController] setSaved:[loadData objectAtIndex:index.row]];
    }
}

In a detali view:

    if (mysound.rate == 1.0) {

        UIImage *img = [UIImage imageNamed:@"Play_BT.png"];
        [control setImage:img forState:UIControlStateNormal];
        [mysound pause];

    } else {

        UIImage *img = [UIImage imageNamed:@"Pause_BT.png"];
        [control setImage:img forState:UIControlStateNormal];

        NSURL *url = [NSURL URLWithString:[saved objectForKey:@"audio"]];
        mysound = [[AVPlayer alloc] initWithURL:url];
        [mysound setAllowsExternalPlayback:YES];
        [mysound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
        [mysound play];
    }
}

Here incoming the issues, this code working very goog, but if tapo on tab bar and load other audio from other plist I listen Audio overlay.

How can use AVPlayer in Override the NSURL *url = [NSURL URLWithString:[saved objectForKey:@"audio"]];when change from tabbar?

Thanks.

BlackSheep
  • 1,087
  • 12
  • 29

1 Answers1

3

Solved, using this code:

In my AppDelegate .h adding

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

    AVPlayer *MyPlayer;
}

@property (strong, nonatomic) AVPlayer *MyPlayer;

in my AppDelegate .m inside didFinishLaunchingWithOptions adding

[MySound superclass];

Now can calling a method in all app with this code directly inside a .m file:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *url = [NSURL URLWithString:[saved objectForKey:@"link"]];
appDelegate.MySound = [[AVPlayer alloc] initWithURL:url];
[appDelegate.MySound setAllowsExternalPlayback:YES];
[appDelegate.MySound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[appDelegate.MySound play];

With this method now is possible controlling the AVPlayer evry where in the APP!!

I Hope this Help any guys ;)

BlackSheep
  • 1,087
  • 12
  • 29