我尝试实现像WhatsApp这样的录制/播放应用功能,如果我加载现有音符,我会在创建新音符或播放侦听器时加载录音监听器。 录音没有问题,我无法播放曲目。我在片段的OnCreateView中设置了监听器。
audioSendButton.setOnClickListener(new playerListener(path,audioSendButton,context));
(我编辑了很多代码,对不起,如果我写了一些愚蠢的东西)
/**
* Created by Marco on 19/07/2016.
*/
public class playerListener extends AppCompatActivity implements View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
private MusicService musicSrv;
private Intent playIntent;
private boolean musicBound=false;
private String path;
private View playButton;
private boolean playing;
private MediaPlayer player;
private Uri trackUri;
private Context context;
public playerListener(String path, View pb, Context c){
this.path = path;
this.playButton = pb;
this.context = c;
this.path = path;
playing = false;
player = new MediaPlayer();
initMusicPlayer();
}
public void initMusicPlayer(){
player.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Log.i("music_service","created");
}
public void playSong(){
player.reset();
try{
player.setDataSource(path);
player.prepareAsync();
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
}
@Override
public void onClick(View view) {
if(!playing){
playSong();
//Toast.makeText(this,"clicked",Toast.LENGTH_SHORT).show();
Log.i("pressed","true");
playing = true;
}
else{
if(player!=null) {
player.stop();
player.reset();
player.release();
playing = false;
//player=null;
}
}
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
//player = null;
playing = false;
}
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
Log.i("Error","found");
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
//player = null;
playing = false;
return false;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i("mediaplayer","preparato");
player.start();
}
}
这是错误日志,路径正确,它是绝对路径(/storage/emulated/0/Nota-2016.07.20 17:57:23.mp4)
07-20 19:53:15.071 30520-30532/com.marcocreation.****** E/MediaPlayer: error (1, -2147483648)
07-20 19:53:15.071 30520-30520/com.marcocreation.****** E/MediaPlayer: Error (1,-2147483648)
07-20 19:53:15.071 30520-30520/com.marcocreation.****** I/Error: found
07-20 19:53:15.072 30520-30520/com.marcocreation.****** E/MediaPlayer: stop called in state 0
07-20 19:53:15.072 30520-30520/com.marcocreation.****** E/MediaPlayer: error (-38, 0)
好吧,我在华为P8 Lite上尝试了我的应用程序,所以使用SD卡,它运行得很好,所以问题是我的nexus 5没有sdcards(或者编码/解码系统不好?)。有什么想法解决它?