0

嗨,我正在开发一个录制音频的 android 应用程序,我需要该应用程序具有有人按下按钮的功能,并且似乎录制中的当前时间例如说我在上课,我正在录制讲座我稍后回去听讲座,看到有一个地方我需要回来再听一次以这种方式保存在文本视图中,当我稍后回来时,我可以去我需要去的地方,而不必再次收听整个录音唯一的问题是我不知道如何在代码中执行此操作

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/PlayingTimeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="40sp" />

    <SeekBar
        android:id="@+id/MediaPlayerSeek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/StartTimeText"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/StartTimeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/PlayerButtonsLayout"
        android:layout_alignParentLeft="true"
        android:text="00:00:00" />

    <TextView
        android:id="@+id/EndTimeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/PlayerButtonsLayout"
        android:layout_alignParentRight="true" />

    <LinearLayout
        android:id="@+id/PlayerButtonsLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        >

        <ImageButton
            android:id="@+id/BackToStartButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/backtostart_icon"
        />

        <ImageButton
            android:id="@+id/PlayerBackButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/playerback_icon"
        />

        <ImageButton
            android:id="@+id/MediaPlayerButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

        <ImageButton
                        android:id="@+id/FastForwardButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/fastforward_icon"
                />

        </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Go Back Time" />

    <TextView
        android:id="@+id/gobacktime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/MediaPlayerSeek"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/PlayingTimeText"
        android:layout_toLeftOf="@+id/EndTimeText" />

</RelativeLayout>

这是我的Java代码:

package com.d4a.voicerecorder;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.audiofx.Equalizer;
import android.media.audiofx.Visualizer;
import android.media.audiofx.Visualizer.OnDataCaptureListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.dva.RecodeTime.R;

public class MeidaPlayerActivity extends Activity {

    private static final String TAG ="VoiceRecorder_MeidaPlayerActivity";
int progres;

//int val ;
    //private AudioManager m_AudioManager;
    //private int m_nRecordVolume;
    //private int m_nOtherVolume;
    private MediaPlayer m_MediaPlayer = null;
    private SeekBar m_SeekBar;
    private TextView m_NowTimeText , startTime;
    private ImageButton m_MediaPlayerButton;

    private Handler m_Handler;
    private Timer m_TimeCountTimer = null;
    private Timer m_ButtonClickHandleTimer = null;
    private boolean m_Playing = false;

    private VisualizerView m_VisualizerView;
    private Equalizer m_Equalizer;
    private Visualizer m_Visualizer;

    Button button;

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mediaplayerview);

        startTime =(TextView)findViewById(R.id.StartTimeText);
         //val = getVal("k");
            button= (Button) findViewById(R.id.button1);


        //Tools.setRecoedVolume(this);

        /*
        m_AudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        m_nOtherVolume = m_AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(sp.getBoolean(getResources().getString(R.string.VolumeAutoSetKey), false) == true){
            m_nRecordVolume = sp.getInt(getResources().getString(R.string.RecordVolumeKey), m_AudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        }else{
            m_nRecordVolume = m_nOtherVolume;
        }
        */

