首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止背景音乐

停止背景音乐
EN

Stack Overflow用户
提问于 2012-12-10 06:43:04
回答 4查看 1.7K关注 0票数 0

我有一个应用程序,它有几个视图控制器。在应用程序代理中,我将其设置为一旦应用程序完成启动,背景音乐就会开始播放。但是,在另一个视图控制器上,我有一个播放此视频的按钮。我的问题是,当我播放电影时,背景音频与电影重叠。我的问题是,我如何在播放电影时停止音乐,并在电影结束后播放音乐。这是我的app_delegate.h:

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-10 06:58:02

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

App_Delegate.h

代码语言:javascript
复制
@property (strong) AVAudioPlayer *player;

App_Delegate.m:(这个underbar是从哪里来的?最不符合常规的!)

代码语言:javascript
复制
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.player.numberOfLoops=-1;
[self.player play];

然后,在你想控制它的任何地方:

代码语言:javascript
复制
[((App_Delegate *)([UIApplication sharedApplication].delegate)).player pause];
// ...
[((App_Delegate *)([UIApplication sharedApplication].delegate)).player play];
票数 3
EN

Stack Overflow用户

发布于 2012-12-12 13:42:34

代码语言:javascript
复制
 set scalling mode to your player

for Paused :
 [moviePlayerController setScalingMode:MPMoviePlaybackStatePaused];

 for Stopped:
 [moviePlayerController setScalingMode:MPMoviePlaybackStateStopped];
票数 2
EN

Stack Overflow用户

发布于 2012-12-12 14:21:48

如果可能,在iOS上,您可以使用脚本化功能将消息发送到静音。我曾经在Mac上这样做过,在那里我曾经通过另一个应用程序控制iTunes。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13792668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档