1

我正在尝试设置一个带有声音的媒体播放器,可以根据需要多次播放和重放。但是我在第一次播放音频时遇到了这个错误:

08-23 14:48:52.613: E/MediaPlayer(24194): error (1, -2147483648)
08-23 14:48:52.613: V/Preschool Basics(24194): Prepare failed.: status=0x1

我调用该剧的代码是:

    btnPlay = (Button) findViewById(R.id.soundButton);
    btnStop = (Button) findViewById(R.id.stopButton);

    mpSound = MediaPlayer.create(this, R.raw.a);
    mpSound.setLooping(false);
    btnPlay.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                //mpSound.start();
                Uri uri = Uri.parse("android.resource://com.test.testing/" + R.raw.a);
                //Toast.makeText(getApplicationContext(), uri.toString(), 2000).show();
                playSong(uri.toString());
                btnPlay.setVisibility(View.GONE);
                btnStop.setVisibility(View.VISIBLE);
                btnStop.setOnClickListener(stopSound);
        }
    });

View.OnClickListener stopSound = new View.OnClickListener() {
    public void onClick(View v) {
        if (mpSound != null) {
            mpSound.stop();
            //mpSound.release();
            btnPlay.setVisibility(View.VISIBLE);
            btnStop.setVisibility(View.GONE);
        }
    }
};

playSong()功能是:

    private void playSong(String songPath) {
        try {

                mpSound.reset();
                mpSound.setDataSource(songPath);
                mpSound.prepare();
                mpSound.start();

                //Stop the song and replace button
                mpSound.setOnCompletionListener(new OnCompletionListener() {
                        public void onCompletion(MediaPlayer arg0) {
                            mpSound.stop();
                            btnPlay.setVisibility(View.VISIBLE);
                            btnStop.setVisibility(View.GONE);
                        }

                });

        } catch (IOException e) {
                Log.v(getString(R.string.app_name), e.getMessage());
        }
}

我的声音位于res/raw/a.mp3

当我按下btnPlay按钮时,我会显示btnStop按钮,但没有播放任何内容。

我希望让用户播放和停止播放等等。我怎样才能实现它?

日志猫:

08-23 15:41:20.761: E/AndroidRuntime(29438): FATAL EXCEPTION: main
08-23 15:41:20.761: E/AndroidRuntime(29438): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.testing/com.test.testing.AlpDisplay}: java.lang.NullPointerException
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.os.Looper.loop(Looper.java:137)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.ActivityThread.main(ActivityThread.java:5195)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at java.lang.reflect.Method.invoke(Method.java:511)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at dalvik.system.NativeStart.main(Native Method)
08-23 15:41:20.761: E/AndroidRuntime(29438): Caused by: java.lang.NullPointerException
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.media.MediaPlayer.create(MediaPlayer.java:824)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at com.test.testing.MusicPlayer.<init>(MusicPlayer.java:13)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at com.test.testing.AlpDisplay.<init>(AlpDisplay.java:45)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at java.lang.Class.newInstanceImpl(Native Method)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at java.lang.Class.newInstance(Class.java:1319)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-23 15:41:20.761: E/AndroidRuntime(29438):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
08-23 15:41:20.761: E/AndroidRuntime(29438):    ... 11 more
4

1 回答 1

2

它可以做得更简单。我已经测试了这段代码,它正在工作:

MusicPlayer.java

public class MusicPlayer
{
    private final int songResId = R.raw.your_song;
    private final MediaPlayer mediaPlayer;

    public MusicPlayer(Context context)
    {
        mediaPlayer = MediaPlayer.create(context, songResId);
        mediaPlayer.setLooping(true);
    }

    public void setPlaying(boolean isPlaying)
    {
        if (isPlaying)
        {
            mediaPlayer.start();
        }
        else
        {
            mediaPlayer.stop();
        }
    }

    public void release()
    {
        mediaPlayer.stop();
        mediaPlayer.release();
    }
}

用法(来自您的Activity):

MusicPlayer musicPlayer = new MusicPlayer(this);
musicPlayer.setPlaying(true);  // play
musicPlayer.setPlaying(false); // stop
musicPlayer.setPlaying(true);  // play from the beginning    
musicPlayer.release();         // when done using MusicPlayer

确保你明白release()

对于歌曲,我推荐 OGG 格式(最大压缩)而不是 MP3,因为它通常占用大约 50% 的空间,而且质量难以区分。您可以使用例如Audacity转换您的文件。

于 2013-08-23T19:19:09.160 回答