我正在制作一个简单的MP3播放器。 A.mp3
在sdcard上。关于mp3播放器
当我点击按钮玩注意到,为什么?
我不知道它有什么问题。
这里的MainActivity
文件:
public class MainActivity extends Activity implements OnClickListener{
private Button play;
private Button pause;
private Button stop;
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play =(Button)findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
initMediaPlayer();
}
private void initMediaPlayer()
{
try {
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
initMediaPlayer();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
这里是Layout
文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play" />
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause" />
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop" />
我发现SD卡的路径是 “/存储/ sdcard1 /”。所以我使用mediaPlayer.setDataSource(“/ storage/sdcard1/A.mp3”);但它是一样的.setDataSource没有运行
你可以发布你的xml在哪里定义按钮? – TheTanic
我已发布xml .. –
您是否检查您的应用程序是否输入案例? – TheTanic