java - Java,Android 我的 MediaPlayer API 无法启动
问题描述
我是一名新程序员,我编写了一个MediaPlayer
API,但该start()
函数不起作用。
public class PlaySound {
public Uri defaultAlarmAlertUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
public int sound = R.raw.a;
public static MediaPlayer mediaPlayer;
private Context context;
private Uri uri;
int playSound;
private Boolean playing;
public PlaySound(Context context){
this.context = context;
playing = false;
if(mediaPlayer != null){
playing = mediaPlayer.isPlaying();
}
playSound = 0;
uri = defaultAlarmAlertUri;
}
public PlaySound setSound(Uri uri){
this.uri = uri;
return this;
}
public PlaySound setSound(int uri){
this.playSound = uri;
return this;
}
public PlaySound start() {
if (uri != null && playSound != 0) {
mediaPlayer = MediaPlayer.create(context, uri);
} else {
mediaPlayer = MediaPlayer.create(context, playSound);
}
playing = true;
mediaPlayer.stop();
return this;
}
public Boolean isPlaying(){
return playing;
}
public PlaySound stop(){
if(mediaPlayer != null &&isPlaying()) {
mediaPlayer.reset();
mediaPlayer.stop();
}
return this;
}
}
解决方案
我发现了错误。
public class PlaySound {
public Uri defaultAlarmAlertUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
public int sound = R.raw.a;
public static MediaPlayer mediaPlayer;
private Context context;
private Uri uri;
int playSound;
private Boolean playing;
public PlaySound(Context context){
this.context = context;
playing = false;
if(mediaPlayer != null){
playing = mediaPlayer.isPlaying();
}
playSound = 0;
uri = defaultAlarmAlertUri;
}
public PlaySound setSound(Uri uri){
this.uri = uri;
return this;
}
public PlaySound setSound(int uri){
this.playSound = uri;
return this;
}
public PlaySound start() {
if (uri != null && playSound == 0) {
mediaPlayer = MediaPlayer.create(context, uri);
} else {
mediaPlayer = MediaPlayer.create(context, playSound);
}
playing = true;
mediaPlayer.start();
return this;
}
public Boolean isPlaying(){
return playing;
}
public PlaySound stop(){
if(mediaPlayer != null &&isPlaying()) {
mediaPlayer.reset();
mediaPlayer.stop();
}
return this;
}
}
推荐阅读
- selenium - 如何使远程机器始终处于活动状态以执行自动化执行
- python - 在 Django 3 项目中使用 Motor 或任何其他异步 MongoDB 驱动程序
- tensorflow - 带有生成器的 tf.data api 的多个输入
- sql-server - 最近日期的左外连接与 CASE 条件
- java - @RunAs javax 安全注解有什么用?
- datetime - 如何在 Flutter 中查找特定周的工作日?
- selenium - i 帧的硒问题
- reactjs - 如何检查用户输入是否与反应中的特定正则表达式匹配?
- java - 查找图像 OpenCV Android SDK 中的颜色百分比
- database - PostgreSQL 中的并发查询 - 实际发生了什么?