Android 静音一个音频并在其位置播放其他音频(VideoView) 包z.x; 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.IOException; 导入java.io.RandomAcces
静音一个音频并在其位置播放其他音频(VideoView)Android 静音一个音频并在其位置播放其他音频(VideoView) 包z.x; 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.IOException; 导入java.io.RandomAcces,android,media-player,android-videoview,android-audiomanager,Android,Media Player,Android Videoview,Android Audiomanager,静音一个音频并在其位置播放其他音频(VideoView) 包z.x; 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.IOException; 导入java.io.RandomAccessFile; 导入android.app.Activity; 导入android.content.Context; 导入android.media.AudioManager; 导入android.media.MediaPlayer; 导入and
包z.x;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入android.app.Activity;
导入android.content.Context;
导入android.media.AudioManager;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.MediaController;
导入android.widget.TextView;
导入android.widget.VideoView;
公共类AsdqweActivity扩展活动{
/**在首次创建活动时调用*/
视频视图我的视频视图;
文本视图翻译;
字符串SrcPath=“/sdcard/bunny.MP4”,txtdisplay=“”;
螺纹1、螺纹2;
按钮标签;
int count=0,tstart=-1,tend=0;
音频经理am;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myVideoView=(VideoView)findViewById(R.id.newVideoView);
translation=(TextView)findViewById(R.id.translation);
MediaController MediaController=新的MediaController(此);
mediaController.setAnchorView(myVideoView);
myVideoView.setMediaController(mediaController);
myVideoView.setKeepScreenOn(true);
Runnable Runnable=新的视频播放器();
Thread2=新线程(可运行);
Thread2.start();
runnable=new countdownlunner();
Thread1=新线程(可运行);
Thread1.start();
runnable=新的AudioPlayer();
Thread1=新线程(可运行);
Thread1.start();
am=
标记=(按钮)findViewById(R.id.start);
tag.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
//如何更改按钮上的文本????????
如果(计数%2==0)
{
tstart=myVideoView.getCurrentPosition();
System.out.println(“标记从这里开始:+tstart”);
myVideoView.pause();
倾向=t开始+1000;
计数++;
if((tstart+1000)>myVideoView.getDuration())
{
tend=myVideoView.getDuration();
计数++;
}
}
其他的
{
tend=myVideoView.getCurrentPosition();
如果(趋势>=(tstart+1000)){
System.out.println(“标记在此处结束:+tend”);
计数++;
myVideoView.pause();
}
其他的
{
System.out.println(“无效标记”);
}
tstart=-1;
}
}
});
myVideoView.setOnCompletionListener(新的MediaPlayer.OnCompletionListener()
{
完成时的公共作废(MediaPlayer mp)
{
Log.v(“日志标签”,“完成时”);
试一试{
Thread1.join();
螺纹2.连接();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//r、 止动螺纹();
完成();
}
});
}
类VideoPlayer实现可运行{
公开募捐
{
myVideoView.requestFocus();
设置视频路径(SrcPath);
myVideoView.start();
}
}
类AudioPlayer实现Runnable{
公共音频播放器(){
//TODO自动生成的构造函数存根
}
公开募捐
{
/*myVideoView.setsetvolume(0,0);
requestFocus();
setVideoPath(SrcPathaudio);
audio.start()*/
}
}
类CountDownRunner实现可运行
{
File File=新文件(“/sdcard/harsh.srt”);
长r=0;
随机存取文件rand;
公共倒计时{
//TODO自动生成的构造函数存根
如果(!file.exists())
{
System.out.println(“文件不存在”);
系统出口(0);
}
试一试{
兰德=新的随机存取文件(文件“r”);
}捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
translation=(TextView)findViewById(R.id.translation);
}
公开募捐
{
//获取DataInputStream的对象
而(!Thread.currentThread().isInterrupted())
{
尝试
{
销钉();
睡眠(100);
}捕捉(中断异常e)
{
Thread.currentThread().interrupt();
e、 printStackTrace();
}捕获(例外e)
{
e、 printStackTrace();
}
}
closeRandFile();
}
公共void closeRandFile(){
试一试{
rand.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共工作{
runOnUiThread(新的Runnable(){
公开募捐{
试一试{
兰德·赛克(r);
//如果返回毫秒,则除以1000
int playTime=myVideoView.getCurrentPosition();
长t1=0,t2=0;
int i=0,j=0;
package z.x;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
public class AsdqweActivity extends Activity {
/** Called when the activity is first created. */
VideoView myVideoView;
TextView translation;
String SrcPath = "/sdcard/bunny.MP4",txtdisplay="";
Thread Thread1,Thread2;
Button tag;
int count=0,tstart=-1,tend=0;
AudioManager am;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myVideoView = (VideoView) findViewById(R.id.newVideoView);
translation= (TextView) findViewById(R.id.translation);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
myVideoView.setMediaController(mediaController);
myVideoView.setKeepScreenOn(true);
Runnable runnable = new VideoPlayer();
Thread2= new Thread(runnable);
Thread2.start();
runnable = new CountDownRunner();
Thread1= new Thread(runnable);
Thread1.start();
runnable = new AudioPlayer();
Thread1= new Thread(runnable);
Thread1.start();
am =
tag = (Button) findViewById(R.id.start);
tag.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//how to change the text on the button????????
if(count%2==0)
{
tstart=myVideoView.getCurrentPosition();
System.out.println("tag starts here: "+ tstart);
myVideoView.pause();
tend=tstart+1000;
count++;
if((tstart+1000)>myVideoView.getDuration())
{
tend=myVideoView.getDuration();
count++;
}
}
else
{
tend = myVideoView.getCurrentPosition();
if(tend>=(tstart+1000)){
System.out.println("tag ends here: "+ tend);
count++;
myVideoView.pause();
}
else
{
System.out.println("invalid tagging");
}
tstart=-1;
}
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
Log.v("log_tag", "On Completion");
try {
Thread1.join();
Thread2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//r.stopThread();
finish();
}
});
}
class VideoPlayer implements Runnable{
public void run()
{
myVideoView.requestFocus();
myVideoView.setVideoPath(SrcPath);
myVideoView.start();
}
}
class AudioPlayer implements Runnable{
public AudioPlayer() {
// TODO Auto-generated constructor stub
}
public void run()
{
/*myVideoView.setsetvolume(0,0);
audio.requestFocus();
audio.setVideoPath(SrcPathaudio);
audio.start();*/
}
}
class CountDownRunner implements Runnable
{
File file = new File("/sdcard/harsh.srt");
long r=0;
RandomAccessFile rand;
public CountDownRunner() {
// TODO Auto-generated constructor stub
if(!file.exists())
{
System.out.println("File does not exist.");
System.exit(0);
}
try {
rand = new RandomAccessFile(file,"r");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
translation = (TextView)findViewById(R.id.translation);
}
public void run()
{
// Get the object of DataInputStream
while(!Thread.currentThread().isInterrupted())
{
try
{
doWork();
Thread.sleep(100);
}catch (InterruptedException e)
{
Thread.currentThread().interrupt();
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
}
closeRandFile();
}
public void closeRandFile(){
try {
rand.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doWork(){
runOnUiThread(new Runnable() {
public void run() {
try{
rand.seek(r);
//If it returns milliseconds, divide by 1000
int playTime = myVideoView.getCurrentPosition();
long t1=0,t2=0;
int i=0,j=0;
/* String textValue = "i havent entered it yet";
System.out.println(playTime);
if(playTime<15000){
textValue = "its still not 15000";
System.out.println(textValue);
}else if(playTime>3000)
{textValue = "its more than 15000";}*/
////////////////////////////////////
try{
// Open the file that is the first
// command line parameter
String strLine;
//Read File Line By Line
while(((strLine = rand.readLine()) != null)&&((strLine=="")))
{
System.out.println(strLine+" line 184");
}
if(strLine != null)
{
i = Integer.parseInt(strLine);
System.out.println(strLine+" line 189");
if(i>j){
if((strLine = rand.readLine()) != null)
{
//02:03:24,100 --> 02:03:25,500
//String substring(int startIndex, int endIndex)
int h =Integer.parseInt(strLine.substring(0,2));
int m =Integer.parseInt(strLine.substring(3,5));
int s =Integer.parseInt(strLine.substring(6,8));
int ms=Integer.parseInt(strLine.substring(9,12));
t1 = (((((h*60)+m)*60)+s)*1000)+ms ;
System.out.println("start of this text time "+t1);
int l = strLine.indexOf("-->")+4;
h =Integer.parseInt(strLine.substring(l+0,l+2));
m =Integer.parseInt(strLine.substring(l+3,l+5));
s =Integer.parseInt(strLine.substring(l+6,l+8));
ms=Integer.parseInt(strLine.substring(l+9,l+12));
t2 = (((((h*60)+m)*60)+s)*1000)+ms ;
System.out.println("end of this text time "+t2);
}
if((playTime<=t2)&&(playTime>=t1)){
while (((strLine = rand.readLine()) != null)&&(strLine!="")){
// Print the content on the console
txtdisplay=txtdisplay+strLine;
}
System.out.println(txtdisplay);
r=rand.getFilePointer();
translation.setText(txtdisplay);
}
txtdisplay="";
}
}
//Close the input stream
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
//////////////////////////////////////
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.setOnPreparedListener(PreparedListener);
videoView.requestFocus();
//Dont start your video here
//videoView.start();
}
MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer m) {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};