【问题标题】:MPMoviePlayerController playing audio stream in the backgroundMPMoviePlayerController 在后台播放音频流
【发布时间】:2012-02-25 16:05:19
【问题描述】:

当应用程序进入后台时,我在播放音频流时遇到了问题。

我使用代码启动流:

NSURL *mediaURL = [NSURL URLWithString:@"http://url.to.my.stream"];

MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];

[[NSNotificationCenter defaultCenter] addObserver:self 

                                         selector:@selector(moviePlayBackDidFinish:) 

                                             name:MPMoviePlayerPlaybackDidFinishNotification 

                                           object:nil]; 



[mp setControlStyle:MPMovieControlStyleFullscreen];

[mp setMovieSourceType:MPMovieSourceTypeStreaming];

[mp setFullscreen:YES];



[self.view addSubview:[mp view]];



[mp prepareToPlay];

[mp play];

完美运行。但是,尽管我在应用程序进入后台时在属性列表中设置了标志“应用程序播放音频”,但流停止播放。

如何让我的应用程序在后台播放音频流?

最好的问候,非常感谢您的帮助!

【问题讨论】:

    标签: ios mpmovieplayercontroller


    【解决方案1】:

    我自己没有尝试过,但这看起来很有希望:iOS Multitasking: Background Audio

    创建项目后,转到 APP-Info.plist 并添加 UIBackgroundModes 作为新行。然后它应该创建数组。

    打开数组并在第 0 项的右侧将其设置为音频。

    编辑

    您的 AVAudioSession 设置正确吗?

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:YES error:nil];
    

    【讨论】:

    • 是的,我这样做了,但是没有用。在本教程中,这个人正在使用 AVAudio 框架。我正在使用媒体播放器,因为 AV 框架由于某种原因不播放我的音频流...
    【解决方案2】:

    此代码对我有用,首先您必须授予您的应用权限以继续在后台播放音乐(在您的 .plis 中),然后转到希望的类并实现此代码,首先是导入和方法播放音乐。

    #import <MediaPlayer/MPNowPlayingInfoCenter.h>
    #import <MediaPlayer/MPMediaItem.h>
    #import <AVFoundation/AVFoundation.h>
    

    ---- o ----

    -(void) playMusic{
    
         [[AVAudioSession sharedInstance] setDelegate: self];
    
         NSError *myErr;
    
         // Initialize the AVAudioSession here.
        if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr]) {
           // Handle the error here.
           NSLog(@"Audio Session error %@, %@", myErr, [myErr userInfo]);
        }else{
           // Since there were no errors initializing the session, we'll allow       begin receiving remote control events
           [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        }
    
        //initialize our audio player
        audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.cocoanetics.com/files/Cocoanetics_031.mp3"]];
    
        [audioPlayer setShouldAutoplay:NO];
        [audioPlayer setControlStyle: MPMovieControlStyleEmbedded];
        audioPlayer.view.hidden = YES;
    
        [audioPlayer prepareToPlay];
    
        [audioPlayer play];
    }//end playmusic
    

    【讨论】:

    • 如果您能在代码中提供解释,将会有所帮助。突出显示不同之处以及如何使其发挥作用。仅代码的答案是不受欢迎的。
    猜你喜欢
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多