java Android MediaPlayer 不播放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41482240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:54:53  来源:igfitidea点击:

Android MediaPlayer not playing

javaandroidaudioandroid-mediaplayer

提问by BeNeDeV

Following problem: I just want to play a small .wavfile with a mediaplayer in Android. The .wavfile is in the rawdirectory. I want to start the sound from the MainActivity. This is how I play the sound:

以下问题:我只想.wav在 Android 中使用媒体播放器播放一个小文件。该.wav文件在raw目录中。我想从 MainActivity 开始声音。这是我播放声音的方式:

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound1);
mp.start();

As soon as I call mp.start()I get following errorsin my LogCat:

一旦我打电话,mp.start()我的 LogCat 就会出现以下错误:

01-05 11:07:17.729 19960-19960/de.benedev.simplesound E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
01-05 11:07:17.729 19960-19960/de.benedev.simplesound E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
01-05 11:07:17.862 19960-19960/de.benedev.simplesound D/MediaPlayer: setSubtitleAnchor in MediaPlayer
01-05 11:07:17.925 19960-21184/de.benedev.simplesound E/MediaPlayer: error (1, -2147483648)
01-05 11:07:17.926 19960-19960/de.benedev.simplesound E/MediaPlayer: Error (1,-2147483648)

Anyone knows how tho fix that?

任何人都知道如何解决这个问题?

采纳答案by Arpan Sharma

MediaPlayer player = MediaPlayer.create(this,  R.raw.zindgivalaaop);
player.setVolume(50,50);
player.start();

@Override
public void onDestroy() {
     super.onDestroy();
     player.stop();
     player.release();
}

try passing class context instead getApplicationContext()

尝试传递类上下文 getApplicationContext()

回答by Chingiz

Use the code below which answered here. It'll work like a charm.

使用下面回答here的代码。它会像魅力一样发挥作用。

package com.example.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;

public class AudioPlayer {

private MediaPlayer mMediaPlayer;

public void stop() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

public void play(Context c, int rid) {
    stop();

    mMediaPlayer = MediaPlayer.create(c, rid);
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            stop();
        }
    });

    mMediaPlayer.start();
}

}

回答by Krupa Kakkad

Try this: Playing short .wav files - Android

试试这个:播放简短的 .wav 文件 - Android

SoundPool is the best way to play short .wav(2-3 sec.) file

SoundPool 是播放短 .wav(2-3 秒)文件的最佳方式

For more information: https://developer.android.com/reference/android/media/SoundPool.html

更多信息:https: //developer.android.com/reference/android/media/SoundPool.html

回答by Harshitha Palihawadana

I think your MediaPlayer is working. Check the device 'Media volume' in settings. JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0this message comes when the device 'Media volume' set to zero.

我认为您的 MediaPlayer 正在运行。在设置中检查设备“媒体音量”。JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0当设备“媒体音量”设置为零时,会出现此消息。