在布局活动中如何显示随机声音结果的数值,以便能够播放。

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

how to display the value of random sound results in the layout activity so that it can be played

问题

I have translated the provided text as requested. Here are the translated parts:

我有3个音频文件在原始的Android Studio文件夹中

Sound1.mp3
Sound2.mp3
Sound3.mp3

然后我想使用Fisher Yates Shuffle算法对音频进行随机排序
下面的代码是否正确还是我需要先初始化它

/package com.example.once;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
import java.util.Arrays;



public class Main4Activity extends AppCompatActivity {
// 用于随机排列给定数组的Java程序

    public static class ShuffleRand {
        // 生成arr []的随机排列的函数
        static void randomize (String arr [], int n) {
            // 创建Random类的对象
            Random r = new Random ();

            // 从最后一个元素开始,逐个交换。我们不需要运行第一个元素,所以i>0
            for (int i = n - 1; i > 0; i--) {

                // 从0到i选择一个随机索引
                int j = r.nextInt (i + 1);

                // 用随机索引处的元素交换arr [i]
                String temp = arr [i];
                arr [i] = arr [j];
                arr [j] = temp;
            }
            // 打印随机数组
            System.out.println (arr [0]);
            System.out.println (Arrays.toString (arr));
        }

        // 主程序以测试上述函数
        public static void main (String [] args) {

            String [] arr = {"R.raw.sound1, R.raw.sound2, R.raw.sound3"};
            int n = arr.length;
            randomize (arr, n);
        }

    }
}

在获取随机值(arr [0])后,我想在点击PlaySong按钮时将值(arr [0])输入到.xml活动布局中。如何操作?

<ImageView
        android:id="@+id/imageView5"
        android:layout_width="75dp"
        android:layout_height="80dp"
        android:onClick="playSong"
        android:layout_marginTop="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView8"
        app:srcCompat="@drawable/play"/>

请注意,上述代码需要根据您的具体需求进行进一步定制和调整。如果您需要更多帮助或有其他问题,请随时提出。

英文:

I have 3 audio in the raw android studio folder:

Sound1.mp3
Sound2.mp3
Sound3.mp3

Then, I want to do audio shuffle using the fisher yates shuffle algorithm.
Is the code below correct? Or do I need to initialize it first?

/ package com.example.once;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
import java.util.Arrays;
public class Main4Activity extends AppCompatActivity {
// Java Program to shuffle a given array
public static class ShuffleRand {
// A Function to generate a random permutation of arr []
static void randomize (String arr [], int n) {
// Creating a object for Random class
Random r = new Random ();
// Start from the last element and swap one by one. We don&#39;t
// need to run for the first element that&#39;s why i&gt; 0
for (int i = n - 1; i&gt; 0; i--) {
// Pick a random index from 0 to i
int j = r.nextInt (i + 1);
// Swap arr [i] with the element at random index
String temp = arr [i];
arr [i] = arr [j];
arr [j] = temp;
}
// Prints the random array
System.out.println (arr [0]);
System.out.println (Arrays.toString (arr));
}
// Driver Program to test above function
public static void main (String [] args) {
String [] arr = {&quot;R.raw.sound1, R.raw.sound2, R.raw.sound3&quot;};
int n = arr.length;
randomize (arr, n);
}
}
}

After getting the random value (arr [0]), I want to enter the value (arr [0]) on the .xml activity layout in the play button with the onClick PlaySong. How to?

&lt;ImageView
android: id = &quot;@ + id / imageView5&quot;
android: layout_width = &quot;75dp&quot;
android: layout_height = &quot;80dp&quot;
android: onClick = &quot;playSong&quot;
android: layout_marginTop = &quot;50dp&quot;
app: layout_constraintEnd_toEndOf = &quot;parent&quot;
app: layout_constraintStart_toStartOf = &quot;parent&quot;
app: layout_constraintTop_toBottomOf = &quot;@ + id / imageView8&quot;
app: srcCompat = &quot;@ drawable / play&quot; /&gt;

答案1

得分: 0

以下是翻译好的代码部分:

private MediaPlayer mMediaPlayer;

public void stopSong() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

public void playSong(Context c, int rid) {
    stopSong();

    mMediaPlayer = MediaPlayer.create(c, rid);
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            stopSong();
        }
    });

    mMediaPlayer.start();
}

// 调用这个函数的方式如下:
playSong(this, getResources().getIdentifier(arr[0], "raw", getPackageName()));

请注意,我只翻译了代码部分,没有包括其他内容。

英文:

You can do like this :

private MediaPlayer mMediaPlayer;
public void stopSong() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public void playSong(Context c, int rid) {
stop();
mMediaPlayer = MediaPlayer.create(c, rid);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stop();
}
});
mMediaPlayer.start();
}

so you can call this function :

 playSong(this, getResources().getIdentifier(arr[0], &quot;raw&quot;, getPackageName());

huangapple
  • 本文由 发表于 2020年8月1日 11:19:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201421.html
匿名

发表评论

匿名网友

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

确定