问题描述
我目前正处于冒险之中,我的精神ity可危,我试图在Web应用程序中简单地播放mp3文件。此mp3会逐块加载,以使体验响应更快。
即使MediaSource API的文档非常明显,几乎是暴力的,有用的,并且有数百个经过充分解释的示例为救赎铺平了道路,但我发现自己无法使一切正常工作。
我面临的最后一个问题是寻求功能。我使用此功能的简单想法是简单地杀死所有活着的东西,将所有东西烧掉并重新恢复。更准确地说,如果用户愿意,我会停止机器并开始从头开始播放所有内容,但要有所需的偏移量(以秒为单位)。
到目前为止, 一切都在起作用,除了最后一件事:currentTime
,在将其设置为用户请求的时间后,将其设置为另一个值。
曲目继续按预期播放,但问题是currentTime
是用来为用户显示曲目中当前位置的内容。
这是事件在用户搜寻前后发生的方式
[play-click] <--- User clicks play button
:
[timeupdate] currentTime: 00.01
[timeupdate] currentTime: 00.02
[timeupdate] currentTime: 00.03
[seek-click] <--- User seeks,stop() is called and then playFrom(seconds)
[loadstart] currentTime: 33.33 # This is the (correct) time set in playFrom(seconds)
[loadmetadata] currentTime: 10.12 # Now the time gets set to this (wrong) value
[seek] currentTime: 10.12 # The triggered seek event comes in
[seeked] currentTime: 10.12
[canplay] currentTime: 10.12
[playing] currentTime: 10.12
[timeupdate] currentTime: 10.13 # Track continues to play correctly but this value is still wrong
:
这是寻找的方式:
public play(track: Track) {
this.track = track;
this.playFrom(0);
}
public seekTo(seconds: number) {
this.stop();
this.playFrom(seconds);
}
public stop() {
this.logger.debug('Stop ..');
this.sourceBuffer.abort();
this.audioObj.pause();
this.removeEvents(this.audioObj,this.audioEvents,this.audioEventsHandler);
this.audioObj = null;
this.sourceBuffer = null;
this.mediaSource = null;
}
现在,playFrom(seconds)
方法要比这复杂一些,但是它真正要做的只是重新创建我刚刚销毁的所有内容:
private playFrom(seconds: number) {
this.logger.debug(`Start playing from ${seconds.toFixed(2)} seconds`)
// [:] (collapsed some less important code here)
// Create the media source object
this.mediaSource = new MediaSource();
this.mediaSource.addEventListener('sourceopen',this.onSourceOpen);
// Create audio element
this.audioObj = document.createElement('audio');
this.audioObj.src = URL.createObjectURL(this.mediaSource);
this.audioObj.currentTime = seconds;
this.addEvents(this.audioObj,this.audioEventsHandler);
this.logger.debug(`Object-url: ${this.audioObj.src}`);
this.state = {currentTime: seconds,playing: true};
}
如何在此处获取正确的当前时间?
我尝试了另一个尝试,尝试在SourceBuffer
上创建新的MediaSource
并将其添加到from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
classifier = Sequential()
classifier.add(Convolution2D(64,(9,9),input_shape=(128,128,3),activation='relu'))
classifier.add(MaxPooling2D(pool_size=(4,4)))
#2nd Conv layer
classifier.add(Convolution2D(32,(3,activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
##
classifier.add(Dense(units = 128,activation = 'relu'))
classifier.add(Dense(units = 128,activation = 'relu'))
classifier.add(Dense(units = 1,activation = 'sigmoid'))
classifier.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,shear_range = 0.2,zoom_range = 0.2,horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('dataset/training_set',target_size = (128,128),batch_size = 32,class_mode = 'binary')
test_set = test_datagen.flow_from_directory('dataset/test_set',class_mode = 'binary')
classifier.fit_generator(
training_set,steps_per_epoch=(1341+3875)/32,epochs=15,validation_data=test_set,validation_steps=(234+390)/32)
并从那里播放音频,但我也做不到。
解决方法
在向媒体元素发出搜索时,媒体来源(或媒体元素)报告的持续时间是多少?如果它是无限的,则可能需要使用MSE中的{set,clear} LiveSeekableRange API来告知实现,是否希望让播放器超出当前缓冲的媒体的最高呈现结束时间。或者,您可以设置 MediaSource持续时间,然后再尝试成为有限的东西,但至少与当前缓冲的最高媒体演示结束时间一样长。