//      DataBaseHelper base= new DataBaseHelper(MeidaPlayerActivity.this);
//      base.insertImageUrl("dinesh");




            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    DataBaseHelper baseHelper = new DataBaseHelper(MeidaPlayerActivity.this);
                    baseHelper.insertImageUrl(String.valueOf(progres));
                }
            });




        m_MediaPlayer = new MediaPlayer();
        m_Handler = new Handler();
        m_TimeCountTimer = new Timer(true);
    //  dataBase.insertImageUrl("jhg");
        if(SDCardCtrl.checkSDCard(this) == true){
            m_MediaPlayer = MediaPlayer.create(this, Uri.fromFile(new File(getIntent().getStringExtra(getResources().getString(R.string.IntentAudioFilePathKey)))));
            try {
                m_MediaPlayer.prepare();
            } catch (IllegalStateException e) {
                //e.printStackTrace();
            } catch (Exception e) {
            //  e.printStackTrace();
            }
            m_MediaPlayer.setOnCompletionListener(new OnCompletionListener(){
                @Override
                public void onCompletion(MediaPlayer mp) {
                    finish();
                }
            });
            m_MediaPlayer.start();
            m_NowTimeText = (TextView) findViewById(R.id.PlayingTimeText);

            m_SeekBar = (SeekBar) findViewById(R.id.MediaPlayerSeek);
            m_SeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
                    seekBar.setProgress(progress);
                    m_MediaPlayer.seekTo(progress);
                    m_NowTimeText.setText(Tools.ConversionTime(progress));
                    startTime.setText(Tools.ConversionTime(progress));
                    progres = progress;


                    //val
                    //===============================================
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    m_Playing = m_MediaPlayer.isPlaying();
                    if(m_Playing == true){
                        m_MediaPlayer.pause();
                        //m_MediaPlayerButton.setText(R.string.AudioPlayerStartButtonText);
                        m_MediaPlayerButton.setImageResource(R.drawable.audioplay_icon);
                    }
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    if(m_Playing == true){
                        m_MediaPlayer.start();
                        m_Playing = false;
                        m_MediaPlayerButton.setImageResource(R.drawable.audiopause_icon);
                        //m_MediaPlayerButton.setText(R.string.AudioPlayerPauseButtonText);
                    }
                }
            });
            m_SeekBar.setMax(m_MediaPlayer.getDuration());

            TextView EndTimeText = (TextView) findViewById(R.id.EndTimeText);
            EndTimeText.setText(Tools.ConversionTime(m_MediaPlayer.getDuration()));
        }

        ImageButton BackToStartButton = (ImageButton) findViewById(R.id.BackToStartButton);
        BackToStartButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                m_MediaPlayer.seekTo(0);
                m_SeekBar.setProgress(0);
                m_NowTimeText.setText(Tools.ConversionTime(0));
            }
        });

        ImageButton PlayerBackButton = (ImageButton) findViewById(R.id.PlayerBackButton);
        //ボタン�押��時�ボタンを離��時�Handleを�得�る��TouchListener��る
        PlayerBackButton.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    if(m_MediaPlayer.isPlaying() == true){
                        m_MediaPlayer.pause();
                        m_Playing = true;
                    }
                    setControlPlayerTimer(-(m_MediaPlayer.getDuration() / Config.PLAYER_FORWARD_BACK_TIMESPAN));
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    if(m_Playing == true){
                        m_MediaPlayer.start();
                        m_Playing = false;
                    }
                    m_ButtonClickHandleTimer = ReleaseTimer(m_ButtonClickHandleTimer);
                    return true;
                }
                return false;
            }
        });









