0

我们正在使用用 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();

});
4

1 回答 1

2

设置音频会话很重要,如下所示:

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.
于 2012-08-28T14:01:41.490 回答