用Android studio 2.1写的
如题,简单的调用一个Media Player来播放一首歌曲,语法应该没啥错误,但是在模拟机上运行的时候,程序可以打开,但是按播放按钮的时候就这样了。(歌曲也放在路径下了)
代码如下,大佬们帮看看到时是代码的问题,还是缺什么东西没装?
package com.example.dy.musicplayer;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button startBtn;
SeekBar positionBar;
SeekBar volumeBar;
TextView elapsedTimeLabel;
TextView remainingTimeLabel;
MediaPlayer mp;
int totalTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn = (Button) findViewById(R.id.startBtn);
elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
remainingTimeLabel = (TextView) findViewById(R.id.remainingTimeLabel);
//Media Player
mp = MediaPlayer.create(this, R.raw.music);
mp.setLooping(true);
mp.seekTo(0);
mp.setVolume(0.5f, 0.5f);
totalTime = mp.getDuration();
//position Bar
positionBar = (SeekBar) findViewById(R.id.positionBar);
positionBar.setMax(totalTime);
//Volume Bar
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
}
public void StartBtnClick(View view){
if(!mp.isPlaying()){
//stoping
mp.start();
startBtn.setBackgroundResource(R.drawable.stop);
}else{
//Palying
mp.pause();
startBtn.setBackgroundResource(R.drawable.start);
}
}
}
下面这是界面布局
<?xml version="1.0" encoding="utf-8"?><ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/test"
android:layout_marginTop="20dp" />
<SeekBar
android:id="@+id/positionBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/elapsedTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:11"
android:layout_marginLeft="40dp" />
<TextView
android:id="@+id/remainingTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1:49"
android:layout_marginLeft="240dp"/>
</LinearLayout>
<Button
android:id="@+id/startBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/start"
android:layout_marginTop="40dp"
android:onClick="startBtnClick"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp"
android:gravity="center">
<ImageButton
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/sound"/>
<SeekBar
android:id="@+id/volumeBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:progress="50"
android:max="100"/>
<ImageView
android:layout_width="26dp"
android:layout_height="26dp"
android:src="@drawable/sound2"/>
</LinearLayout>