目前我正在制作音乐播放器。请帮我解决这些错误
public class MainActivity extends Activity {
Button playb,pauseb,stopb;
SeekBar seeker;
Handler seekhandler;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player=new MediaPlayer();//.create(MainActivity.this, R.raw.spaceman);
//Interface between code and xml
playb=(Button)findViewById(R.id.play_button);
stopb=(Button)findViewById(R.id.stop_button);
pauseb=(Button)findViewById(R.id.pause_button);
seeker=(SeekBar)findViewById(R.id.seekBar);
try {
player.prepare();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),(CharSequence) e,Toast.LENGTH_SHORT ).show();
}
//Code for Play Button
playb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
player.stop();
player.reset();
seeker.setProgress(0);
try {
player.prepare();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), (CharSequence) e,Toast.LENGTH_SHORT).show();
}
player.start();
seeker.setMax(player.getDuration());
}
});
//Code for Pause button
pauseb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.pause();
}
});
//code for Stop button
stopb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.reset();
}
});
//Code for seekbar change listner
seeker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Runnable runnable =new Runnable() {
@Override
public void run() {
seeker.setProgress(player.getCurrentPosition());
seekhandler.postDelayed(this,1000);
}
};
}
}
错误:
09-19 20:16:15.342 25242-25242 / main.deviac.com.seekbar E / AndroidRuntime:FATAL EXCEPTION:main 处理:main.deviac.com.seekbar,PID:25242 java.lang.RuntimeException:无法启动活动ComponentInfo {main.deviac.com.seekbar / main.deviac.com.seekbar.MainActivity}:java.lang.IllegalStateException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 在android.app.ActivityThread.access $ 900(ActivityThread.java:154) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1380) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:152) 在android.app.ActivityThread.main(ActivityThread.java:5497) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.IllegalStateException 在android.media.MediaPlayer._prepare(本机方法) 在android.media.MediaPlayer.prepare(MediaPlayer.java:1158) 在main.deviac.com.seekbar.MainActivity.onCreate(MainActivity.java:42) 在android.app.Activity.performCreate(Activity.java:6285) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 在android.app.ActivityThread.access $ 900(ActivityThread.java:154) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1380) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:152) 在android.app.ActivityThread.main(ActivityThread.java:5497) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 09-19 20:16:15.344 25242-25242 / main.deviac.com.seekbar D / AppTracker:App事件:崩溃 09-19 20:16:28.590 25409-25409 / main.deviac.com.seekbar W / System:ClassLoader引用未知路径:/data/app/main.deviac.com.seekbar-2/lib/arm64 09-19 20:16:28.669 25409-25409 / main.deviac.com.seekbar W / System:ClassLoader引用未知路径:/data/app/main.deviac.com.seekbar-2/lib/arm64
我把mp3文件(太空人)保存在res目录中的原始文件夹中
答案 0 :(得分:2)
欢迎使用Android的媒体播放器,或者我称之为"基于状态的地狱"。
您的原始错误是由准备媒体播放器引起的,如果您在本地播放音频文件,则不会准备媒体播放器,因为create()方法(如果您使用此方法创建媒体播放器)会自动准备。还有其他媒体播放器基于状态的错误,但下面的代码应该修复它们。
立即忽略搜索栏代码(我会在添加搜索栏功能之前获得此基本播放,暂停和停止代码),播放,暂停和停止按钮的代码应如下所示:
public class MainActivity extends Activity {
Button playb, pauseb, stopb;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Interface between code and xml
playb = (Button) findViewById(R.id.play_button);
stopb = (Button) findViewById(R.id.stop_button);
pauseb = (Button) findViewById(R.id.pause_button);
//Code for Play Button
playb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.file_to_play.mp3);
mediaPlayer.start();
}
});
//Code for Pause button
pauseb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
});
//code for Stop button
stopb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
}
}
});
}
答案 1 :(得分:0)
您已将文件路径添加到媒体播放器。只需在媒体播放器类中调用静态方法create
MediaPlayer player = MediaPlayer.create(this, R.raw.yoursongfile);
或调用setDataSource
方法
player.setDataSource("/sdcard/Music/songfile.mp3");//Path of your song location
或者您也可以访问以下资源
player.setDataSource(context, Uri.parse("android.resource://com.package.name/raw/songfile"));