2 mengmengyu mengmengyu_ 于 2015.06.16 16:26 提问

带字幕的音乐播放器返回时崩溃

希望能够实现按下返回键可以返回上一个界面,并且停止当前音频字幕播放,还有就是跳过欢迎界面,直接退出程序

 package com.example.jindutiao.audio1;


import java.io.IOException; 

import com.com.demokejian_1.ContentActivity;
import com.com.demokejian_1.R;
import com.example.jindutiao.audio1.*;

import android.app.Activity; 
import android.content.Intent;
import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.Handler; 
import android.view.KeyEvent;
import android.view.View; 
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
public class TextAActivity extends Activity { 
/** Called when the activity is first created. */ 
private LyricView lyricView; 
private MediaPlayer mediaPlayer; 
private Button button; 
private SeekBar seekBar; 
private String mp3Path; 
private int INTERVAL=15;//歌词每行的间隔 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏

setContentView(R.layout.main); 
mp3Path ="/sdcard/texta.mp3"; 

    lyricView = (LyricView) findViewById(R.id.mylrc); 
    mediaPlayer = new MediaPlayer(); 
    ResetMusic(mp3Path); 
    SerchLrc(); 
    lyricView.SetTextSize(); 
    button = (Button) findViewById(R.id.button); 
    button.setText("播放"); 
    seekBar = (SeekBar) findViewById(R.id.seekbarmusic); 
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 

        } 
        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 

        } 
        @Override 
        public void onProgressChanged(SeekBar seekBar, int progress, 
                boolean fromUser) { 
            if (fromUser) { 
                mediaPlayer.seekTo(progress); 
                lyricView.setOffsetY(220 - lyricView.SelectIndex(progress) 
                        * (lyricView.getSIZEWORD() + INTERVAL-1)); 
            } 
        } 
    }); 
    button.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
            if (mediaPlayer.isPlaying()) { 
                button.setText("播放"); 
                mediaPlayer.pause(); 
            } else { 
                button.setText("暂停"); 
                mediaPlayer.start(); 
                lyricView.setOffsetY(220 - lyricView.SelectIndex(mediaPlayer.getCurrentPosition()) 
                        * (lyricView.getSIZEWORD() + INTERVAL-1)); 
            } 
        } 
    }); 
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
        @Override 
        public void onCompletion(MediaPlayer mp) { 
            ResetMusic(mp3Path); 
            lyricView.SetTextSize(); 
            lyricView.setOffsetY(200); 
            mediaPlayer.start(); 
        } 
    }); 
    seekBar.setMax(mediaPlayer.getDuration()); 
    new Thread(new runable()).start(); 
    } 
    public void SerchLrc() { 
        String lrc = mp3Path; 
        lrc = lrc.substring(0, lrc.length() - 4).trim() + ".lrc".trim(); 
        LyricView.read(lrc); 
        lyricView.SetTextSize(); 
        lyricView.setOffsetY(350); 
    } 
    public void ResetMusic(String path) { 
        mediaPlayer.reset(); 
        try { 
            mediaPlayer.setDataSource(mp3Path); 
            mediaPlayer.prepare(); 
        } catch (IllegalArgumentException e) { 
            e.printStackTrace(); 
        } catch (IllegalStateException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
    class runable implements Runnable { 
        @Override 
        public void run() { 
            while (true) { 
                try { 
                    Thread.sleep(100); 
                    if (mediaPlayer.isPlaying()) { 
                        lyricView.setOffsetY(lyricView.getOffsetY() - lyricView.SpeedLrc()); 
                        lyricView.SelectIndex(mediaPlayer.getCurrentPosition()); 
                        seekBar.setProgress(mediaPlayer.getCurrentPosition()); 
                        mHandler.post(mUpdateResults); 
                    } 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
    } 
    Handler mHandler = new Handler(); 
    Runnable mUpdateResults = new Runnable() { 
        public void run() { 
            lyricView.invalidate(); // 更新视图 
        } 
    }; 
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK||keyCode == KeyEvent.KEYCODE_HOME) {
            if(mediaPlayer != null){
                mediaPlayer.stop();
                mediaPlayer.release();

            }
            Intent intent1 = new Intent(this, ContentActivity.class);
            startActivity(intent1);
        }
        return super.onKeyDown(keyCode, event);
    }
} 

图片说明

2个回答

hikyson
hikyson   2015.06.19 11:41
已采纳

日志说了你的mediaplayer正在播放了,你把这段代码执行的地方把它先停掉吧。

wenpinglaoyao
wenpinglaoyao   2015.06.17 12:27

很抱歉本人能力有限,帮不了问主!但我可以给您一个建议,如果您的C币富裕的话,追加两个悬赏吧,这一个币确实有点少,无法吸引大牛费心回答

mengmengyu_
mengmengyu_ 好的,谢谢,其实我也不富裕,这个问题我用其他方式避开了,有兴趣可以再说
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片