2012-12-09 306 views
0

我有这个应用程序有几个视图控制器。在应用程序委托中,我将其设置为一旦应用程序启动完成,背景音乐就会启动。但是,在另一个视图控制器上,我有这个播放此视频的按钮。我的问题是,当我播放电影时,背景音频与电影重叠。我的问题是,如何在播放电影时停止播放音乐,并在电影结束后播放音乐。 这里是我的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 

回答

3

您显示的代码有一个指向player对象的局部变量。为了控制播放器,其他代码需要能够找到它。就像这样:

App_Delegate.h

@property (strong) AVAudioPlayer *player; 

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]; 
+0

错误:属性“玩家”不是类型“ID ”的对象 – PoisonNinja

+1

对不起,多个支架需要找到。我在上面的答案中将它们添加到这两行代码中。 – emrys57

+0

如何在电影播放完成后再播放? – PoisonNinja

2
set scalling mode to your player 

for Paused : 
[moviePlayerController setScalingMode:MPMoviePlaybackStatePaused]; 

for Stopped: 
[moviePlayerController setScalingMode:MPMoviePlaybackStateStopped]; 
+0

什么?我不明白。我应该在哪里放置代码? – PoisonNinja

0

如果可能在iOS上,可以使用脚本功能将消息发送到静音。我曾经在Mac OS X中使用过,这是我用来从另一个应用程序控制iTunes的地方。

+0

我该怎么做? – PoisonNinja

+0

搜索“Scripting Bridge编程指南” – Abhinav

0

先加开始到歌曲应用程序委托

.H应用程序委托

#import <UIKit/UIKit.h> 
#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVFoundation.h> 
@class ViewController; 
@interface AppDelegate : UIResponder <UIApplicationDelegate,AVAudioPlayerDelegate> 
{ 
    AVAudioPlayer *myAudioPlayer; 
    NSDictionary *config; 
    NSMutableArray *ARRAY; 

} 
-(void)stop; 
@property(retain,nonatomic) NSDictionary *config; 
@property (nonatomic, retain) AVAudioPlayer *myAudioPlayer; 
@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) ViewController *viewController; 

@end 

.M应用程序委托

@implementation AppDelegate 
    { 
     AVAudioPlayer* audioPlayer; 
    } 
    @synthesize myAudioPlayer; 
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
    { 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
     audioPlayer.delegate = self; 
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
     [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
     audioPlayer.numberOfLoops = 1; 
     audioPlayer.delegate=self; 
     [audioPlayer play]; 
} 
-(void)stop 
{ 
    [audioPlayer stop]; 

} 
-(void)star 
{ 
    [audioPlayer play]; 

} 

在使用时需要启动和停止背景音乐在应用程序

直接调用此方法-start -stop和

..它真的管用