英文:
How to prevent MediaPlayer from getting started when setPlaybackParams() is called using Java in Android Studio?
问题
在我的项目中,在启动MediaPlayer(用户点击Button时会启动MediaPlayer)之前,我需要设置播放参数。但是,当我使用下面的代码设置播放参数时,MediaPlayer会自动启动。
mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(1.5f));
现在,我该如何防止它自动启动呢?
(你可以说,我可能会在按钮被点击时设置参数,但是这样做行不通,因为我有多个MediaPlayer需要设置参数。)
英文:
In my project I need to set the playback params before the MediaPlayer is started (MediaPlayer will start when user clicks a Button). But when I set playback params using the code given below, the MediaPlayer automatically gets started.
mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(1.5f));
Now, how would I prevent it from getting started?
(You can say, I might just set the params when button is clicked, but it won't work as I have several MediaPlayers to set params.)
答案1
得分: 2
根据MediaPlayer
的文档:
public void setPlaybackParams (PlaybackParams params)
使用PlaybackParams设置播放速率。该对象将其内部PlaybackParams设置为输入值,但当输入速度为零时,对象会记住先前的速度。这使得对象在调用start()时可以恢复到先前的速度。在对象准备好之前调用它不会改变对象的状态。在对象准备好之后,使用零速度调用它相当于调用pause()。在对象准备好之后,使用非零速度调用它相当于调用start()。
我怀疑你在设置参数之前调用了mediaPlayer.prepare()
,或者你使用了mediaPlayer.create()
(它相当于同时调用了mediaPlayer.setDataSource()
和mediaPlayer.prepare()
)。在这种情况下,当设置参数时播放器将开始播放,正如文档中所描述。
因此,请先使用mediaPlayer.setDataSource()
,然后设置播放参数(这样它就不会自动启动mediaPlayer
)。之后调用mediaPlayer.prepare()
,然后再调用mediaPlayer.start()
。
英文:
According to MediaPlayer
s doc:
> public void setPlaybackParams (PlaybackParams params)
>
> Sets playback rate using PlaybackParams. The object sets its internal PlaybackParams
> to the input, except that the object remembers previous speed when
> input speed is zero. This allows the object to resume at previous
> speed when start() is called. Calling it before the object is prepared
> does not change the object state. After the object is prepared,
> calling it with zero speed is equivalent to calling pause(). After the
> object is prepared, calling it with non-zero speed is equivalent to
> calling start().
I suspect you call mediaPlayer.prepare()
before you set the params or you have used mediaPlayer.create()
(which is equivalent to call mediaPlayer.setDataSource()
and mediaPlayer.prepare()
both at the same time. So, you indirectly called mediaPlayer.prepare()
), in which case the player will start playing when the params are set, exactly as described in the documentations.
So, use mediaPlayer.setDataSource()
and then set playback params (so that it doesn't automatically start the mediaPlayer
). After that call mediaPlayer.prepare()
before calling mediaPlayer.start()
.
答案2
得分: 1
如果您需要在不重新启动的情况下更改速度,您可以在更改速度之前检查播放状态,然后使用此信息在更改速度后立即停止播放:
mediaPlayer 是您的 Mediaplayer 实例,speed 是您的速度值
var isCurrentlyPlaying: Boolean = mediaPlayer!!.isPlaying
mediaPlayer!!.setPlaybackParams(mediaPlayer!!.getPlaybackParams().setSpeed(speed))
if (!isCurrentlyPlaying) {
mediaPlayer!!.pause()
}
英文:
If you need to change speed while not restarting, you could check playing status before then use this info to stop playing just after changing speed:
mediaPlayer is your Mediaplayer instance, speed is your value for speed
var isCurrentlyPlaying:Boolean=mediaPlayer!!.isPlaying
mediaPlayer!!.setPlaybackParams(mediaPlayer!!.getPlaybackParams().setSpeed(speed))
if (!isCurrentlyPlaying) {
mediaPlayer!!.pause()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论