在我的应用程序中使用Media Player

时间:2013-08-30 09:20:14

标签: java android eclipse media-player text-to-speech

我正在运行文本到语音代码,但现在我的新要求是提供“暂停”功能。我读了SO问题的数量,发现我必须将数据写入文件,并且必须使用媒体播放器类来播放该文件。以下是我的代码。

public void speak(String text)
    {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);

        tts.synthesizeToFile(text, map,"ttsFile.wav");


        //tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

这里我有两个问题。

  1. 我不想打开媒体播放器播放文件,应该使用媒体播放器控件在应用内播放文件(就像你可以将videoView.setMediaController();设置为VideoView) 。我怎样才能做到这一点?有什么例子吗?

  2. 是否有任何“缓存”位置可以保存上述文件,以便在应用关闭后将其删除?

1 个答案:

答案 0 :(得分:0)

您可以这样做:

在您的应用中创建视频播放器:

VideoPlayerActivity.java:

package com.camera.manual;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;


public class VideoPlayerActivity extends Activity {

    private boolean mResumed = false;
    private boolean mFocused = false;
    private boolean mControlResumed = false;
    private VideoView videoView = null;
    private int stopPosition = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_TransparentVideo);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

        setContentView(R.layout.video_view);

        videoView =(VideoView)findViewById(R.id.myvideoview);
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);        
        Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.slow);        
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);        
        videoView.requestFocus();
        videoView.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        mResumed = false;
        if (mControlResumed) {
            if (null != videoView)
                videoView.pause();
            stopPosition = videoView.getCurrentPosition();
            mControlResumed = false;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        mResumed = true;
        if (mFocused && mResumed && !mControlResumed) {
            if(null != videoView) {
                //videoView.resume();
                videoView.seekTo(stopPosition);
                videoView.start();
            }
            mControlResumed = true;
        }
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mFocused = hasFocus;
        if (mFocused && mResumed && !mControlResumed) {
            if (null != videoView) {
                //videoView.resume();
                videoView.seekTo(stopPosition);
                videoView.start();
            }
            mControlResumed = true;
        }
    }
}   

您可以这样称呼它:

Intent intent = new Intent();
    intent.setClass(mContext, VideoPlayerActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mContext.startActivity(intent);