Android的卡拉OK一样:从MediaPlayer.currentPosition更新GUI() [英] Android Karaoke-like : Updating GUI from MediaPlayer.currentPosition()

查看:212
本文介绍了Android的卡拉OK一样:从MediaPlayer.currentPosition更新GUI()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做某种卡拉OK的Andr​​oid应用
所以,基本上,我有一些TextViews,我想以编程方式更改文本颜色,而播放音频。

TextViews具有其涉及在音频文件

的位置处的BEGIN_TIME和end_time时间属性

播放音频,并收集有关信息是容易的MediaController

所以,现在我想要动态地改变颜色......我试着用一个单独的线程等多种途径,但我一直坚持了同样的问题。


  

android.view.ViewRoot $ CalledFromWrongThreadException:只有
  创建视图层次可以触摸其观点原来的线程。


我想了解更多关于这一点,我所学到的是,与UI相关的一切都在UI线程工作要做。
大多数人使用的处理程序在后台线程中执行GUI更新。但我没有做这个该死的东西制定了......所以,关于处理任何建议以及它们如何工作是值得欢迎的。

不管怎么说,为什么不使用AsyncTask的?
从进程和线程开发指南

报价

  

也许是最好的解决办法,虽然是延长的AsyncTask类,
  这简化的工作线程任务需要执行
  与UI交互。


其实,我用这AsyncTask的,但它并不简化线程

也许他们是不时事件GUI更新的其他解决方案?


好吧,其实我不想做只是一个卡拉OK的TextView必须点击,所以当就一个字用户点击,音频转到该位置在音频和TextView的颜色有改变动态

所以,我想过用动画流畅变色。或者,也许TransitionDrawable,但它使用层和画布。

我也觉得出这个与ForegroundColorSpan:

  Spannable WordtoSpan =新SpannableString(我知道如何耳语);
WordtoSpan.setSpan(新ForegroundColorSpan(Color.BLUE),1,13,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView的电视=新的TextView(上下文).setText(WordtoSpan);

但不知道这将是足够的动态​​,由于ForegroundColorSpan结束计算。

因此​​,使用仅TextView的考虑,什么是实现动态的TextView颜色改变的最佳方式(有这些解决方案,或任何其他)


二修改

好吧,我几乎完成这个项目...
我现在能颜色词动态随着时间的推移,但问题是,它不是与音频同步。

即:
让我们来一句话你好,我的名字是约翰和我要迟到了我的工作
如果音频播放[我是]彩色的文字[就业]

所以,它只是一个同步的问题,但我面对这几个星期。

的文本被组织成一个链表
链表的元素是对象(因为我有Word和标点符号类),但都有一个开始和结束位置,对应的相对位置在音频以毫秒为单位。

所以...我有类似的东西:
您好:首先= 0,结束= 1000
我的:开始= 1200,最终= 1400
等等...

在现在的主要活动中,我有一个Runnable是处理文本着色
而且我也有3个对象,previous_element,current_element和next_element

目前可运行的开头,在previous_element的颜色重新设置为白色,所以只有当前词是有颜色的。
然后,current_element的颜色设置为黄色,或任何...
然后,我检查next_element.getBegin()> =音频当前位置
 ...如果是,意味着当前的字已经结束了,所以我改变current_element到next_element,并使用Handler.post重新开始的过程(MyRunnableThatDoesColoration)

所以我不理解它是怎么回事错
如果有人人不得不线索?


解决方案

  

android.view.ViewRoot $ CalledFromWrongThreadException:只有
  创建视图层次可以触摸其观点原来的线程。


这可以通过使用runOnUIThread来实现:

  runOnUiThread(新的Runnable(){
    公共无效的run(){
        //你的UI更改code HERE!
    }
});

如果你看到卡拉OK基本GUI(textViews),我认为这将是不够的动态。您应该使用 Android的画布 gameEngine (Andengine ..)。

I'm making some kind of "karaoke" android apps So, basically, I have some TextViews and I want to change the text color programmatically while playing audio.

TextViews have the "begin_time" and "end_time" attributes which relates to the position in the audio file

Playing audio, and gathering informations about that is easy with MediaController

So, now I'm trying to change the color dynamically ... I tried with a separate thread and many other ways, but I always stuck with the same issue

" android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. "

I tried to learn more about that, and what I learned is that everything related with the UI has to be done in the UI Thread. Most of people use handlers to perform GUI update in background thread. But I didn't made this damn thing worked out ... So any suggestion about handlers and how they work is welcome.

Anyways, why not using AsyncTask ? Quote from the "Processes and Threads dev' guide"

" Perhaps the best solution, though, is to extend the AsyncTask class, which simplifies the execution of worker thread tasks that need to interact with the UI. "

Actually, I'm using this AsyncTask, but it doesn't "simplifies" threading

Or Maybe they're is an other solution for GUI update from time event ?


Well, in fact, i don't want to make "just a karaoke" Textview have to be clickable, so when the user click on a word, the audio goes to that position in audio, and textview color have to change dynamically

So, I thought about using Animation for smooth color changing. Or maybe TransitionDrawable, but it use layers and canvas.

I also find out this, with ForegroundColorSpan:

Spannable WordtoSpan = new SpannableString("I know just how to whisper");            
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 1, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
TextView tv = new TextView(context).setText(WordtoSpan); 

But not sure it will be dynamic enough due to calculation of the end of the ForegroundColorSpan.

So considering using only TextView, what is the best way to achieve dynamic textview color changing (with those solutions, or any other)?


Second edit

Well, i'm almost done with this project ... I am now able to color word dynamically over time, but the problem is that it's not synchronized with audio.

i.e : let's take a sentence like "hello, my name is john and i'm late for my job" If audio is playing [i'm] the colored text is [job]

So, it's just a synchronization problem, but i'm facing it for several weeks.

The text is organized into a linked list elements of the linked list are Object (because i'm having both Word and Ponctuation class) but both have a begin and end position, corresponding to their relative position in audio in milliseconds.

so ... I have something like that : Hello : begin = 0, end = 1000 my : begin = 1200, end = 1400 and so on ...

In the main activity now, I have a Runnable that deal with text coloration And I also have 3 Object, previous_element, current_element, and next_element

At the begining of the runnable, the color of the previous_element is set back to White, so only the current word is colored. Then, the current_element's color is set to Yellow, or whatever ... Then, I check if next_element.getBegin() >= current position in audio ... If it is, that mean the current word is now over, so I change the current_element to next_element, and start over the process using Handler.post(MyRunnableThatDoesColoration)

So I'm not understanding where it's going wrong If anyone one have a clue ?

解决方案

" android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. "

This can be achieved by using runOnUIThread:

runOnUiThread(new Runnable() {
    public void run() {
        //Your UI Changing code HERE!
    }
});

If you see the karaoke as basic gui (textViews), I think it won't be dynamic enough. You should use Android Canvas or a gameEngine (Andengine..).

这篇关于Android的卡拉OK一样:从MediaPlayer.currentPosition更新GUI()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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