0

当我在真实设备上测试我的收音机 iOS 应用程序时遇到问题,它运行良好,但是当屏幕或设备关闭时,收音机停止并关闭,为什么会发生这种情况?以及如何使活动指示器视图停止移动并在收音机打开并完成加载时隐藏它???

(void)viewDidLoad
{
    [super viewDidLoad];
    timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(uodatetimer) userInfo:nil repeats:YES];
    player = [[MPMoviePlayerController alloc] initWithContentURL:   
                                       [NSURL URLWithString:@"http://46.43.64.50:8008/AJYAL.m3u"]];
    player.movieSourceType = MPMovieSourceTypeStreaming;
    player.view.hidden = YES;
    [self.view addSubview:player.view];
    [player prepareToPlay];
    [player play];
   // player.useApplicationAudioSession=NO;
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    spinner.frame = CGRectMake(0.0, 0.0, 20.0, 20.0);

    spinner.center=self.view.center;

    [spinner startAnimating];

    [self.view addSubview:spinner];

    MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:
                                 CGRectMake(0, 0, 105, 15)] autorelease];
    volumeView.center = CGPointMake(152,372);
    [volumeView setShowsVolumeSlider:YES];
    [volumeView setShowsRouteButton:YES];
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];

}
4

3 回答 3

1

您需要做 2 件事才能在后台启用音频播放:

  • 使用“App 播放音频”值将“必需的背景模式”键添加到 Info.plist 文件中。
  • 将音频会话类别设置为 AVAudioSessionCategoryPlayback 并激活音频会话:

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

于 2013-01-21T08:38:52.210 回答
0

为了能够让它在后台播放,您需要在项目 .plist 文件中启用UIBackgroundModes人类可读格式的必需背景模式,并在数组中启用第一个字符串行写入音频。这将使您的应用程序在屏幕关闭后在后台运行。

于 2013-01-02T12:26:41.773 回答
0

yourProject-pList添加这一行

UIBackgroundModes

于 2013-01-02T12:39:34.470 回答