在bluestack app播放器播放音频工作正常吗?但不是在设备中

时间:2015-12-29 09:34:04

标签: android

我是Android应用程序开发的新手。我正在开发一个项目。包含100个音频文件。所有这些都放在资产文件夹中。播放音频我需要一个Mediaplayer类

MusicPlayer.java


/** Plays music and one-off sound files while managing the resources efficiently */
public class MusicPlayer {

    private static MusicPlayer mInstance;
    private MediaPlayer mMediaPlayer;

    private MusicPlayer() { }

    /**
     * Returns the single instance of this class
     *
     * @return the instance
     */
   /* */

    public static MusicPlayer getInstance() {

        if (mInstance == null) {
            mInstance = new MusicPlayer();
        }

        return mInstance;
    }

    /**
     * Plays the sound with the given resource ID
     *
     * @param context a valid `Context` reference
     * @param soundResourceId the resource ID of the sound (e.g. `R.raw.my_sound`)
     */
    public synchronized void play(final Context context, final int soundResourceId) {
        // if there's an existing stream playing already
        if (mMediaPlayer != null) {
            // stop the stream in case it's still playing
            try {
                mMediaPlayer.stop();
            }
            catch (Exception e) { }

            // release the resources
            mMediaPlayer.release();

            // unset the reference
            mMediaPlayer = null;
        }

        // create a new stream for the sound to play
        mMediaPlayer = MediaPlayer.create(context.getApplicationContext(), soundResourceId);

        // if the instance could be created
        if (mMediaPlayer != null) {
            // set a listener that is called when playback has been finished
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(final MediaPlayer mp) {
                    // if the instance is set
                    if (mp != null) {
                        // release the resources
                        mp.release();

                        // unset the reference
                        mMediaPlayer = null;
                    }
                }

            });

            // start playback
            mMediaPlayer.start();
        }
    }

    public synchronized void stop() {
        // if there's an existing stream playing already

                mMediaPlayer.stop();

            // release the resources
            mMediaPlayer.release();

            // unset the reference
            mMediaPlayer = null;
        }

    public synchronized void isPlaying() {
        // if there's an existing stream playing already
if(mMediaPlayer != null)
{
    mMediaPlayer.stop();

    // release the resources
    mMediaPlayer.release();

    // unset the reference

}

    }
}
在我使用的活动中

MusicPlayer.getInstance().play(Activity.this, R.raw.gajju_urturn);

当我在bluestack app播放器中运行项目时,所有音频都在播放。但是当我在真实设备中运行而不播放音频并获得错误时。 PLZ任何人帮我解决问题。在我的logcat中显示以下错误...

12-29 15:02:23.076 219-18360/? E/GenericSource: Failed to init from data source!
12-29 15:02:23.077 18346-18358/? E/MediaPlayer: error (1, -2147483648)
12-29 15:02:34.130 219-18366/? E/GenericSource: Failed to init from data source!
12-29 15:02:34.131 18346-18357/? E/MediaPlayer: error (1, -2147483648)
12-29 15:02:38.100 219-18368/? E/GenericSource: Failed to init from data source!
12-29 15:02:38.101 18346-18357/? E/MediaPlayer: error (1, -2147483648)
12-29 15:02:53.618 219-18370/? E/GenericSource: Failed to init from data source!
12-29 15:02:53.620 18346-18358/? E/MediaPlayer: error (1, -2147483648)
12-29 15:03:03.138 219-18373/? E/GenericSource: Failed to init from data source!
12-29 15:03:03.138 18346-18358/? E/MediaPlayer: error (1, -2147483648)

1 个答案:

答案 0 :(得分:0)

和我一起工作。检查资源格式,还检查“raw”目录是否包含所请求的文件。还可以在其他手机中运行您的代码。我使用了相同的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MusicPlayer.getInstance().play(this, R.raw.no_promises);
}