Cordova Media Plugin没有实现audioPlayerEndInterruption

时间:2014-07-18 16:58:23

标签: ios objective-c cordova plugins avaudioplayer

我创建了一个iOS应用,可以在应用在后台运行时播放音频。如果音频中断(即:电话),音频将停止并且永不恢复。

我认为这是因为cordova媒体插件没有实现audioPlayerEndInterruption

我对Javascript很了解但对Objective-C几乎一无所知。有没有人有关于如何添加此功能的建议?

是否有一个不同的媒体插件实现了audioPlayerEndInterruption,还是有一种简单的方法将audioPlayerEndInterruption合并到cordova插件中?

3 个答案:

答案 0 :(得分:2)

<强> CDVSound.h -modify:add:,MEDIA_END_INTERRUPT = 5

enum CDVMediaStates {
    MEDIA_NONE = 0,
    MEDIA_STARTING = 1,
    MEDIA_RUNNING = 2,
    MEDIA_PAUSED = 3,
    MEDIA_STOPPED = 4,
    MEDIA_END_INTERRUPT = 5
};

<强> CDVSound.m -add:

- (void)audioPlayerEndInterruption:(AVAudioPlayer*)player successfully:(BOOL)flag
{
    CDVAudioPlayer* aPlayer = (CDVAudioPlayer*)player;
    NSString* mediaId = aPlayer.mediaId;
    CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
    NSString* jsString = nil;

    if (audioFile != nil) {
        NSLog(@"Ended Interruption of playing audio sample '%@'", audioFile.resourcePath);
    }
    if (flag) {
        jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_END_INTERRUPT];
    } else {
        jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
    }

    [self.commandDelegate evalJs:jsString];
}

<强> Media.js -modify:add:Media.MEDIA_END_INTERRUPT = 5; AND,&#34; EndInterrupt&#34;

// Media states
Media.MEDIA_NONE = 0;
Media.MEDIA_STARTING = 1;
Media.MEDIA_RUNNING = 2;
Media.MEDIA_PAUSED = 3;
Media.MEDIA_STOPPED = 4;
Media.MEDIA_END_INTERRUPT = 5;
Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped", "EndInterrupt"];

然后找Media.onStatus = function(id,msgType,value){并在Media.MEDIA_STATE:block ...

的情况下添加条件
 if(value == Media.MEDIA_END_INTERRUPT) {
     //do whatever you want
 }

答案 1 :(得分:1)

刚刚跟进。我最终为此编写了一个插件,它覆盖了一些默认的cordova媒体插件(v0.2.12)方法。我在这里发布了一个链接,但代码很乱。我只是想尝试一些有效的东西而不是真正计划分享。但这里是(没有提供文件,抱歉):

https://github.com/stevethorson/Interruptible-Cordova-Media.git

答案 2 :(得分:0)

我使用getCurrentPosition方法从媒体插件本身解决了此问题,该方法可以检测流是否被电话或语音邮件打扰了,然后停止并再次播放音频。我希望这可以帮助我永远带我去使用媒体插件和广播的人。

var mediaplayer = new Media(streamingUrl, mediaSuccess, mediaFail, mediaState);
mediaplayer.play({playAudioWhenScreenIsLocked :true});

var prevPos = 0;
        var mediaTimer = setInterval(function () {
            // get media position
            mediaplayer.getCurrentPosition(
                // success callback
                function (currentposition) {
                    if (currentposition > prevPos) {                        
                        prevPos = currentposition;                        
                    } else if (currentposition === prevPos) {                   
                        stop();
                        play();
                    } else {
                        console.log("can't get pos");
                    }
                },
                // error callback
                function (e) {
                    alert+("Error getting pos=" + e);
                }
            );
        }, 5000);