【发布时间】:2014-03-22 12:27:44
【问题描述】:
我正在尝试从音频文件中填充列表视图并想要播放单击的项目。它不工作。任何帮助。
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"/>
I have created an already functioning app which displays ListView items and when an item in the list is selected, it then opens new activity.在 new_activity 中,我有 TextView 和 Image 按钮,显示播放、暂停和停止的音频按钮。 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();
}
}
【问题讨论】:
-
你遇到什么问题,我的意思是你有什么错误吗?
-
尝试以下解决方案,如果您遇到任何问题,请告诉我
-
我遇到的问题是应用程序没有打开,它崩溃了
标签: android android-mediaplayer