后台模式下的 MPMoviePlayerController

发布于 2024-11-03 23:01:08 字数 1749 浏览 0 评论 0原文

我正在尝试使用 MPMoviePlayerController 从特定 URL 播放流媒体文件。当我加载播放器时它工作正常,但当应用程序进入后台模式(多任务处理)时我需要保持它运行。这就是我正在做的事情:

-(void) playAudio :(NSString *)playAudioURL
{

    //NSURLRequest *audioFile = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:self.playAudioURL] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];  

    MPMoviePlayerController *mediaPlayer  = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:playAudioURL]];

    mediaPlayer.scalingMode = MPMovieScalingModeAspectFill; 

    mediaPlayer.scalingMode = MPMovieScalingModeAspectFill;

    mediaPlayer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home-bg.png"]];

    //[self.view addSubview:mediaPlayer.view];  

    mediaPlayer.fullscreen = YES; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(myMovieFinishedCallback:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:mediaPlayer]; 


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoPreloadCallback:)
                                                 name:MPMoviePlayerContentPreloadDidFinishNotification
                                               object:mediaPlayer];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

    [mediaPlayer play];         

}

现在,当我在运行音频时按下 iPhone 主页按钮时,它会暂停流式传输,直到我加载回应用程序。当应用程序进入后台时,我需要保持它运行。

I am trying to play streaming file from particular URL using MPMoviePlayerController. It is working fine when I load the player but I need to keep it running when application enter into the background mode (Multitasking). This is what I am doing:

-(void) playAudio :(NSString *)playAudioURL
{

    //NSURLRequest *audioFile = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:self.playAudioURL] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];  

    MPMoviePlayerController *mediaPlayer  = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:playAudioURL]];

    mediaPlayer.scalingMode = MPMovieScalingModeAspectFill; 

    mediaPlayer.scalingMode = MPMovieScalingModeAspectFill;

    mediaPlayer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home-bg.png"]];

    //[self.view addSubview:mediaPlayer.view];  

    mediaPlayer.fullscreen = YES; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(myMovieFinishedCallback:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:mediaPlayer]; 


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoPreloadCallback:)
                                                 name:MPMoviePlayerContentPreloadDidFinishNotification
                                               object:mediaPlayer];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

    [mediaPlayer play];         

}

Now when I press iPhone home button while running the audio, it pauses the streaming until I load back the Application. I need to keep it running when app enter into background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-11-10 23:01:08

在您的 plist 文件中,您是否将 UIBackgroundModes 键设置为 audio ?如果没有这个,您的应用程序将不会在后台播放任何声音。

请注意,在 iOS5 中,当您编辑 plist 文件时,UIBackgroundModes 现在被引用为必需的背景模式

In your plist file, did you set the UIBackgroundModes key to audio ? Without this, your application will not play any sound in background

Note that in iOS5, UIBackgroundModes is now referenced as Required background modes when you edit your plist file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文