如何在启动播放器之前检查互联网

时间:2014-01-16 11:43:58

标签: ios avplayer reachability

我有一个播放在线媒体的代码。我正在播放歌曲时实施网络检查。这意味着,如果互联网在播放歌曲时播放,它将显示警报。但问题是,如果我没有连接到互联网,然后我打开应用程序,如果我按下播放按钮,它不会检查互联网,也不会显示警报。如何在播放器开始播放歌曲之前检查互联网。如果没有互联网,播放器应显示警报而不是暂停按钮。

这是我的代码部分:

#import "FirstViewController.h"
CM_EXPORT const CMTime kCMTimeZero;
@interface FirstViewController ()
@end


@implementation FirstViewController
@synthesize  metadatas;
@synthesize toggleButton;
@synthesize slider;
@synthesize mpVolumeView = _mpVolumeView;
@synthesize viewVolume;

- (void)viewDidLoad
{

    toggleIsOn=TRUE;

    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:self.viewVolume.bounds] ;

    [self.viewVolume addSubview:volumeView];

    [volumeView sizeToFit];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

}

-(IBAction)playButtonPressed:(id)sender
{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:TRUE forKey:@"FirstPlay"];
    [defaults setBool:YES forKey:@"alertShown"];

        if(toggleIsOn){

        toggleIsOn=!toggleIsOn;

        player = nil;
        NSString *stringurl = @"";
        stringurl = @"http://majestic.wavestreamer.com:6221/listen.pls";
        NSURL *url = [NSURL URLWithString:stringurl];
        asset = [AVURLAsset URLAssetWithURL:url options:nil];
        playerItem = [AVPlayerItem playerItemWithAsset:asset];
        player = [AVPlayer playerWithPlayerItem:playerItem];
        player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
        [playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
        [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        [player play];
        isPlaying = TRUE;
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];

        [self.toggleButton setImage:[UIImage imageNamed:@"reload.png"] forState:UIControlStateNormal];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance] setActive: YES error: nil];
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    }
    else {

        [self.toggleButton setImage:[UIImage imageNamed:@"playMusic.png"] forState:UIControlStateNormal];
        self->player.rate=0.0;
        toggleIsOn=!toggleIsOn;
        isPlaying = FALSE;
    }

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:TRUE forKey:@"alertShown"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [Reachability reachabilityWithHostName:@"www.apple.com"];
    [hostReachable startNotifier];


}

检查网络的代码

-(void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            NSLog(@"%d",[defaults boolForKey:@"alertShown"]);
            BOOL isAlertShown = [defaults boolForKey:@"alertShown"];
            if(isAlertShown)
           {
               isPlaying = false;
               [self showAlert];
           }

            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            if(self.alert)
            {
                [self.alert dismissWithClickedButtonIndex:0 animated:YES];
                self.alert = nil;
            }

            BOOL isFirstTimePlayed = [defaults boolForKey:@"FirstPlay"];
            if(!isPlaying)
            {
            if(isFirstTimePlayed)
            {
                [self playButtonPressed:nil];
            }
            }

            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            if(isPlaying)
            {
            [self playButtonPressed:nil];
            }
            if(self.alert)
            {
                [self.alert dismissWithClickedButtonIndex:0 animated:YES];
                self.alert = nil;
            }
            break;
        }
    }

   }


-(void)showAlert
{
    //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //[defaults setBool:FALSE forKey:@"alertShown"];

    //alert = [[UIAlertView alloc] initWithTitle: @"Alert" message: @"You have lost data connectivity. Please wait while we try to establish the connection again." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    //[alert show];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:FALSE forKey:@"alertShown"];

    self.alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                                    message:@"You have lost data connectivity. Please wait while we try to establish the connection again."
                                                                   delegate:self
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil];
    [self.alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
        if(!isPlaying)
        {
            [player pause];
            [self.toggleButton setImage:[UIImage imageNamed:@"playMusic.png"] forState:UIControlStateNormal];
        }
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}    
@end

在播放歌曲之前,应该执行检查互联网的功能。

0 个答案:

没有答案