Android wear 应用只播放媒体 10 次

Android wear application only plays media 10 times

我正在制作一个 android wear 应用程序,它可以播放音频文件并在两个屏幕之间切换。当我点击屏幕时会播放声音并出现下一个屏幕。在声音停止之前,我可以这样做大约十次,但我可以继续按下按钮并转到下一个屏幕。我已经看过了 mediaplayer-stops-playing-after-playing-a-few-times。然而,我不知道如何将它应用到我的问题中,并且将 mp.release 添加到我的一个活动中导致了一个新的错误。有谁知道解决方案?我的声音文件大约 .1 兆字节,持续 1 秒。

这是我的代码:

public class MainActivity extends AppCompatActivity {
     final MediaPlayer mp = new MediaPlayer();
    //used to be final





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //start out with head up
        setContentView(R.layout.activity_main);

        ImageButton button1 = (ImageButton) findViewById(R.id.imageButton);

        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                                //handles audio

                    if (mp.isPlaying()) {
                        mp.stop();
                    }
                    //mp.release();


                try {
                    mp.reset();
                    AssetFileDescriptor afd;
                    afd = getAssets().openFd("Coin_Flip_Sound.mp3");
                    mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                    mp.prepare();
                    mp.start();

                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    System.out.println("This is Hans and their is a Illegal State ");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("This is a IO Exception");
                }



















                //generate random int
                double flip = Math.random();
                if (flip > .5) {

                    Intent intent = new Intent(v.getContext(), Back.class);
                    startActivityForResult(intent, 0);
                }
            }
        });

这里是 logcat 当我得到错误时:

02-01 14:24:09.675    2061-2061/com.example.hansg17.watchflip I/Choreographer﹕ Skipped 200 frames!  The application may be doing too much work on its main thread.
02-01 14:24:09.699    2061-2061/com.example.hansg17.watchflip D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
02-01 14:24:48.601    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:52.410    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:53.249    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:53.418    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:53.631    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:54.293    2061-2061/com.example.hansg17.watchflip I/Choreographer﹕ Skipped 240 frames!  The application may be doing too much work on its main thread.
02-01 14:24:54.465    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:55.135    2061-2061/com.example.hansg17.watchflip I/Choreographer﹕ Skipped 84 frames!  The application may be doing too much work on its main thread.
02-01 14:24:55.400    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:56.023    2061-2074/com.example.hansg17.watchflip E/MediaPlayer﹕ error (1, -19)
02-01 14:24:56.059    2061-2061/com.example.hansg17.watchflip E/MediaPlayer﹕ invoke failed: wrong state 0
02-01 14:24:56.059    2061-2061/com.example.hansg17.watchflip E/MediaPlayer﹕ Error (1,-19)
02-01 14:24:56.089    2061-2061/com.example.hansg17.watchflip E/MediaPlayer﹕ Error (1,-1010)
02-01 14:24:56.213    2061-2061/com.example.hansg17.watchflip D/MediaPlayer﹕ setSubtitleAnchor in MediaPlayer
02-01 14:24:56.473    2061-2073/com.example.hansg17.watchflip W/art﹕ Suspending all threads took: 5.220ms
02-01 14:24:56.782    2061-2459/com.example.hansg17.watchflip E/MediaPlayer﹕ error (1, -19)
02-01 14:24:56.787    2061-2061/com.example.hansg17.watchflip E/MediaPlayer﹕ invoke failed: wrong state 0
02-01 14:24:56.787    2061-2061/com.example.hansg17.watchflip E/MediaPlayer﹕ Error (1,-19)
02-01 14:24:56.788    2061-2061/com.example.hansg17.watchflip E/MediaPlayer﹕ Error (1,-1010)

Logcat 关闭后:

02-01 14:18:56.884     961-2788/? E/AudioFlinger﹕ createTrack_l() initCheck failed -12; no control block?
02-01 14:18:56.884     961-2788/? E/AudioTrack﹕ AudioFlinger could not create track, status: -12
02-01 14:18:56.886     961-2788/? E/AudioSink﹕ Unable to create audio track
02-01 14:18:56.887     961-2788/? W/NuPlayerRenderer﹕ openAudioSink: non offloaded open failed status: -19
02-01 14:18:56.887     961-2786/? E/NuPlayer﹕ received error(0xffffffed) from audio decoder, flushing(0), now shutting down
02-01 14:18:56.887     961-2786/? D/NuPlayerDriver﹕ notifyListener_l(0xb619abc0), (100, 1, -19)
02-01 14:18:56.887    2004-2015/com.example.hansg17.watchflip E/MediaPlayer﹕ error (1, -19)
02-01 14:18:56.887     961-2788/? W/NuPlayerRenderer﹕ onDrainAudioQueue(): audio sink is not ready
02-01 14:18:56.888     961-2788/? W/NuPlayerRenderer﹕ onDrainAudioQueue(): audio sink is not ready
02-01 14:18:56.891     961-2789/? W/AMessage﹕ failed to post message as target looper for handler 0 is gone.
02-01 14:18:56.898    2004-2004/com.example.hansg17.watchflip E/MediaPlayer﹕ Error (1,-19)
02-01 14:18:56.927    1300-1319/system_process I/ActivityManager﹕ Displayed com.example.hansg17.watchflip/.Back: +56ms
02-01 14:18:57.389    1300-1319/system_process I/Choreographer﹕ Skipped 32 frames!  The application may be doing too much work on its main thread.
02-01 14:18:57.460    1300-1319/system_process I/Choreographer﹕ Skipped 41 frames!  The application may be doing too much work on its main thread.
02-01 14:18:57.526    1300-1319/system_process I/Choreographer﹕ Skipped 33 frames!  The application may be doing too much work on its main thread.

如果你想要更多 logcat 问我。有很多。

我仍然推荐 SoundPool(我链接到的那个 SO post 中有足够的信息让你继续)。但如果您更愿意坚持使用 MediaPlayer,我建议您将大部分代码移出 onClick 处理程序。像这样:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    mp.reset();
    AssetFileDescriptor afd = getAssets().openFd("Coin_Flip_Sound.mp3");
    mp.setDataSource(afd.getFileDescriptor(),
            afd.getStartOffset(), afd.getLength());
    mp.prepareAsync();

    ImageButton button1 = (ImageButton) findViewById(R.id.imageButton);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (mp.isPlaying()) {
                mp.stop();
            }

            try {
                mp.start();
            } catch (Exception e) {
                ...
            }

            //generate random int
            ...
        }
    });
}

换句话说,每次点击按钮时都不需要加载资源和prepare MediaPlayer;提前做一次。

此外,请注意我使用了 prepareAsync() 而不仅仅是 prepare(),将文件系统访问和 MediaPlayer 初始化从 UI 线程中移出。