2013-04-17 103 views
0

activity_main.xml中为什么此更新失败?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/hello_world" /> 

<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/textView1" 
android:layout_marginTop="153dp" 
android:layout_toRightOf="@+id/textView1" 
android:onClick="adelante" 
android:text="Button" /> 
</RelativeLayout> 

MainActivity.java

package com.example.pruebamusica; 

import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    MediaPlayer mp[]=new MediaPlayer[6]; 
    public TextView tv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tv = (TextView) findViewById(R.id.textView1); 
    for (int i=0; i<6; i++){ 
     String sonido = "raw/g" + (i+1); 
     int resID = getResources().getIdentifier(sonido, null, this.getPackageName()); 
     mp[i]= MediaPlayer.create(this, resID); 
    } 
    } 

    public void adelante (View v){ 
    for (int i=0; i<6; i++){ 
     tv.setText("Sonido " + i); 
     mp[i].start(); 
     do { // ESPERAMOS A QUE EL SONIDO ACABE. 
     } while (mp[i].isPlaying()); 
    } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
    } 
} 

我试图打的3 6个声音到5秒,我想在的每一个循环更新textView1 {} ,但textView1只更新为“Sonido 5”。 有什么建议吗?

+0

我认为当你播放非常短的声音,文本视图可能会更新很快,你不能查看文本视图的早期更新,从0到4.您确定播放所有声音吗? – Kameswari

回答

0

adelante将bock UI线程。直到所有任务完成后,用户界面才会更新。所以你只能看到最后的结果。 请使用处理程序和线程来更新UI。

你应该用的AsyncTask做到这一点:

MySyncTask extends AsyncTask<Void, Integer, Void> { 
     protected Long doInBackground(URL... urls) { 
      for (int i=0; i<6; i++){ 
      publishProgress(i); 
      mp[i].start(); 

      do { // ESPERAMOS A QUE EL SONIDO ACABE. 
      } while (mp[i].isPlaying()); 
     } 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     tv.setText("xxx play:" + i); 
    } 
} 
+0

解决了,谢谢Ger Soto。 – josemfr

0
for (int i=0; i<6; i++) 

你从0到5,如果你想要去的1-6,你需要做的tv.setText("Sonido " + (i+1));

+0

这不是问题的根源 –

0

不要在onCreate()做到这一点。您只应使用onCreate()来设置活动的最低限度。

在这种情况下,用户界面甚至还没有构建,所以当您最终允许onCreate()返回以及构建用户界面的活动时,任何更新都将只显示最后一个。

使用onResume()或更高版本。

0

首先,是非常危险的使用禁止{} while()循环等待音乐结束,因为它收获的资源做没有,所以,你必须以异步的方式思考它。

我建议你将播放器作为一个AsyncTask(Android Programming中的一个必须!)来管理,其中,在“onPreExecute()”中,你可以刷新电视TextView,并且当它结束时,退出asyncTask,一个新的戏剧。

另一种选择是使用“监听器”来等待播放的结束。

0

你可以使用任何循环,,,但如果它是关于时间。即你想播放声音几秒钟你的for循环会得到最后一个即最近更新的值我... 所以我想要获得更新值的每一秒你必须使用Handler()进行更新每一个值每一秒......这就是为什么你得到我最近的价值循环