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 到 5 秒的 6 种声音,我想textView1
在 for{} 的每个循环中进行更新,但textView1
只更新为“Sonido 5”。有什么建议么?