真机播放 资源内and手机目录 mp3文件
话不多先上正确代码
下面展示Android Studio 我的正确代码Java 代码段
:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mp3);
//因为是6.0以上的手机安卓版本所有加了一个申请权限的方法
verifyStoragePermissions(this);
//定义全局变量 m mm 两个路径的文件播放所以有两个
final MediaPlayer m;
final MediaPlayer mm;
m=MediaPlayer.create(mp3.this,R.raw.nide);//初始化
m.setLooping(true);//设置循环播放
mm=new MediaPlayer();//初始化
try {
//这儿就是有些问题的地方,另一种写法 mm.setDataSource("这个写路径");
//这种写法,在这儿使用会报错,具体问题我也不清楚,会报错说找不到文件,但是实际上我的文件是放好了
File file = new File(Environment.getExternalStorageDirectory(), "cc.mp3");
//此处两个 setDataSource()以及prepare() 方法都要你使用错误信息处理 try catch 方法,加上就行了
mm.setDataSource(file.getPath());
mm.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mm.setLooping(true);
final ImageButton b1=(ImageButton)findViewById(R.id.b1);
final ImageButton b2=(ImageButton)findViewById(R.id.b2);
final ImageButton b3=(ImageButton)findViewById(R.id.b3);
final ImageButton b4=(ImageButton)findViewById(R.id.b4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//单机事件 因为只有一个 ImageButton 所以会更换样式
if(m.isPlaying()){
m.pause();
b1.setImageResource(R.drawable.music_play);
}else{
m.start();
b1.setImageResource(R.drawable.music_pause);
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m.stop();
// m.release(); //原本是清除之前播放系统占用的资源
try {
m.prepare();//因为停止了,所以重新准备资源
} catch (IOException e) {
e.printStackTrace();
}
b1.setImageResource(R.drawable.music_play);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mm.isPlaying()){
mm.pause();
b3.setImageResource(R.drawable.music_play);
}else{
mm.start();
b3.setImageResource(R.drawable.music_pause);
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mm.stop();
// mm.release();
try {
mm.prepare();
} catch (IOException e) {
e.printStackTrace();
}
b3.setImageResource(R.drawable.music_play);
}
});
}
因为之前一直在再用 setDataSource()方法
但是一直报错,也没有找到具体报错原因,但是在真机运行的时候,用以上代码就不会报错!
layout代码入下:
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/girl"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/music_play"/>
<ImageButton
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/music_stop" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sd播放"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/music_play"/>
<ImageButton
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/music_stop" />
</LinearLayout>