javascript - Setting html5 media source using ajax - Stack Overflow

I needed to aid authentication headers for my audio files i was grabbing from a external server. So now

I needed to aid authentication headers for my audio files i was grabbing from a external server. So now im trying to use ajax, i can grab the files fine, but i cant set them as the media source for my player. How do you approach setting a ajax loaded file as a audio source?

EDIT

Ended up fixing it in case someone es back this way.

if (this.mAudioPlayer.canPlayType("audio/mpeg")) {
    this.mExtension = '.mp3';
}else if (this.mAudioPlayer.canPlayType("audio/ogg")) {
    this.mExtension = '.ogg';
} else if (this.mAudioPlayer.canPlayType("audio/mp4")) {
    this.mExtension = '.m4a'; 
}

this.CreateAudioData = function() {

    //downloading audio for use in data:uri
    $.ajax({
        url: aAudioSource + this.mExtension + '.txt',
        type: 'GET',
        context: this,
        async: false,
        beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', window.userId);},
        success: this.EncodeAudioData,
        error: function(xhr, aStatus, aError) { HandleError('Audio Error: ' + aStatus); }
    });
};

this.EncodeAudioData = function(aData) {
    //this.mAudioData = base64_encode(aData);
    this.mAudioData = aData;

    if (this.mExtension == '.m4a') {
        Debug("playing m4a");
        this.mAudioSrc = "data:audio/mp4;base64," + this.mAudioData;
    } else if (this.mExtension == '.ogg') {
        Debug("playing ogg");
        this.mAudioSrc = "data:audio/ogg;base64," + this.mAudioData;
    } else if (this.mExtension == '.mp3') {
        Debug("playing mp3");
        this.mAudioSrc = "data:audio/mp3;base64," + this.mAudioData;
    }

};

this.play = function() {

    if (this.mAudioPlayer.src != this.mAudioSrc) {
        this.mAudioPlayer.src = this.mAudioSrc;
    }
    this.mAudioPlayer.load();
    this.mAudioPlayer.play();
};

Had to do asynch:false, otherwise i would get a small chunk of the audio instead of all of it. Though removing the asynch made debugging easier in the end.

I needed to aid authentication headers for my audio files i was grabbing from a external server. So now im trying to use ajax, i can grab the files fine, but i cant set them as the media source for my player. How do you approach setting a ajax loaded file as a audio source?

EDIT

Ended up fixing it in case someone es back this way.

if (this.mAudioPlayer.canPlayType("audio/mpeg")) {
    this.mExtension = '.mp3';
}else if (this.mAudioPlayer.canPlayType("audio/ogg")) {
    this.mExtension = '.ogg';
} else if (this.mAudioPlayer.canPlayType("audio/mp4")) {
    this.mExtension = '.m4a'; 
}

this.CreateAudioData = function() {

    //downloading audio for use in data:uri
    $.ajax({
        url: aAudioSource + this.mExtension + '.txt',
        type: 'GET',
        context: this,
        async: false,
        beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', window.userId);},
        success: this.EncodeAudioData,
        error: function(xhr, aStatus, aError) { HandleError('Audio Error: ' + aStatus); }
    });
};

this.EncodeAudioData = function(aData) {
    //this.mAudioData = base64_encode(aData);
    this.mAudioData = aData;

    if (this.mExtension == '.m4a') {
        Debug("playing m4a");
        this.mAudioSrc = "data:audio/mp4;base64," + this.mAudioData;
    } else if (this.mExtension == '.ogg') {
        Debug("playing ogg");
        this.mAudioSrc = "data:audio/ogg;base64," + this.mAudioData;
    } else if (this.mExtension == '.mp3') {
        Debug("playing mp3");
        this.mAudioSrc = "data:audio/mp3;base64," + this.mAudioData;
    }

};

this.play = function() {

    if (this.mAudioPlayer.src != this.mAudioSrc) {
        this.mAudioPlayer.src = this.mAudioSrc;
    }
    this.mAudioPlayer.load();
    this.mAudioPlayer.play();
};

Had to do asynch:false, otherwise i would get a small chunk of the audio instead of all of it. Though removing the asynch made debugging easier in the end.

Share Improve this question edited Sep 19, 2012 at 20:54 Neablis asked Sep 17, 2012 at 22:24 NeablisNeablis 9242 gold badges12 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Are you actually downloading the file, or returning it in base64 encoded format (i.e. as a Data URI)?

Changing the source of an audio element via JavaScript is quite straightforward.

<audio id="myAudio" controls />

And then once you have the source,:

var audio = document.getElementById("myAudio");
audio.src = myAudioFile;
audio.type = "type/ogg"; // ony showing an OGG example here
if (this.mAudioPlayer.canPlayType("audio/mpeg")) {
    this.mExtension = '.mp3';
}else if (this.mAudioPlayer.canPlayType("audio/ogg")) {
    this.mExtension = '.ogg';
} else if (this.mAudioPlayer.canPlayType("audio/mp4")) {
    this.mExtension = '.m4a'; 
}

this.CreateAudioData = function() {

//downloading audio for use in data:uri
$.ajax({
    url: aAudioSource + this.mExtension + '.txt',
    type: 'GET',
    context: this,
    async: false,
    beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', window.userId);},
    success: this.EncodeAudioData,
    error: function(xhr, aStatus, aError) { HandleError('Audio Error: ' + aStatus); }
  });
};

this.EncodeAudioData = function(aData) {
  //this.mAudioData = base64_encode(aData);
  this.mAudioData = aData;

  if (this.mExtension == '.m4a') {
    Debug("playing m4a");
    this.mAudioSrc = "data:audio/mp4;base64," + this.mAudioData;
  } else if (this.mExtension == '.ogg') {
    Debug("playing ogg");
    this.mAudioSrc = "data:audio/ogg;base64," + this.mAudioData;
  } else if (this.mExtension == '.mp3') {
    Debug("playing mp3");
    this.mAudioSrc = "data:audio/mp3;base64," + this.mAudioData;
  }

};

this.play = function() {

   if (this.mAudioPlayer.src != this.mAudioSrc) {
       this.mAudioPlayer.src = this.mAudioSrc;
   }
    this.mAudioPlayer.load();
    this.mAudioPlayer.play();
};

Had to do asynch:false, otherwise i would get a small chunk of the audio instead of all of it. Though removing the asynch made debugging easier in the end.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745604065a4635571.html

相关推荐

  • javascript - Setting html5 media source using ajax - Stack Overflow

    I needed to aid authentication headers for my audio files i was grabbing from a external server. So now

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信