//playButton-------------------------------------------------------------------------------------------------------------
        m_MediaPlayerButton = (ImageButton) findViewById(R.id.MediaPlayerButton);
        m_MediaPlayerButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(m_MediaPlayer.isPlaying() == true){
                    m_MediaPlayer.pause();
                    m_MediaPlayerButton.setImageResource(R.drawable.audioplay_icon);
                    //SharedPreferences preferences = SharedPreferences.

//                  String ss = String.valueOf(progres);
//                  saveVal(ss);
    //              m_NowTimeText.setText(Tools.ConversionTime(progres));
            //String ss = String.valueOf(progres);

                    ;

                }else{
                    m_MediaPlayer.start();
                    m_MediaPlayerButton.setImageResource(R.drawable.audiopause_icon);
//                  int timeGet = getVal("k");
//                  Log.d(TAG, "time get : "+timeGet);
//                  System.out.println("bbbbb "+timeGet);
//                  m_NowTimeText.setText(Tools.ConversionTime(progres));
                }
            }


        });
        m_MediaPlayerButton.setImageResource(R.drawable.audiopause_icon);
        //m_MediaPlayerButton.setText(R.string.AudioPlayerPauseButtonText);

        ImageButton FastForwardButton = (ImageButton) findViewById(R.id.FastForwardButton);
        //ボタン�押��時�ボタンを離��時�Handleを�得�る��TouchListener��る
        FastForwardButton.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    if(m_MediaPlayer.isPlaying() == true){
                        m_MediaPlayer.pause();
                        m_Playing = true;
                    }
                    setControlPlayerTimer(m_MediaPlayer.getDuration() / Config.PLAYER_FORWARD_BACK_TIMESPAN);
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    if(m_Playing == true){
                        m_MediaPlayer.start();
                        m_Playing = false;
                    }
                    m_ButtonClickHandleTimer = ReleaseTimer(m_ButtonClickHandleTimer);
                    return true;
                }
                return false;
            }
        });
        settingCountTimer();

    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    private Timer ReleaseTimer(Timer timer){
        if(timer != null){
            timer.cancel();
            timer.purge();
        }
        return null;
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------




    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    //イコライザー(Equalizer):音声信��周波数特性を変更�るも�
    private void setUpEqualizer(){
        m_Equalizer = new Equalizer(0, m_MediaPlayer.getAudioSessionId());
        //�れ������一回無効������有効�����れ����
        m_Equalizer.setEnabled(false);

        TextView eqTextView = new TextView(this);
        eqTextView.setText("Equalizer:");

        short bands = m_Equalizer.getNumberOfBands();

    }






    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    /*
    private OnClickListener m_MediaPlayerClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(m_MediaPlayer.isPlaying() == true){
                m_MediaPlayer.pause();
                m_MediaPlayerButton.setImageResource(R.drawable.audioplay_icon);
            }else{
                m_MediaPlayer.start();
                m_MediaPlayerButton.setImageResource(R.drawable.audiopause_icon);
            }
        }
    };
    */

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    /*
    private OnSeekBarChangeListener m_SeekBarListener = new OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
            seekBar.setProgress(progress);
            m_MediaPlayer.seekTo(progress);
            m_NowTimeText.setText(Tools.ConversionTime(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            m_Playing = m_MediaPlayer.isPlaying();
            if(m_Playing == true){
                m_MediaPlayer.pause();
                //m_MediaPlayerButton.setText(R.string.AudioPlayerStartButtonText);
                m_MediaPlayerButton.setImageResource(R.drawable.audioplay_icon);
            }
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if(m_Playing == true){
                m_MediaPlayer.start();
                m_Playing = false;
                m_MediaPlayerButton.setImageResource(R.drawable.audiopause_icon);
                //m_MediaPlayerButton.setText(R.string.AudioPlayerPauseButtonText);
            }
        }
    };
    */

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    //ボタンを押�����場��処�
    private void setControlPlayerTimer(final int nMoveTime){
        if(m_ButtonClickHandleTimer == null){
            m_ButtonClickHandleTimer = new Timer(true);
        }
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                int nSetPosition = m_MediaPlayer.getCurrentPosition() + nMoveTime;
                if(nSetPosition > m_MediaPlayer.getDuration()){
                    nSetPosition = m_MediaPlayer.getDuration();
                }else if(nSetPosition < 0){
                    nSetPosition = 0;
                }
                m_MediaPlayer.seekTo(nSetPosition);
                m_SeekBar.setProgress(nSetPosition);
                final String text = Tools.ConversionTime(nSetPosition);
                m_Handler.post(new Runnable() {
                    @Override
                    public void run() {
                        m_NowTimeText.setText(text);
                    }
                });
            }
        };
        m_ButtonClickHandleTimer.schedule(task, 0, Config.PRESS_BUTTON_HANDLE_TIMESPAN);

    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    private void settingCountTimer(){
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                m_Handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(m_MediaPlayer != null){
                            if(m_MediaPlayer.isPlaying() == true){
                                m_NowTimeText.setText(Tools.ConversionTime(m_MediaPlayer.getCurrentPosition()));
                                m_SeekBar.setProgress(m_MediaPlayer.getCurrentPosition());
                            }
                        }
                    }
                });
            }
        };
        m_TimeCountTimer.schedule(task, 0, 1);
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    /*
    private OnCompletionListener m_AudioLitener = new OnCompletionListener(){
        @Override
        public void onCompletion(MediaPlayer mp) {
            finish();
        }
    };
    */

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
        }else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
            Tools.setVolumeUp(this);
            /*
            m_nRecordVolume++;
            int nMaxVolume = m_AudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            if(nMaxVolume < m_nRecordVolume){
                m_nRecordVolume = nMaxVolume;
            }
            if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getResources().getString(R.string.VolumeAutoSetKey), false) == true){
                Tools.setVolume(this, m_AudioManager, m_nRecordVolume);
            }else{
                m_AudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, m_nRecordVolume, AudioManager.FLAG_SHOW_UI);
            }
            */
            //true:他�KeyEventを�得����よ���る
            return true;
        }else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
            Tools.setVolumeDown(this);
            /*
            m_nRecordVolume--;
            if(0 >= m_nRecordVolume){
                m_nRecordVolume = 0;
                Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                //500ミリ秒�イブレーション起動。
                vibrator.vibrate(Config.VIBRATING_TIME);
            }
            if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getResources().getString(R.string.VolumeAutoSetKey), false) == true){
                Tools.setVolume(this, m_AudioManager, m_nRecordVolume);
            }else{
                m_AudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, m_nRecordVolume, AudioManager.FLAG_SHOW_UI);
            }
            */
            //true:他�KeyEventを�得����よ���る
            return true;
        }
        return false;
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    protected void onStart() {
        super.onStart();
        Tools.setRecoedVolume(this);
        /*
        if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getResources().getString(R.string.VolumeAutoSetKey), false) == true){
            Tools.setVolume(this,m_AudioManager,m_nRecordVolume);
        }
        */
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    protected void onPause() {
        super.onPause();
        //�ックグラウンド�生�����
        if(m_MediaPlayer.isPlaying() == true){
            m_MediaPlayer.pause();
            m_MediaPlayerButton.setImageResource(R.drawable.audioplay_icon);
        }
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    protected void onStop() {
        super.onStop();
        /*
        if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getResources().getString(R.string.VolumeAutoSetKey), false) == true){
            m_AudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, m_nOtherVolume, AudioManager.FLAG_SHOW_UI);
        }
        */
        Tools.setBeforeVolume(this);
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (m_Visualizer != null) {
            m_Visualizer.setEnabled(false);
            m_Visualizer.release();
            m_Visualizer = null;
        }
        if(m_MediaPlayer != null){
            m_ButtonClickHandleTimer = ReleaseTimer(m_ButtonClickHandleTimer);
            m_TimeCountTimer = ReleaseTimer(m_TimeCountTimer);
            if(m_MediaPlayer.isPlaying() == true){
                m_MediaPlayer.stop();
            }
            m_MediaPlayer.release();
            m_MediaPlayer = null;
        }
        Tools.releaseImageView(m_MediaPlayerButton);
        Tools.releaseImageView((ImageButton) findViewById(R.id.BackToStartButton));
        Tools.releaseImageView((ImageButton) findViewById(R.id.PlayerBackButton));
        Tools.releaseImageView((ImageButton) findViewById(R.id.FastForwardButton));
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------------------------
    private void saveVal(String progres2) {
        // TODO Auto-generated method stub
         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MeidaPlayerActivity.this);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putString("k", progres2);
          editor.commit();
          Log.d(TAG, "time saved : "+progres2);
          System.out.println("aaaaa "+progres2);



//  MySession mySession = new MySession();
//  mySession.MySession(MeidaPlayerActivity.this);
//  mySession.saveVal(progres2);


    }

    private String getVal(String key) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
      String name = preferences.getString(key,"");

      return name;

    }
}

任何帮助将非常感激我是一个新的Android用户以及初学者程序员所以请不要判断

提前谢谢方式

说唱11

4

1 回答 1

0

1 - 开始录制时保存系统时钟的时间。

    long beginTime = System.currentTimeMillis();

2 - 保存每次按钮单击的系统时间和新时间之间的差异。

    // set up a SparseArray to store the click times
    SparseArray<long> savedTimes = new SpareArray<long>
    ...
    // set up a counter to keep track of your clicks
    int clickNumber = 0;
    ...

    // in your buttons onClick, add the new click time
    long timeSinceRecordStart = System.currentTimeMillis() - beginTime;        
    savedTimes.put(clickNumber, timeSinceRecordStart)
    clickNumber++;
    ...

3 - 当您想要回放时,使用 MediaPlayer.seekTo(int msec) 方法并将您的已保存时间的相关偏移量传递给它。

于 2013-10-13T00:36:41.267 回答