简体   繁体   English

Android MediaPlayer 无法开始播放音频

[英]Android MediaPlayer won't start playing audio

I have a very simple Android application where an audio file should start playing as soon as the app opens up.我有一个非常简单的 Android 应用程序,其中音频文件应在应用程序打开后立即开始播放。

But the audio doesn't play at all and I get this warning in Logcat: MediaPlayer finalized without being released但是音频根本没有播放,我在 Logcat 中收到了这个警告: MediaPlayer finalized without being released

MainActivity.java: MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.song);

        mPlayer.start();
    }
}

I initialized MediaPlayer with the MediaPlayer.create() method, so I shouldn't have to prepare or release the mPlayer.我使用 MediaPlayer.create() 方法初始化了 MediaPlayer,因此我不必准备或释放 mPlayer。

I'm not sure if it's a problem in my code or just a sound output problem with the android emulator.我不确定这是我的代码中的问题,还是 android 仿真器的声音 output 问题。

UPDATE: I tried to declare the MediaPlayer outside of the onCreate() method and initialize it inside onCreate().更新:我尝试在 onCreate() 方法之外声明 MediaPlayer,并在 onCreate() 中对其进行初始化。 Now I don't get the warning but it still doesn't play the audio.现在我没有收到警告,但它仍然没有播放音频。

Updated MainActivity.java:更新 MainActivity.java:

public class MainActivity extends AppCompatActivity {

    MediaPlayer mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlayer = MediaPlayer.create(this, R.raw.song);

        mPlayer.start();
    }
}

UPDATE 2: I added 3 callbacks, onPreparedListener, onErrorListener, and onCompletionListener in my application.更新 2:我在我的应用程序中添加了 3 个回调,onPreparedListener、onErrorListener 和 onCompletionListener。

Updated MainActivity.java更新 MainActivity.java

public class MainActivity extends AppCompatActivity {

    MediaPlayer mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlayer = MediaPlayer.create(this, R.raw.song);

        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e("Error", "onError: " + what);
                return false;
            }
        });

        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.i("Completion", "onCompletion: Completed");
            }
        });

        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.i("Prepared", "onPrepared: Prepared MediaPlayer");
            }
        });

        mPlayer.start();
    }
}

I get the prepared log pretty much as soon as the app starts, and I get the completed log after I wait out the duration of my audio file.应用程序启动后,我几乎会立即获得准备好的日志,并在等待音频文件的持续时间后获得完整的日志。 I don't get any error logs in between.我没有收到任何错误日志。

The first you must declare the class of MediaPlayer,首先必须声明MediaPlayer的class,

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.create(this, R.raw.song);
mediaPlayer.prepare();
mediaPlayer.start();

I expect this code work and helpful to everyone that read this post我希望这段代码能正常工作并对阅读这篇文章的每个人都有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM