繁体   English   中英

从App Deligate UISplitViewController(MobileVLCKit)中的DetailViewController访问活动的Mediaplayer

[英]Access active Mediaplayer from DetailViewController in App Deligate UISplitViewController (MobileVLCKit)

我目前正在使用MobileVLCKit框架开发视频流iOS应用。 要浏览不同的发射器,我使用UISplitViewController 在锁定iPad 3的屏幕之前,该应用程序运行良好。我调试了代码,并知道播放器仍在后台运行。 我必须访问媒体播放器属性(_mediaplayer)才能停止播放器,如果该播放器辞职或在应用程序限定中进入后台。

我已经在www和stackoverflow上进行了搜索,但这似乎是我可能看不到或误解的一小部分。 如果有人可以花一点时间帮助我发现问题,我将感到非常高兴。

顺便说一句 是的,在使用了将近两年后,我在SO上发表了第一篇文章:D

问题:如何访问appDeligate.m中的属性?

AppDeligate.h

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UISplitViewController *splitViewController;
}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) DetailViewController *viewController;

@end

AppDeligate.m

#import "AppDelegate.h"
#import "DetailViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    DetailViewController *thePlayer = [[splitViewController viewControllers] objectAtIndex:1];

    if(thePlayer._mediaplayer.isPlaying == true) {
        NSLog(@"Player is playing");
    }

    NSLog(@"Background");
}

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>

@property (strong, nonatomic) VLCMediaPlayer* _mediaplayer;
@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) IBOutlet UIView *movieView;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;

@end

DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
@end

@implementation DetailViewController

@synthesize _mediaplayer;


#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
    NSLog(@"setDetailItem");
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)mediaPlayerStateChanged:(NSNotification *)aNotification
{
    VLCMediaPlayerState currentState = _mediaplayer.state;

    /* Playback opening */
    if (currentState == VLCMediaPlayerStateOpening) {
        [self.indicator startAnimating];
        NSLog(@"Openning");
    }

    /* or if playback ended */
    if (currentState == VLCMediaPlayerStateEnded || currentState == VLCMediaPlayerStateStopped) {
        NSLog(@"Stopped");
        [self.indicator stopAnimating];
    }

    /* Playback buffering */
    if (currentState == VLCMediaPlayerStateBuffering) {
        NSLog(@"buffering");
        [self.indicator startAnimating];
    }

    /* Playback buffering */
    if (currentState == VLCMediaPlayerStatePlaying) {
        NSLog(@"Playing");
        [self.indicator stopAnimating];
    }
}

