ios - 停止背景音乐

标签 ios iphone objective-c media-player avfoundation

我的这个应用有多个 View Controller 。在应用程序委托(delegate)中,我将其设置为一旦应用程序完成启动,背景音乐就会开始。但是,在另一个 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

最佳答案

您显示的代码有一个指向 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];

关于ios - 停止背景音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13792668/

相关文章:

iphone - 如何在 UITableView 中显示 NSSet?

iphone - 如何通过 NSOutputStream 发送整数?

ios - UILabel 文本在两个字符串之间变化,每个字符串为 "touch up inside"

objective-c - UICollectionViewLayout 教程

ios - 提交到应用商店时出现 UIRequiredDeviceCapabilities 错误

iphone - IOS在keychain中存储多个密码

objective-c - 使用 UIImagePNGRepresentation 保存 UIImage 不保留我的图层修改

ios - Apptentive Message 单元格颜色变化

iphone - iPhone开发中如何访问不同类栈中的对象?

iphone - 如何将多个项目合并为一个项目