我在列表视图中有json的歌曲,每行都有一个播放按钮,点击播放按钮我的应用程序冻结,应用程序没有响应对话框来了,有时媒体播放器在冻结后有时会崩溃。因为App无响应。这是我的代码:
viewHolder.playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedPosition = position;
if (selectedPosition != mPlayingPosition) {
try {
mPlayerforplanet.reset();
mPlayerforplanet.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayerforplanet.setDataSource(song_urls);
logger.addRecordToLog("MediaPlayer audio session ID: " + mPlayerforplanet.getAudioSessionId());
logger.addRecordToLog("Media Player started " + "Started !");
mPlayerforplanet.prepare();
mPlayerforplanet.start();
} catch (IOException e) {
e.printStackTrace();
}
// playSongs(position);(i also try with method of playsong but no luck)
Toast.makeText(getContext(), "play song" + mPlayingPosition, Toast.LENGTH_SHORT).show();
}}});
点击播放按钮后记录猫
01-10 07:13:33.501 17284-17293/luck.materialdesign.tabsnavigator I/art: Thread[5,tid=17293,WaitingInMainSignalCatcherLoop,Thread*=0xab8d8600,peer=0x12c000a0,"Signal Catcher"]: reacting to signal 3
01-10 07:13:34.273 17284-17293/luck.materialdesign.tabsnavigator I/art: Wrote stack traces to '/data/anr/traces.txt'
答案 0 :(得分:2)
请使用prepareAsync而不是准备和处理onPrepared并从那里触发启动。
mPlayerforplanet.prepareAsync();
public void onPrepared(MediaPlayer mp) {
mPlayerforplanet.start;
}
示例代码:
package com.example.simplemediaplayer.app;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.IOException;
public class MediaPlayerActivity extends ActionBarActivity {
private static final String TAG = "tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
String url = "http://www.brothershouse.narod.ru/music/pepe_link_-_guitar_vibe_113_club_mix.mp3"; // your URL here
MediaPlayer myMediaPlayer = new MediaPlayer();
myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
myMediaPlayer.setDataSource(url);
myMediaPlayer.prepareAsync(); // might take long! (for buffering, etc)
} catch (IOException e) {
Toast.makeText(this, "mp3 not found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
//mp3 will be started after completion of preparing...
myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
player.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.media_player, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
对于搜索栏使用重新运行:
启动runnable,
public void onPrepared(MediaPlayer player) {
mPlayerforplanet.start();
mUpdateSeekBar.run();
}
可运行的代码
private final Runnable mUpdateSeekBar = new Runnable() {
@Override
public void run() {
int elapsedtime = mPlayerforplanet.getCurrentPosition();
/* update UI with getCurrentPosition*/
mHandler.postDelayed(mUpdateSeekBar, 1000);
}
};