0

いくつかのView Controllerを持つこのアプリがあります。アプリ デリゲートでは、アプリの起動が完了するとすぐに BGM が開始されるように設定しました。ただし、別のView Controllerには、このビデオを再生するこのボタンがあります。私の問題は、ムービーを再生すると、バックグラウンド オーディオがムービーとオーバーラップすることです。私の質問は、映画の再生中に音楽を停止し、映画の終了後に音楽を再生するにはどうすればよいかということです。ここに私の app_delegate.h があります:

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

@interface App_Delegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

ここに私の App_Delegate.m があります

#import "App_Delegate.h"
#import "RootViewController.h"


@implementation App_Delegate

@synthesize window;
@synthesize navigationController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    {NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"beethoven_sym_5_i" ofType:@"mp3"];
        NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
        player.numberOfLoops=-1;
        [player play];
    }


    // Override point for customization after application launch.

    // Set the navigation controller as the window's root view controller and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}


- (void)dealloc {
    [navigationController release];
    [window release];
    [super dealloc];
}


@end

私の MovieViewController.h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface MovieViewController : UIViewController {

    IBOutlet UIScrollView *sesamescroller;
}

- (IBAction)playsesamemovie:(id)sender;


@end

最後に、私の MovieViewController.m

#import "MovieViewController.h"


@interface MovieViewController ()

@end

@implementation MovieViewController

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

- (void)viewDidLoad


- (void)viewDidUnload


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)playsesamemovie:(id)sender {
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"How to make Sesame chicken" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];
}


- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];
    [moviePlayerController.view removeFromSuperview];
    [moviePlayerController release];
}

- (void)dealloc {
    [sesamescroller release];
    [super dealloc];
}
@end
4

4 に答える 4

3

player表示するコードには、オブジェクトを指すローカル変数があります。プレーヤーを制御するには、他のコードがプレーヤーを見つけられる必要があります。このような:

App_Delegate.h

@property (strong) AVAudioPlayer *player;

in App_Delegate.m: (このアンダーバーはどこから来たの? 型にはまらない!)

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.player.numberOfLoops=-1;
[self.player play];

次に、それを制御したい場所:

[((App_Delegate *)([UIApplication sharedApplication].delegate)).player pause];
// ...
[((App_Delegate *)([UIApplication sharedApplication].delegate)).player play];
于 2012-12-09T22:58:01.523 に答える
2
 set scalling mode to your player

for Paused :
 [moviePlayerController setScalingMode:MPMoviePlaybackStatePaused];

 for Stopped:
 [moviePlayerController setScalingMode:MPMoviePlaybackStateStopped];
于 2012-12-12T05:42:34.430 に答える
0

iOS で可能であれば、スクリプト可能な機能を使用してメッセージをミュートに送信できます。以前は別のアプリから iTunes を制御していた Mac OS X でこれを行いました。

于 2012-12-12T06:21:48.063 に答える