gpt4 book ai didi

java - Android MediaPlayer-如何从原始文件夹加载加载多个文件

转载 作者:行者123 更新时间:2023-12-03 02:08:26 24 4
gpt4 key购买 nike

我正在查看MediaPlayer的example,我想找出如何从原始文件夹中加载多个文件mp3并使它们全部循环播放。

我真的很想从原始文件夹中加载4个简短的mp3文件,所以当播放bt激活时。所有音频将循环播放。谢谢

package com.example.mediaplayer;

import java.util.concurrent.TimeUnit;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

public TextView songName,startTimeField,endTimeField;
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();;
private int forwardTime = 5000;
private int backwardTime = 5000;
private SeekBar seekbar;
private ImageButton playButton,pauseButton;
public static int oneTimeOnly = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songName = (TextView)findViewById(R.id.textView4);
startTimeField =(TextView)findViewById(R.id.textView1);
endTimeField =(TextView)findViewById(R.id.textView2);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
playButton = (ImageButton)findViewById(R.id.imageButton1);
pauseButton = (ImageButton)findViewById(R.id.imageButton2);
songName.setText("song.mp3");
mediaPlayer = MediaPlayer.create(this, R.raw.song);
seekbar.setClickable(false);
pauseButton.setEnabled(false);

}

public void play(View view){
Toast.makeText(getApplicationContext(), "Playing sound",
Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if(oneTimeOnly == 0){
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}

endTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) finalTime)))
);
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}

private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
public void pause(View view){
Toast.makeText(getApplicationContext(), "Pausing sound",
Toast.LENGTH_SHORT).show();

mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
public void forward(View view){
int temp = (int)startTime;
if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
}
else{
Toast.makeText(getApplicationContext(),
"Cannot jump forward 5 seconds",
Toast.LENGTH_SHORT).show();
}

}
public void rewind(View view){
int temp = (int)startTime;
if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
}
else{
Toast.makeText(getApplicationContext(),
"Cannot jump backward 5 seconds",
Toast.LENGTH_SHORT).show();
}

}

@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;
}

}

最佳答案

您可以使用以下代码获取原始文件夹中所有音频文件的资源ID。

ArrayList<Integer> audioResourceId=new ArrayList<Integer>();
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
int resourceID=fields[count].getInt(fields[count]);//resource id
audioResourceId.add(resourceID);
}
}

比实现MediaPlayer.onCompletionListener
    @Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
if(index<audioResourceId.size())
index++;
else
index=0;
mediaPlayer = MediaPlayer.create(this,audioResourceId(index));
mediaPlayer.start();

}

关于java - Android MediaPlayer-如何从原始文件夹加载加载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com