简体   繁体   English

绑定要在特定时间播放的MediaPlayer

[英]Binding MediaPlayer to be played at a specific time

When using MediaPlayer, I noticed that whenever my phone stucks, the MediaPlayer glitches and then continues playing from the position in the audio it glitched. 使用MediaPlayer时,我注意到只要手机卡住,MediaPlayer都会出现故障,然后继续从音频出现故障的位置继续播放。
This is bad for my implementation since I want the audio to be played at a specific time. 这对我的实现不利,因为我希望在特定时间播放音频。

If I have a song of 1000 millisecond length, I want is the ability to set MediaPlayer to start playing at some specific time t , and then exactly stop at at time t+1000 . 如果我有一首长度为1000毫秒的歌曲,我希望能够将MediaPlayer设置为在某个特定时间t开始播放,然后在时间t + 1000恰好停止播放。

This means that I actually need two things: 这意味着我实际上需要两件事:
1) Start MediaPlayer at a specific time with a very small delay. 1)在特定时间以很小的延迟启动MediaPlayer。
2) Making MediaPlayer glitches ignore the audio they glitched on and continue playing in order to finish the song on time. 2)使MediaPlayer小故障忽略其小故障的音频,并继续播放以按时完成歌曲。

The delay of the functions is very important to me and I need the audio to be played exactly(~) at the time it was supposed to be played. 功能的延迟对我来说非常重要,我需要在播放音频时准确播放音频(〜)。

Thanks! 谢谢!

You will need to use possibly mp.getDuration(); 您可能需要使用mp.getDuration(); and/or mp.getCurrentPosition(); 和/或mp.getCurrentPosition(); although it's impossible to know exactly what you mean by "I need the audio to be played exactly(~) at the time it was supposed to be played." 尽管无法确切地知道您的意思是“我需要在应该播放的时候准确播放音频(〜)”。

Something like this should get you started: 这样的事情应该让您入门:

  int a = (mp.getCurrentPosition() + b); 

Thanks for the answer Mike. 谢谢你的回答迈克。 but unfortunately this won't help me. 不幸的是,这对我没有帮助。

Let's say that I asked MediaPlayer to start playing a song of length 3:45 at 00:00. 假设我让MediaPlayer在00:00开始播放长度为3:45的歌曲。
At 01:00 I started using the phone's resources, due to the heavy usage my phone glitched making MediaPlayer pause for 2 seconds. 在01:00,由于使用率过高,我开始使用手机的资源,我的手机出现故障,使MediaPlayer暂停了2秒钟。

Time: 时间:

00:00-01:00 - I heard the audio at 00:00-01:00 00:00-01:00 -我听到了声音,在00:00-01:00
01:00-01:02 - I heard silence because the phone glitched 01:00-01:02 -听说沉默,因为手机glitched
01:02-03:47 - I heard the audio at 01:00-03:45 with 2 second time skew 01:02-03:47 -我听到01的声音:00-03:45和2秒的时间偏移

Now from what I understood MediaPlayer is a bad choice of usage on this problem domain, since MediaPlayer provides a high level API. 现在,据我了解,MediaPlayer是在此问题域上的错误选择,因为MediaPlayer提供了高级API。
I am currently experimenting with the AudioTrack class which should provide me with what I need: 我目前正在试验AudioTrack类,该类应该为我提供所需的东西:

//Creating a new audio track
AudioTrack audioTrack = new AudioTrack(...)

//Get start time
long start = System.currentTimeMillis();

// loop until finished
for (...) {
    // Get time in song
    long now = System.currentTimeMillis();
    long nowInSong = now - start;
    // get a buffer from the song at time nowInSong with a length of 1 second
    byte[] b = getAudioBuffer(nowInSong);
    // play 1 second of music
    audioTrack.write(b, 0, b.length);
    // remove any unplayed data
    audioTrack.flush();
}

Now if I glitch I only glitch for 1 second and then I correct myself by playing the right audio at the right time! 现在,如果我出现毛刺,我只会毛刺1秒钟,然后在正确的时间播放正确的音频来纠正自己!

NOTE 注意

I haven't tested this code but it seems like the right way to do it. 我没有测试此代码,但似乎是正确的方法。 If it will actually work I will update this post again. 如果它确实可行,我将再次更新此帖子。

PS seeking in MediaPlayer is: 1. A heavy operation that will surely delay my music (every millisecond counts here) 2. Is not thread safe and cannot be used from multiple threads (seeks, starts etc...) MediaPlayer寻求的PS是:1.繁重的操作肯定会延迟我的音乐(在这里要数毫秒)2.线程不安全,不能在多个线程中使用(搜索,启动等...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM