结合的MediaPlayer到在特定的时间播放 [英] Binding MediaPlayer to be played at a specific time

查看:231
本文介绍了结合的MediaPlayer到在特定的时间播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的MediaPlayer,我注意到,每当我的电话stucks的MediaPlayer的毛刺,然后继续从它glitched音频的位置打。结果
因为我想要音频在特定时间播放这是不好的我的执行。结果
结果
如果我有1000毫秒长的一首歌,我想是设置的MediaPlayer,开始在一些特定的时间播放能力的 T ,然后在在时间的 T + 1000 。
搜索结果
这意味着,我确实需要两件事:结果
1)启动的MediaPlayer在特定时间具有非常小的延迟。结果
2)制作的MediaPlayer毛刺忽略他们glitched上的音频,并继续以按时完成的歌曲播放。

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.
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.

This means that I actually need two things:
1) Start MediaPlayer at a specific time with a very small delay.
2) Making MediaPlayer glitches ignore the audio they glitched on and continue playing in order to finish the song on time.

的功能的延迟是对我很重要,我需要的声音是在它应该播放的时间正好扮演(〜)。

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.

谢谢!

推荐答案

感谢您的回答麦克。但遗憾的是这不会帮助我。搜索结果比方说,我问的MediaPlayer开始于00:00播放长度3:45的一首歌。结果在01:00我开始使用手机的资源,由于大量使用我的手机glitched制作媒体播放停顿2秒。搜索结果
时间:

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.
At 01:00 I started using the phone's resources, due to the heavy usage my phone glitched making MediaPlayer pause for 2 seconds.

Time:

00:00-01:00 - 我听到了声音在00:00-01:00结果
01:00-01:02 - 听说沉默,因为手机glitched结果
01:02-03:47 - 我听到了声音在01:00-03:45和2秒的时间偏移搜索结果
现在,从我的理解MediaPlayer正在使用的对这个问题领域一个不错的选择,因为的MediaPlayer 提供了一个高层次的API。结果我目前的试验
AudioTrack 类,它应该提供给我什么,我需要:

00:00-01:00 - I heard the audio at 00:00-01:00
01:00-01:02 - I heard silence because the phone glitched
01:02-03:47 - I heard the audio at 01:00-03:45 with 2 second time skew

Now from what I understood MediaPlayer is a bad choice of usage on this problem domain, since MediaPlayer provides a high level API.
I am currently experimenting with the AudioTrack class which should provide me with what I need:

//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();
}

现在,如果我毛刺我唯一问题1秒,然后我通过打右音频在正确的时间正确的喽!

Now if I glitch I only glitch for 1 second and then I correct myself by playing the right audio at the right time!

注意

我没有测试此code,但它似乎是做了正确的方式。如果将实际工作,我会再次更新这个帖子。

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.

P.S。寻求在的MediaPlayer 是:
1.重的操作,肯定会耽误我的音乐(每毫秒这里计数)
2.不是线程安全的,不能从多个线程(寻求,启动等)

P.S. 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到在特定的时间播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