我查看了各种不同的网站,在我的代码中寻找解决此问题的方法。它是一个基本的音频播放器,有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);
}
}
}
我已经尝试过在没有搜索栏的情况下运行基础知识,只是为了确保不会导致错误,但它位于播放/暂停/停止代码的逻辑中,因为即使使用它也会产生相同的错误搜索条代码被省略。
答案 0 :(得分:8)
问题是,一旦你停止了它,你再次setDataSource
。然后,您必须致电prepare()
player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
player.prepare();
如果您在其上致电setDataSource()
,则需要再次prepare()
。可以把它想象成加载文件,准备播放。
顺便提一下,我注意到你混淆了调用MediaPlayer.create()
并且多次为同一个文件调用setDataSource()
。没有必要这样做。创建和准备MediaPlayer后(调用.create()
为您执行此操作),您只需在其上调用.stop()
,.pause()
,.play()
,无需重置所有内容时间。
答案 1 :(得分:1)
我通过删除播放器因player.stop()
完成歌曲时调用的player.setOnCompletionListener()
来解决此问题。这已经解决了问题,玩家工作正常。
答案 2 :(得分:0)
我通过`mediaPlayer.prepare()修复了这个问题;我的问题是关于直播无线电流,我实现了这个问题,如下面的代码:
if (!radioIsOpen) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer
.setDataSource(URL);
mediaPlayer.prepare();
} catch (Exception e) {
}
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
Toast.makeText(getBaseContext(), "Radio is opening...", 2).show();
} else {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
Toast.makeText(getBaseContext(), "Radio is closing...", 2).show();
}
}