我有5个按钮,我想在按钮的点击事件中更改mediaPlayer的来源,怎么做呢?

huangapple go评论70阅读模式
英文:

I have 5 buttons I want to change mediaPlayer source fonClick of the buttons how to do that?

问题

我遇到了关于mediaPlayer的问题,我想通过按钮的点击来更改(文件)mediaplayer=mediaplayer.create(r.folder."file"),并且在按下另一个按钮时再次更改它。
我有几个按钮,我想为每个事件分配不同的声音进行播放。

英文:

I am having an issue with mediaPlayer I want to change(file) mediaplayer=mediaplayer.create(r.folder."file") with onClick of botton and change it again if another button is pressed.
I am having buttons and i want to assign different sounds to be played on every event

答案1

得分: 0

使用switch语句,根据按钮的ID创建具有不同源的媒体文件。

英文:

use switch statement and based on the id of the button create mediafile with different source

答案2

得分: 0

首先,在活动中将MediaPlayer变量设置为全局,并在onCreate方法触发时进行赋值,如下所示:

private MediaPlayer mediaPlayer;

@Override
public void onCreate() {
    super.onCreate();

    // 分配媒体播放器
    mediaPlayer = new MediaPlayer();
}

假设您已经将5个按钮分配为{ button1 button2 button3 button4 和 button5 },请为它们添加点击监听器,如下所示:

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // 更改音乐
    }
});

现在,我们要做的是,每次单击按钮时,都要重置现有的MediaPlayer,然后将新文件分配给它,然后播放该文件。

因此,在每个监听器的onClick事件中,您需要运行以下代码:

mediaPlayer.reset();

try {
      mediaPlayer.setDataSource("/storage/emulated/0....路径到您的文件");
      mediaPlayer.prepare();
      mediaPlayer.start();
}
catch (Exception ex){
    // 发生异常
};

每个按钮的onClick监听器现在应该如下所示:

// 按钮1
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // 更改音乐
        mediaPlayer.reset();

        try {
              mediaPlayer.setDataSource("/storage/emulated/0....路径到您的文件");
              mediaPlayer.prepare();
              mediaPlayer.start();
        }
        catch (Exception ex){
            // 发生异常
        };
    }
});

// 按钮2
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // 更改音乐
        mediaPlayer.reset();

        try {
              mediaPlayer.setDataSource("/storage/emulated/0....路径到您的文件");
              mediaPlayer.prepare();
              mediaPlayer.start();
        }
        catch (Exception ex){
            // 发生异常
        };
    }
});

Button 3、4和5的处理方式类似。

英文:

Firstly make the MediaPlayer variable global in the activity, and assign it when the onCreate methord fires in your Activity like this

Private MediaPlayer mediaPlayer ;

@Override
public void onCreate() {
    super.onCreate();
    
    //assign the media player
    mediaPlayer = new MediaPlayer();
}

Assuming you have your 5 buttons assigned as { button1 button2 button3 button4 and button5 } , add onclick listeners to all of them like this

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //change the music
        }
});

Now what we want to do is that, everytime you click a button we want to reset the existing MediaPlayer and then assign a new file to it, and then play that file.

So in the onClick event of each listener you need to run the code

mediaPlayer.reset();

try {
      mediaPlayer.setDataSource("/storage/emulated/0....path to yor file");
      mediaPlayer.prepare();
      mediaPlayer.start();
    }
catch (Exception ex){
    //we have an exception
};

The onClick listeners for each button should now look like this

//button1
button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //change the music
            mediaPlayer.reset();

            try {
                  mediaPlayer.setDataSource("/storage/emulated/0....path to yor file");
                  mediaPlayer.prepare();
                  mediaPlayer.start();
                }
                catch (Exception ex){
                //we have an exception
                };



//button 2
button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //change the music
            mediaPlayer.reset();

            try {
                  mediaPlayer.setDataSource("/storage/emulated/0....path to yor file");
                  mediaPlayer.prepare();
                  mediaPlayer.start();
                }
                catch (Exception ex){
                //we have an exception
                };

        }
});


        }
});


//Button 3 , 4 and 5

huangapple
  • 本文由 发表于 2020年9月27日 23:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64090195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定