在播放新流之前需要停止AVPlayer

时间:2014-08-18 09:20:45

标签: ios avplayer

我有在线广播应用。位于第一个viewController(tableView)音频流。音频流作为表格制作。通过单击AVpalyer打开的流屏幕名称。问题是当打开第二个流时,同时播放该流。如何制作如果一个线程,第二个被拒绝将被播放?

avplayer:

@interface RadioViewController ()

@end

@implementation RadioViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];

// Do any additional setup after loading the view.
NSString *restorationId = self.restorationIdentifier;
NSString *streamId = _detail.label;
NSString* identifer = [restorationId stringByAppendingString: streamId];
NSLog(@"%@", identifer);


self.title = apptitle;
self.streamLabel.text = _detail.label;
//Определение потока

NSString *streamURL;

if (_detail.label == @"ПЕРВЫЙ"){
    streamURL = streamURLpervyj;
} if (_detail.label == @"РОК") {
    streamURL = streamURLrok;
} if (_detail.label == @"БИТ") {
    streamURL = streamURLbit;
} if (_detail.label == @"ПО-РУССКИ") {
    streamURL = streamURLporusski;
} if (_detail.label == @"ХИП-ХОП") {
    streamURL = streamURLhiphop;
} if (_detail.label == @"РЕЛАКС") {
    streamURL = streamURLrelax;
} if (_detail.label == @"РЕТРО") {
    streamURL = streamURLretro;
}

    if(![self connected]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Невозможно установить соединение" message:@"Проверьте соединение с интернетом" delegate:self cancelButtonTitle:@"ОК" otherButtonTitles: nil];
        [alert show];
    } else {
        //Start stream
        AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:streamURL]];
        [playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
        music = [AVPlayer playerWithPlayerItem:playerItem];
        [music play];
    }


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playButton:)
                                             name:@"TogglePlayPause"
                                           object:nil];

//Set Title View
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 240, myScrollView.frame.size.width, 40)];
[self setStyle]; //Set style layers
titleView.text = @"Подключаюсь...";
CGFloat textLength = titleView.intrinsicContentSize.width;
[myScrollView addSubview: titleView];
[self.view addSubview:myScrollView];
if (textLength < 240) {
    [self centerText];
}

 timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self   selector:@selector(timed) userInfo:nil repeats:YES];

 }

http://dev.b4u.by/radio.jpg

2 个答案:

答案 0 :(得分:0)

你可以删除avplayer对象&#34;音乐&#34;在您的appdelegate中,然后通过这种方式访问​​它

AppDelegate *mainDelegate = [UIApplication sharedApplication].delegate;
[mainDelegate.music pause];
mainDelegate.music = nil;

当您想要启动新流时或在分配init

之前

答案 1 :(得分:0)

StreamViewController.h

@interface APStreamsViewController : UIViewController <UITableViewDataSource>
{   
    AVPlayer *musicSegue;
}

StreamViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (indexPath) {
    APStreamsData *item = [_data objectAtIndex:indexPath.row];
    [segue.destinationViewController setDetail:item];
}
    RadioViewController *music = [segue destinationViewController];
    [music setMusic: musicSegue];
}

RadioViewController.h

@property (nonatomic, strong) AVPlayer *music;

在RadioViewController中将音乐改为_music)))