我查看了各种不同的网站,在我的代码中寻找解决此问题的方法。它是一个基本的音频播放器,有 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);
}
}
}
我也尝试过在没有搜索栏的情况下让基础工作,只是为了确保这不会导致错误,但它位于播放/暂停/停止代码的逻辑范围内,因为即使使用搜索栏代码它也会产生相同的错误省略。