1

我是android开发的新手。我正在制作一个基于听写​​的应用程序。
在下面的活动中,我有 15 个小音频片段;文件一个接一个地播放,延迟 5 秒。
我的问题是我如何才能只播放 15 个片段中的 10 个片段?我还希望随机播放 10 个剪辑。
任何帮助将不胜感激。

package com.example.dictationary;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Level1 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level1);
        MediaPlayer n1 = MediaPlayer.create(this,R.raw.assassin); 
        final MediaPlayer n2 = MediaPlayer.create(this,R.raw.accessible);
        final MediaPlayer n3 = MediaPlayer.create(this,R.raw.bashfully);
        final MediaPlayer n4 = MediaPlayer.create(this,R.raw.blistering);
        final MediaPlayer n5 = MediaPlayer.create(this,R.raw.butter);
        final MediaPlayer n6 = MediaPlayer.create(this,R.raw.campaign);
        final MediaPlayer n7 = MediaPlayer.create(this,R.raw.circumstances);
        final MediaPlayer n8 = MediaPlayer.create(this,R.raw.clinching);
        final MediaPlayer n9 = MediaPlayer.create(this,R.raw.deferential);
        final MediaPlayer n10 = MediaPlayer.create(this,R.raw.distinguished);
        final MediaPlayer n11 = MediaPlayer.create(this,R.raw.embarrass);
        final MediaPlayer n12 = MediaPlayer.create(this,R.raw.etiquette);
        final MediaPlayer n13 = MediaPlayer.create(this,R.raw.fatigue);
        final MediaPlayer n14 = MediaPlayer.create(this,R.raw.feasible);
        final MediaPlayer n15 = MediaPlayer.create(this,R.raw.glitch);

        n1.start();





    Timer buttonTimer = new Timer();
    buttonTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n2.start();

                }

            });
        }
    }, 5000);

    Timer buttonTimer1 = new Timer();
    buttonTimer1.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n3.start();

                }
            });
        }
    }, 10000);

    Timer buttonTimer2 = new Timer();
    buttonTimer2.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n4.start();

                }
            });
        }
    }, 15000);


    Timer buttonTimer3 = new Timer();
    buttonTimer3.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n5.start();

                }
            });
        }
    }, 20000);

    Timer buttonTimer4 = new Timer();
    buttonTimer4.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n6.start();

                }
            });
        }
    }, 25000);

    Timer buttonTimer5 = new Timer();
    buttonTimer5.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n7.start();

                }
            });
        }
    }, 30000);

    Timer buttonTimer6 = new Timer();
    buttonTimer6.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n8.start();

                }
            });
        }
    }, 35000);

    Timer buttonTimer7 = new Timer();
    buttonTimer7.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n9.start();

                }
            });
        }
    }, 40000);

    Timer buttonTimer8 = new Timer();
    buttonTimer8.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n10.start();

                }
            });
        }
    }, 45000);

    Timer buttonTimer9 = new Timer();
    buttonTimer9.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n11.start();

                }
            });
        }
    }, 50000);

    Timer buttonTimer10 = new Timer();
    buttonTimer10.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n12.start();

                }
            });
        }
    }, 55000);

    Timer buttonTimer11 = new Timer();
    buttonTimer11.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n13.start();

                }
            });
        }
    }, 60000);


    Timer buttonTimer12 = new Timer();
    buttonTimer12.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n14.start();

                }
            });
        }
    }, 65000);


    Timer buttonTimer13 = new Timer();
    buttonTimer13.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     n15.start();

                }
            });
        }
    }, 70000);
}
    }
4

2 回答 2

2

您可以将它们放在数组或列表中,并使用随机访问它们。示例:创建一个 ArrayList:

//Here we create the ArrayList
LinkedList<MediaPlayer> list = new LinkedList<MediaPlayer>(15);
list.add(new MediaPlayer .... // for each element.

访问此操作

// now we create the random int
Random r = new Random();
int pointer = r.nextInt(list.size());

// and here we access the "random" MediaPlayer
list.remove(pointer).start(); // gets a random media

其中 nextInt 返回一个介于 0 和 <=list.size 之间的值

// 编辑更改的代码

于 2013-05-22T09:50:46.583 回答
0

您无法使用现在组织代码的方式...

你应该重构为

`MediaPlayer n = new MediaPlayer[];
n[1] = MediaPlayer.create(this,R.raw.assassin);
//...
n[15] = MediaPlayer.create(this,R.raw.assassin);

然后只需在间隔中选择一个随机数[0-15)(不包括 15 个)并播放那个。

此外,您的代码将大大简化,因为只需要一个重复的 TimerTask,并且每次它都会调用一个随机数(单词)。然而,这会引入重复的单词——为确保没有单词重复,将随机选择的数字添加到alreadyHeard列表中,而不是播放该索引,只需选择一个新的并再次测试它(事实上,while()选择它们直到你想出一个不在alreadyHeard列表中的)

那应该可以得到你想要的:)

于 2013-05-22T09:49:37.873 回答