12

我查看了各种不同的网站,在我的代码中寻找解决此问题的方法。它是一个基本的音频播放器,有 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);
    }
}
}

我也尝试过在没有搜索栏的情况下让基础工作,只是为了确保这不会导致错误,但它位于播放/暂停/停止代码的逻辑范围内,因为即使使用搜索栏代码它也会产生相同的错误省略。

4

3 回答 3

9

问题是一旦你停止了它,你setDataSource又一次。然后你必须打电话prepare()

player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
player.prepare();

任何时候你调用setDataSource()它,你都需要prepare()再次调用它。将其视为加载文件,准备播放。

顺便说一句,我注意到您混淆了调用MediaPlayer.create()并且还setDataSource()多次调用同一个文件。没有必要这样做。一旦创建并准备好 MediaPlayer(调用.create()为你做的),你只需调用.stop(), .pause(),.play()就可以了,不需要一直重置它。

于 2013-07-18T13:37:49.227 回答
1

我通过删除player.stop()播放器完成歌曲时因player.setOnCompletionListener(). 现在这已经解决了问题,播放器运行良好。

于 2013-07-19T09:31:28.523 回答
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();
                }
            }
于 2014-12-28T13:59:08.043 回答