- (void)configureView
{
    NSLog(@"configureView");
    // Update the user interface for the detail item.

    if (self.detailItem) {

        self.imageView.image = nil;

        if(self._mediaplayer.isPlaying) {
            // Stop active media
            [self._mediaplayer stop];
        }

        // Set new url
        self._mediaplayer.media = [VLCMedia mediaWithURL:[NSURL URLWithString:_detailItem]];

        // Play new media
        [self._mediaplayer play];

        // Set radio image
        if ([_detailItem rangeOfString:@"239.192.1"].location != NSNotFound) {
            [self.imageView setImage:[[UIImage imageNamed:@"music.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
            self.imageView.contentMode = UIViewContentModeCenter;
        }


    }
}

- (void)viewDidDisappear
{
    NSLog(@"viewDidDisappear");
    if (self._mediaplayer) {
        @try {
            [self._mediaplayer removeObserver:self forKeyPath:@"time"];
            [self._mediaplayer removeObserver:self forKeyPath:@"remainingTime"];
        }
        @catch (NSException *exception) {
            NSLog(@"we weren't an observer yet");
        }
        if (self._mediaplayer.media)
            [self._mediaplayer stop];

        if (self._mediaplayer)
            self._mediaplayer = nil;
    }
}

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];

    // Hide navbar
    [[self navigationController] setNavigationBarHidden:YES animated:YES];

    // Background logo
    [self.imageView setImage:[[UIImage imageNamed:@"background.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
    self.imageView.contentMode = UIViewContentModeCenter;

    // Init media player
    [self initMediaplayer];
}

- (void)initMediaplayer
{
    self._mediaplayer = [[VLCMediaPlayer alloc] init];
    self._mediaplayer.delegate = self;
    self._mediaplayer.drawable = self.movieView;
    NSLog(@"Init VLC Player");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Split view

- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:
(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return YES;
}


@end

splitViewController设置@interface中的实例变量,然后进行更改:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    DetailViewController *thePlayer = [[splitViewController viewControllers] objectAtIndex:1];
    [thePlayer._mediaplayer stop];

    NSLog(@"Resign");
}

这是对我有用的最终代码。 随时使用!

AppDelegate.h

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) DetailViewController *viewController;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "DetailViewController.h"

@interface AppDelegate () {
    UISplitViewController *splitViewController;
    UINavigationController *navigationController;
    VLCMedia *currentPlayback;
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    splitViewController = (UISplitViewController *)self.window.rootViewController;
    navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"WillResignActive");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"DidEnterBackground");

    DetailViewController *dvc = (DetailViewController *)navigationController.topViewController;
    if (dvc._mediaplayer.isPlaying) {
        [dvc._mediaplayer stop];
        NSLog(@"Mediaplyer stopped");
        currentPlayback = dvc._mediaplayer.media;
        NSLog(@"Saved current playback");

        while (dvc._mediaplayer.isPlaying) {
            [NSThread sleepForTimeInterval:0.1];
        }
    }

    if (dvc._mediaplayer != nil) {
        dvc._mediaplayer = nil;
        NSLog(@"Set player to nil");
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"WillenterForeground");
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"DidBecomeActive");
    DetailViewController *dvc = (DetailViewController *)navigationController.topViewController;
    if (dvc._mediaplayer == nil) {
        dvc._mediaplayer = [[VLCMediaPlayer alloc] init];
        dvc._mediaplayer.delegate = self;
        dvc._mediaplayer.drawable = dvc.movieView;
        NSLog(@"init mediaplayer");
    }

    if (currentPlayback != nil || dvc._mediaplayer.media == nil) {
        dvc._mediaplayer.media = currentPlayback;
        NSLog(@"set current playback media");
    }

    if (dvc._mediaplayer.media != nil || dvc._mediaplayer.isPlaying == false) {
        [dvc._mediaplayer play];
        NSLog(@"play");
    }
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

DetailViewController.h

#import <UIKit/UIKit.h>
#import <MobileVLCKit/MobileVLCKit.h>

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UITapGestureRecognizer *tapGestureRecognizer;
@property (strong, nonatomic) VLCMediaPlayer* _mediaplayer;
@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) IBOutlet UIView *movieView;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;

@end

DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
@property (strong, nonatomic) UIBarButtonItem *barButton;
@end

@implementation DetailViewController

@synthesize _mediaplayer;

- (IBAction)handleTap:(UITapGestureRecognizer *)sender {
    [_barButton.target performSelector: _barButton.action withObject: _barButton];
}

#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
    NSLog(@"setDetailItem");
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)mediaPlayerStateChanged:(NSNotification *)aNotification
{
    VLCMediaPlayerState currentState = _mediaplayer.state;

    /* Playback opening */
    if (currentState == VLCMediaPlayerStateOpening) {
        [self.indicator startAnimating];
        NSLog(@"Openning");
    }

    /* or if playback ended */
    if (currentState == VLCMediaPlayerStateEnded || currentState == VLCMediaPlayerStateStopped) {
        NSLog(@"Stopped");
        [self.indicator stopAnimating];
    }

    /* Playback buffering */
    if (currentState == VLCMediaPlayerStateBuffering) {
        NSLog(@"buffering");
        [self.indicator startAnimating];
    }

    /* Playback buffering */
    if (currentState == VLCMediaPlayerStatePlaying) {
        NSLog(@"Playing");
        [self.indicator stopAnimating];
    }
}

- (void)configureView
{
    NSLog(@"configureView");
    // Update the user interface for the detail item.

    if (self.detailItem) {

        self.imageView.image = nil;

        if(_mediaplayer.isPlaying) {
            // Stop active media
            [_mediaplayer stop];
        }

        // Set new url
        _mediaplayer.media = [VLCMedia mediaWithURL:[NSURL URLWithString:_detailItem]];

        // Play new media
        [_mediaplayer play];

        // Set radio image
        if ([_detailItem rangeOfString:@"239.192.1"].location != NSNotFound) {
            [self.imageView setImage:[[UIImage imageNamed:@"music.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
            self.imageView.contentMode = UIViewContentModeCenter;
        }
    }
}

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];

    // Hide navbar
    [[self navigationController] setNavigationBarHidden:YES animated:YES];

    // Show navbar
    [_barButton.target performSelector: _barButton.action withObject: _barButton afterDelay:1];

    // Background logo
    [self.imageView setImage:[[UIImage imageNamed:@"background.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
    self.imageView.contentMode = UIViewContentModeCenter;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Split view

- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:
(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return YES;
}

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    _barButton = barButtonItem;
}
@end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM