1

I think everything is in the title.

I have a UIWebiew that plays music, and when the user use the power or home button, putting the app in background, I want the music to keep playing.

Is it possible ?

Thank you for your help !

4

4 に答える 4

5

UIWebViewを使用してこれを実際に行うことはできませんが、それでも行うことができます。このメソッドは、URLを介してオンラインリソースからオーディオをストリーミングします。ここにあなたを助けるためのいくつかのコードがあります。ここで、アプリがビデオの終了時にオーディオをストリーミングしたいとします。これは非常に似ていますが、AVAudioPlayerフレームワークの代わりにMediaPlayerフレームワークを使用します。

NSURL *url = [NSURL URLWithString:@"http://website.com/audio.mp3"];

NSData *data = [NSData dataWithContentsOfURL:url];

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];

[audioPlayer play];
于 2012-08-15T01:39:23.740 に答える
3

A little tricky but it is possible. From the official Apple doc below:

https://developer.apple.com/library/ios/qa/qa1668/_index.html

You need to first declare in your plist the UIBackgroundModes for audio, and then write the following code snippet in your AppDelegate

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }

NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }

Now when you have audio playing from a UIWebView, you can go to the home screen, change to another app, or lock the screen and the audio will continue playing.

于 2014-06-24T03:59:20.257 に答える
1

I don't believe this is possible, and even after checking UIWebView's documentation I only found the following three attributes related to media playback.

  allowsInlineMediaPlayback  property
  mediaPlaybackRequiresUserAction  property
  mediaPlaybackAllowsAirPlay  property

Unfortunately, none of them do this.

于 2012-08-13T16:20:57.820 に答える
0

Yes it is possible. You need to do:

  1. Activate playback session:

@import AVFoundation; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

  1. Add audio background mode in your Info.plist:

Required background modes: App plays audio or streams audio/video using AirPlay

<key>UIBackgroundModes</key> <array> <string>audio</string> </array>

于 2015-09-15T07:13:57.660 に答える