【问题标题】:Stop background music停止背景音乐
【发布时间】:2017-11-28 00:15:20
【问题描述】:

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

【问题讨论】:

    标签: ios iphone objective-c media-player avfoundation


    【解决方案1】:

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

    【讨论】:

    • 错误:在 'id ' 类型的对象上找不到属性“player”
    • 抱歉,需要更多括号。我将它们添加到上面答案中的这两行代码中。
    • 电影播放完成后如何再次播放?
    • 这就是上面那行player play]; 所做的。
    • 在哪里添加代码行?我希望它在电影视图关闭后播放。应该放在“电影播放完成”行后面吗?
    【解决方案2】:
     set scalling mode to your player
    
    for Paused :
     [moviePlayerController setScalingMode:MPMoviePlaybackStatePaused];
    
     for Stopped:
     [moviePlayerController setScalingMode:MPMoviePlaybackStateStopped];
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 搜索“脚本桥编程指南”
      【解决方案4】:

      首先在应用程序委托中添加歌曲开始

      .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

      ..它确实有效

      【讨论】:

        猜你喜欢
        • 2010-12-12
        • 1970-01-01
        • 2016-12-29
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2016-10-14
        相关资源
        最近更新 更多