【问题标题】:Android wear application only plays media 10 timesAndroid Wear 应用只播放媒体 10 次
【发布时间】:2016-02-01 22:26:49
【问题描述】:

我正在制作一个播放音频文件并在两个屏幕之间翻转的 Android Wear 应用程序。当我点击屏幕时,会播放声音并出现下一个屏幕。在声音停止之前我可以这样做大约十次,但我可以继续按下按钮并转到下一个屏幕。我已经看过了 mediaplayer-stops-playing-after-playing-a-few-times。但是,我不知道如何将其应用于我的问题,并且将 mp.release 添加到我的一项活动中导致了一个新错误。有谁知道解决方案?我的声音文件大约 0.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,请咨询我。有很多。

【问题讨论】:

  • 只是一个建议,MediaPlayer 并不适合抛硬币等短片。有关使用 SoundPoolPlayer 的示例代码,请参阅 stackoverflow.com/questions/13883883,这可能是更好的选择。
  • 我不太了解声音池,StackOverflow 之外的信息也不多

标签: android android-intent android-mediaplayer wear-os


【解决方案1】:

我仍然推荐 SoundPool(我链接到的那篇 SO 帖子中有足够的信息让你继续前进)。但是,如果您更愿意使用 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 线程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多