程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-帶字幕的音樂播放器返回時崩潰

android-帶字幕的音樂播放器返回時崩潰

編輯:編程綜合問答
帶字幕的音樂播放器返回時崩潰

希望能夠實現按下返回鍵可以返回上一個界面,並且停止當前音頻字幕播放,還有就是跳過歡迎界面,直接退出程序

 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);
    }
} 

圖片說明

最佳回答:


日志說了你的mediaplayer正在播放了,你把這段代碼執行的地方把它先停掉吧。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved