iPhone AVPlayer从PhoneGap中的远程URL流 [英] iPhone AVPlayer Streaming from Remote URL in PhoneGap
本文介绍了iPhone AVPlayer从PhoneGap中的远程URL流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们正在使用一个用Obj-C编写的PhoneGap插件在iPhone应用程序中从远程URL获取音频流。由于某些原因,它不通过扬声器播放音频,它只通过耳机播放。我们试过使用AVAudioPlayer没有运气。我需要将音频输出设置在某个地方吗?
We are working on getting audio streaming from a remote URL in a iPhone application using a PhoneGap plugin written in Obj-C. For some reason, it is not playing the audio through the speakers, it only plays through headphones. We've tried using AVAudioPlayer with no luck. Do I need to set the audio output somewhere?
--CharStream.m--
#import "CharStream.h"
#import "AppDelegate.h"
@implementation CharStream
-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView {
self = (CharStream*)[super initWithWebView:theWebView];
return self;
}
-(void) loadPlaylist:(NSArray*)arguments withDict:(NSMutableDictionary *)options {
//currentSong = 0;
//playlist = [[arguments objectAtIndex:currentSong] objectFromJSONString];
item = [[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://music.clayfreeman.com/music/Hybrid%20Minds%20-%20Inner%20Beauty.m4a"]] retain];
[item addObserver:self forKeyPath:@"playbackBufferEmpty" options:0 context:nil];
[item addObserver:self forKeyPath:@"status" options:0 context:nil];
player = [[AVPlayer alloc] initWithPlayerItem:item];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];
NSLog(@"Loading song.");
}
-(void) play:(NSArray*)arguments withDict:(NSMutableDictionary *)options {
[self play];
}
-(void) play {
[player play];
}
-(void) pause:(NSArray*)arguments withDict:(NSMutableDictionary *)options {
[player pause];
}
-(void) nextSong:(NSArray*)arguments withDict:(NSMutableDictionary *)options {
[self nextSong];
}
-(void) nextSong {
currentSong++;
item = [[AVPlayerItem playerItemWithURL:[NSURL URLWithString:[playlist objectAtIndex:currentSong]]] retain];
[item addObserver:self forKeyPath:@"status" options:0 context:nil];
[item addObserver:self forKeyPath:@"playbackBufferEmpty" options:0 context:nil];
player = [AVPlayer playerWithPlayerItem:item];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isKindOfClass:[AVPlayerItem class]]) {
AVPlayerItem* changedItem = (AVPlayerItem *)object;
if ([keyPath isEqualToString:@"playbackBufferEmpty"]) {
[item removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[self nextSong];
NSLog(@"Song over. Moving to next item.");
} else if ([keyPath isEqualToString:@"status"]) {
if (changedItem.status == AVPlayerItemStatusFailed) {
[item removeObserver:self forKeyPath:@"status"];
[self nextSong];
NSLog(@"Item failed. :( Moving to next item.");
} /*else if (changedItem.status == AVPlayerItemStatusReadyToPlay) {
[self play];
NSLog(@"Item is ready to play.");
}*/
}
} else if ([object isKindOfClass:[AVPlayer class]]) {
AVPlayer* playerLol = (AVPlayer *) object;
if ([keyPath isEqualToString:@"status"]) {
if (playerLol.status == AVPlayerStatusReadyToPlay) {
[self play];
}
}
}
}
@end
--CharStream.h--
#import <AVFoundation/AVFoundation.h>
#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVPlugin.h>
#else
#import "CDVPlugin.h"
#endif
@interface CharStream : CDVPlugin {
NSMutableArray* playlist;
AVPlayerItem* item;
int currentSong;
AVPlayer* player;
}
-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView;
-(void)loadPlaylist:(NSArray*)arguments withDict:(NSMutableDictionary*)options;
-(void)pause:(NSArray*)arguments withDict:(NSMutableDictionary*)options;
-(void)play:(NSArray*)arguments withDict:(NSMutableDictionary*)options;
-(void)play;
-(void)nextSong:(NSArray*)arguments withDict:(NSMutableDictionary*)options;
-(void)nextSong;
@end
--CharStream.js--
function CharStream() {
this.init = true;
};
CharStream.prototype.loadPlaylist = function(array, options) {
Cordova.exec("CharStream.loadPlaylist", array, options);
};
CharStream.prototype.pause = function(options) {
Cordova.exec("CharStream.pause", options);
};
CharStream.prototype.play = function(options) {
Cordova.exec("CharStream.play", options);
};
CharStream.prototype.nextSong = function(options) {
Cordova.exec("CharStream.nextSong", options);
};
Cordova.addConstructor(function()
{
if(!window.plugins)
{
window.plugins = {};
}
window.plugins.CharStream = new CharStream();
});
推荐答案
设置音频会话很重要,例如因此:
It is important to set up your audio session, like so:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
error:&setCategoryError];
if (!ok) {
NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__,
setCategoryError);
return;
}
NSError *activationError = nil;
ok = [audioSession setActive:YES error:&activationError];
if (!ok) {
NSLog(@"%s activationError=%@", __PRETTY_FUNCTION__,
activationError);
return;
}
// The audio session is OK and ready for playback.
这篇关于iPhone AVPlayer从PhoneGap中的远程URL流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文