gpt4 book ai didi

java - MediaPlayer 播放和重播问题

转载 作者:行者123 更新时间:2023-11-29 21:36:07 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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 转换您的文件.

关于java - MediaPlayer 播放和重播问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18409957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com