簡體   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