android:播放音频文件并动态显示图像

时间:2012-01-31 11:49:08

标签: android media-player

大家好我有三个音频文件和三个图像,每个图像显示三个文字。 现在,一旦应用程序启动第一个图像,文本和音频文件开始播放,我完成的工作,第二个完成后,第三个开始播放。

基本上我需要这些按顺序依次播放,但动态,因为我有20个类似于此的文件

下面是我的代码

public class S001Activity extends Activity {
    /** Called when the activity is first created. */

    ImageView img;
    TextView display;
    MediaPlayer apple = new MediaPlayer();
    MediaPlayer boy = new MediaPlayer();
    MediaPlayer cat = new MediaPlayer();
    //Runnable rb,rc;

    Document doc;
    DocumentBuilder builder;
    Handler handler = new Handler();

    int i=0;
    int noOfQues = 0;
    //int media[] = {R.raw.apple_v,R.raw.boy_v,R.raw.cat_v};
    //int image[] = {R.drawable.apple,R.drawable.boy,R.drawable.cat};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        img = (ImageView)findViewById(R.id.img);
        display = (TextView)findViewById(R.id.display);

        img.setBackgroundResource(R.drawable.apple);
        apple = MediaPlayer.create(this, R.raw.apple_v);
        apple.start();
        xml();


        boy = MediaPlayer.create(this, R.raw.boy_v);

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                i++;
                img.setBackgroundResource(R.drawable.boy);

                boy.start();
                xml();
            }
        },apple.getDuration()+ 2000 );

        cat = MediaPlayer.create(this, R.raw.cat_v);
            handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                i++;
                img.setBackgroundResource(R.drawable.cat);
                cat.start();
                xml();
            }
        },apple.getDuration()+ boy.getDuration()+ 2000 );
}


    public void xml(){
         try {
            InputStream in = getResources().openRawResource(R.raw.topic1761);
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            doc = builder.parse(in, null);

            NodeList listOfPersons = doc.getElementsByTagName("Point");
            noOfQues = listOfPersons.getLength();
            System.out.println("items======= " + noOfQues);

            Node firstPersonNode = listOfPersons.item(i);

            Element firstPersonElement = (Element) firstPersonNode;

            NodeList firstNameList = firstPersonElement.getElementsByTagName("PracticeText");
            Element firstNameElement = (Element) firstNameList.item(0);

            NodeList textFNList = firstNameElement.getChildNodes();
            display.setText(((Node) textFNList.item(0)).getNodeValue().trim());

        } catch (Throwable t) {
            Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
        }
    }

已编辑的代码

@Override
    public void onCompletion(MediaPlayer mps) {
        // TODO Auto-generated method stub
        //MediaPlayer mps = new MediaPlayer();

        img.setBackgroundResource(image[i]);
        handler.post(new Runnable() {

              @Override
              public void run() {

                  xml();
                  AssetFileDescriptor afd = getResources().openRawResourceFd(media[i]);
                  mp.reset();
                  try {
                    mp.setDataSource(afd.getFileDescriptor());
                    afd.close();
                    mp.prepare();
                    mp.start();

                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

              }
             });  
    }

1 个答案:

答案 0 :(得分:1)

您可以使用数组或列表来存储音乐文件,图像文件和文本列表 然后在MediaPlayer.OnCompletionListener中播放下一个。

编辑:

int media[] = {R.raw.apple_v,R.raw.boy_v,R.raw.cat_v};
MediaPlayer mp = new MediaPlayer();

// in OnCompletionListener,
index = index + 1; //add proper checks
handler.post(new Runnable() {

  @Override
  public void run() {
      AssetFileDescriptor afd = getResources().openRawResourceFd(media[index]);
      mp.reset();
      mp.setDataSource(afd.getFileDescriptor());
      afd.close();
      mp.prepare();
      mp.start();
  }
 });  

我认为您可以通过上面的示例代码获得想法。