我查看了各种不同的网站,寻找解决我的代码问题的方法。这是一个基本的音频播放器,有3个按钮:播放、暂停和停止。播放和暂停都可以正常工作,停止也可以,但一旦音频被停止,它就无法再次启动,会抛出错误:
E/MediaPlayer: stop called in state 0
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)
这是我的MainActivity.java文件:
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener {
static Button playButton;
static Button pauseButton;
static Button stopButton;
static SeekBar seekBar;
private static MediaPlayer player;
static Handler handler;
static Uri audio;
@Override
protected void onCreate(Bundle savedInstanceState) {
player = MediaPlayer.create(this, R.raw.airbourne_runnin_wild);
Uri audio = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.airbourne_runnin_wild);
try {
player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
} catch (IOException e) {
e.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button) findViewById(R.id.playButton);
pauseButton = (Button) findViewById(R.id.pauseButton);
stopButton = (Button) findViewById(R.id.stopButton);
seekBar = (SeekBar) findViewById(R.id.seekBar);
player.setOnPreparedListener(this);
handler = new Handler();
seekBar.setMax(player.getDuration());
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (!player.isPlaying()) {
try {
player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
} catch (IOException e) {
e.printStackTrace();
}
player.start();
updateSeekBar();
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (player.isPlaying()) {
player.pause();
}
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (player.isPlaying()) {
player.stop();
seekBar.setProgress(0);
seekBar.invalidate();
try {
player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
seekChanged(seekBar, i, b);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
player.stop();
seekBar.setProgress(0);
//player.prepareAsync();
seekBar.invalidate();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
playButton.setEnabled(true);
}
public void seekChanged(SeekBar seekBar, int progress, boolean fromUser) {
player.seekTo(progress);
}
public void updateSeekBar() {
seekBar.setProgress(player.getCurrentPosition());
if (player.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
updateSeekBar();
}
};
handler.postDelayed(notification, 1500);
}
}
}
在activity_main.xml中:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/playButton"
android:layout_gravity="center_horizontal|top"
android:longClickable="false"
android:layout_weight="1"
android:layout_column="8"
android:enabled="false"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:id="@+id/pauseButton"
android:layout_gravity="center_horizontal|top"
android:layout_weight="1"
android:layout_column="8"
android:layout_span="7"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/stopButton"
android:layout_column="8"
android:layout_span="24"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_column="8"
android:layout_span="24"
android:layout_weight="1"/>
</TableRow>
如果有人能帮我解决这个错误,将不胜感激。我已经搜索了解决方案并尝试了多种方法,但都似乎没有正常工作。
编辑:我已经稍微改了一下代码:
我的问题仍然与错误有关,这是我现在编辑过的代码:
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener {
static Button playButton;
static Button pauseButton;
static Button stopButton;
static SeekBar seekBar;
private static MediaPlayer player;
static Handler handler;
static Uri audio;
static boolean canMakeCall = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
player = MediaPlayer.create(this, R.raw.airbourne_runnin_wild);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button) findViewById(R.id.playButton);
pauseButton = (Button) findViewById(R.id.pauseButton);
stopButton = (Button) findViewById(R.id.stopButton);
seekBar = (SeekBar) findViewById(R.id.seekBar);
if (canMakeCall = true) {
player.setOnPreparedListener(this);
handler = new Handler();
seekBar.setMax(player.getDuration());
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (!player.isPlaying()) {
player.start();
updateSeekBar();
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (player.isPlaying()) {
player.pause();
}
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (player.isPlaying()) {
player.stop();
seekBar.setProgress(0);
seekBar.invalidate();
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
seekChanged(seekBar, i, b);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
//player.stop();
seekBar.setProgress(0);
//player.prepareAsync();
seekBar.invalidate();
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
playButton.setEnabled(true);
canMakeCall = true;
}
public void seekChanged(SeekBar seekBar, int progress, boolean fromUser) {
player.seekTo(progress);
}
public void updateSeekBar() {
seekBar.setProgress(player.getCurrentPosition());
if (player.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
updateSeekBar();
}
};
handler.postDelayed(notification, 1500);
}
}
}
我已经尝试过在没有seekbar的情况下让基础功能正常工作,以确保不会引起错误,但问题在于播放/暂停/停止代码的逻辑中,因为即使省略了seekbar代码,它仍会产生相同的错误。