我正在尝试从音频文件中填充listview,并希望播放点击的项目。它不起作用。任何帮助。
MainActivity.java
public class MainActivity extends ListActivity {
public static File file;
private List<String> filelist;
ArrayList<String> MyFiles = new ArrayList<String>();
MediaPlayer mp=new MediaPlayer();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
final ListView musiclist = (ListView) findViewById(R.id.PhoneMusicList);
MyFiles = new ArrayList<String>();
final String targetPath = Environment.getExternalStorageDirectory().getAbsolutePath();
file = new File( targetPath + "/AudioRecorder" ) ;
File list[] = file.listFiles();
// Binding resources Array to ListAdapter
for( int i=0; i< list.length; i++)
{
MyFiles.add( list[i].getName() );
}
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, MyFiles);
musiclist.setAdapter(adapter);
musiclist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//checking the position of item in list
Object listItem = musiclist.getItemAtPosition(position);
Toast.makeText(MainActivity.this, ""+ musiclist, Toast.LENGTH_SHORT).show();
playSong(targetPath + MyFiles.get(position));
}
});
}
private void playSong(String songPath) {
// TODO Auto-generated method stub
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/PhoneMusicList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
这是我最新的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@+id/PhoneMusicList"
android:layout_width="223dp"
android:layout_height="334dp" />
<ImageView
android:id="@+id/play_pause"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/pause"/>
我创建了一个已经正常运行的应用程序,它显示了ListView项目,当选择列表中的项目时,它会打开新的活动。在new_activity内部,我有TextView和Image按钮,显示音频按钮,播放,暂停和&amp;停。 TextView功能完美,但图像按钮拒绝播放原始文件夹中的音频文件。我在原始文件夹中有许多音频文件,并希望将每个文件分配给每个将打开每个新活动的ListView。请帮我编辑下面的ListViewAdapter代码来播放,暂停和播放停止播放每个音频文件。
如何编辑此NewActivity.java以使用原始文件夹中的歌曲填充它?非常感谢您的支持。
public class NewActivity extends AppCompatActivity {
private String TAG = "NewActivity ----- ; " ;
// Store instance variables
private String title;
private int page;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
ActionBar actionBar = getSupportActionBar();
TextView mDetailTv = findViewById(R.id.textView);
//get data from previous activity when item of activity is clicked using intent
Intent intent = getIntent();
String mActionBarTitle = intent.getStringExtra("actionBarTitle");
String mContent = intent.getStringExtra("contentTv");
//setctionBar Title
actionBar.setTitle(mActionBarTitle);
//get text in text textView
mDetailTv.setText(mContent);
//ok we are done,
public void play(View v) {
if (player == null) {
player = MediaPlayer.create(this, R.raw.song_1);
Toast.makeText(this, "Tune Playing", Toast.LENGTH_SHORT ).show();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
public void pause(View v) {
if (player != null) {
player.pause();
Toast.makeText(this, "Tune Paused", Toast.LENGTH_SHORT ).show();
}
}
public void stop(View v) {
stopPlayer();
}
private void stopPlayer() {
if (player != null) {
player.release();
player = null;
Toast.makeText(this, "Tune Stoped", Toast.LENGTH_SHORT ).show();
}
}
@Override
protected void onStop() {
super.onStop();
stopPlayer();
}
}
&#13;
答案 0 :(得分:1)
尝试使用以下代码。
public class Recording_List extends Activity{
ListView mListView;
ImageView mPlayPause;
private File file;
private MediaPlayer mp = new MediaPlayer();
private Handler mHandler = new Handler();
private List<String> myList = new ArrayList<String>();
private static final String MEDIA_PATH = new String(
Environment.getExternalStorageDirectory() + "/AudioRecorder/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordlist);
mListView = (ListView) findViewById(R.id.recordList);
mPlayPause = (ImageView) findViewById(R.id.play_pause);
file = new File(MEDIA_PATH);
if (!file.exists()) {
file.mkdir();
}
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList);
adapter.notifyDataSetChanged();
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setCacheColorHint(Color.TRANSPARENT);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
playSong(MEDIA_PATH + myList.get(position));
}
});
mPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
mPlayPause.setImageResource(R.drawable.play);
}
} else {
if (mp != null) {
mp.start();
mPlayPause.setImageResource(R.drawable.pause);
}
}
}
});
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.release();
}
}
答案 1 :(得分:0)
你应该改变targetPath,如下所示
targetPath =targetPath + "/AudioRecorder/" ;
或替换
playSong(targetPath + MyFiles.get(position));
与
playSong(targetPath +"/AudioRecorder/"+ MyFiles.get(position));
答案 2 :(得分:0)
记录器的Ringa_Singa文件夹中文件的列表视图。 当您停止播放时,您必须返回以更新列表,否则就可以了。 您可以通过该播放按钮播放最新唱片
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_audio);
//List View
final ListView listView = (ListView) findViewById(R.id.listView);
myList = new ArrayList<String>();
//Recording events on button
startbtn = findViewById(R.id.btnRecord);
stopbtn = findViewById(R.id.btnStop);
playbtn = findViewById(R.id.btnPlay);
stopplay = findViewById(R.id.btnStopPlay);
stopbtn.setEnabled(false);
playbtn.setEnabled(false);
stopplay.setEnabled(false);
mFileName = Environment.getExternalStorageDirectory()+ "/RingaSinga";
int entryNumber = 1;
File mFile = new File(mFileName + "/RingaSinga_" + String.valueOf(entryNumber) + ".wav");
while (mFile.exists()) {
entryNumber++;
mFile = new File(mFileName + "/RingaSinga_" + String.valueOf(entryNumber) + ".wav");
}
this.mFileName = mFile.getAbsolutePath();
//List all files on list view
File directory = Environment.getExternalStorageDirectory();
mFile = new File( directory + "/RingaSinga" );
final File list[] = mFile.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.
//Click listner on List item
final File finalMFile = mFile;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(RecordAudio.this, ""+ listView, Toast.LENGTH_SHORT).show();
playSong(finalMFile + myList.get(position));
}
});
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (CheckPermissions()) {
stopbtn.setEnabled(true);
startbtn.setEnabled(false);
playbtn.setEnabled(false);
stopplay.setEnabled(false);
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(mFileName);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show();
} else {
RequestPermissions();
}
}
});
stopbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopbtn.setEnabled(false);
startbtn.setEnabled(true);
playbtn.setEnabled(true);
stopplay.setEnabled(true);
mRecorder.stop();
mRecorder.release();
mRecorder = null;
Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_LONG).show();
return;
}
});
playbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopbtn.setEnabled(false);
startbtn.setEnabled(true);
playbtn.setEnabled(false);
stopplay.setEnabled(true);
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
Toast.makeText(getApplicationContext(), "Recording Started Playing", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
});
stopplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPlayer.release();
mPlayer = null;
stopbtn.setEnabled(false);
startbtn.setEnabled(true);
playbtn.setEnabled(true);
stopplay.setEnabled(false);
Toast.makeText(getApplicationContext(), "Playing Audio Stopped", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_AUDIO_PERMISSION_CODE:
if (grantResults.length > 0) {
boolean permissionToRecord = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean permissionToStore = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean permissionToRead = grantResults[2] == PackageManager.PERMISSION_GRANTED;
if (permissionToRecord && permissionToStore && permissionToRead) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean CheckPermissions() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
int result2 = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED;
}
private void RequestPermissions() {
ActivityCompat.requestPermissions(RecordAudio.this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, REQUEST_AUDIO_PERMISSION_CODE);
}
private void playSong(String songPath) {
try {
mPlayer = new MediaPlayer();
mPlayer.reset();
mPlayer.setDataSource(songPath);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mPlayer.release();
}