iPhone AVPlayer从PhoneGap中的远程URL流式传输
iPhone AVPlayer Streaming from Remote URL in PhoneGap
我们正在使用用Obj-C编写的PhoneGap插件从iPhone应用程序中的远程URL获取音频流。出于某种原因,它不是通过扬声器播放音频,而是通过耳机播放。我们尝试过使用AVAudioPlayer,但没有成功。我需要在某个地方设置音频输出吗?
--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();
});
设置音频会话很重要,如下所示:
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.
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 使用php或javascript从facebook相册URL中删除多余的部分
- Angular JS IE9 Hashbang url rewriting
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 直接下载文件,而不是从window.open(url)
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 通过js在新选项卡中有条件地打开url
- CKFinder 3为所选文件返回错误的URL
- 如何获取不属于我项目的上一页的URL
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 在URL中传递JS对象
- 将纯文本URL转换为可单击链接
- Javascript html每点击一次就会更改url
- iPhone AVPlayer从PhoneGap中的远程URL流式传输
- 如何使用Javascript/jQuery将GET值从URL从PAGE1传输到JSON Web服务
- 将javascript变量传输到插件函数(异步URL)
- Ember传输到URL不起作用
- 从经过验证的url流式传输音频
- 当通过文件传输发送时,从相机发送的图像URL错误
- 正确解析通过JSON传输的URL