我正在制作一个播放音频文件的android服装应用程序,并在两个屏幕之间翻转。当我点击屏幕时播放声音并出现下一个屏幕。在声音停止之前,我可以做大约十次但是我可以继续按下按钮然后转到下一个屏幕。我已经看过了 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问我。有很多。
答案 0 :(得分:0)
我仍然推荐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线程。