Android的媒体录制使用线程 [英] Android Media Recording using threads

查看:149
本文介绍了Android的媒体录制使用线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,简单地启动和停止录制使用按钮。 我使用的线程。我创建了三个班。其中,开始录制..之一停止记录和主类。

现在的问题是,我可以看到该文件在我的手机,但它是空的,手机给我一个消息无法播放视频..我想它的线程工作..我不想其他方法。

这是我的code 主类:

 公共类MediaRecorderSampleActivity延伸活动{


    按钮启动;
    按钮停止;
    私人MediaRecorder记录;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        启动=(按钮)findViewById(R.id.startbtn);
        停止=(按钮)findViewById(R.id.stopbtn);
        start.setOnClickListener(新btnClick());
        stop.setOnClickListener(新StopbtnClick());
        }



    类btnClick实现View.OnClickListener {
                公共无效的onClick(查看为arg0){
            Log.i(媒体,启动单击的......);
            螺纹startThread =新主题(新startRe(录像机));
            Log.i(媒体,启动线程创建);
            startThread.start();
             Log.i(媒体,开始录制);

                }
    }


    类StopbtnClick实现View.OnClickListener {
        公共无效的onClick(查看为arg0){
              Log.i(媒体,停止被点击的......);
        // TODO自动生成方法存根
            螺纹stopThread =新主题(新STO precording(录像机));
              Log.i(媒体,停止线程创建);
    stopThread.start();
    Log.i(媒体,停止录制);
        }

  }

      }
 

中的startRecording类

 公共类startRe实现Runnable {
私人MediaRecorder记录;


startRe(MediaRecorder R){
    Log.i(媒体,开始利弊);
    this.recorder = R;

}
公共无效的run(){
    // TODO自动生成方法存根
    Log.i(媒体,,在运行开始录制);
    的StartRecording();
}


公共无效的startRecording(){
    Log.i(媒体,在方法开始录音);
    记录=新MediaRecorder();
    Log.i(媒体,创变);
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Log.i(媒体,1);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    Log.i(媒体,2);
    recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
    Log.i(媒体,3);
    recorder.setOutputFile(getFilePath());
    尝试{
        Log.i(媒体,prePAR);
        。录音机prepare();
        Log.i(媒体,前);
        recorder.start();
        Log.i(媒体,后);
    }赶上(例外五){
        e.printStackTrace();
    }

}


私人字符串getFilePath(){
    字符串文件路径= Environment.getExternalStorageDirectory()getPath()。
    档案文件=新的文件(文件路径,MediaRecorderSample);

    如果(!file.exists())
        file.mkdirs();

    返程(file.getAbsolutePath()+/+ System.currentTimeMillis的()+.MP4);
}


}
 

在stopClass

 公共类STO precording实现Runnable {
私人MediaRecorder记录;

公共STO precording(MediaRecorder recorder2){
    Log.i(媒体,停止在COS);
    // TODO自动生成构造函数存根
    尝试 {
    this.recorder = recorder2; }
    赶上(例外五)
    {
        Log.i(媒体,止损出局的Cos+ e.getMessage());
        }

}
公共无效的run(){
    Log.i(媒体,停止运行);
    STO precording();
    Log.i(媒体,停止了运行的);

}
 

解决方案

有一个在你如何使用 MediaRecorder 的对象问题。您需要创建在活动类的对象,然后将对象传递给两个的Runnable ..

所以,你需要做以下更改:

创建的Activity类对象如下code:

 私人MediaRecorder记录;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        启动=(按钮)findViewById(R.id.startbtn);
        停止=(按钮)findViewById(R.id.stopbtn);
        start.setOnClickListener(新btnClick());
        stop.setOnClickListener(新StopbtnClick());
         //在活动创建的对象,以使双方对同一对象的Runnable作品?
         记录=新MediaRecorder();
        }
 

传递相同的对象既的Runnable 类的,你已经做了。

不要建立在的startRecording()方法的对象,因为它会创建一个本地对象,并将其分配到不能被访问的局部变量的 STO precording 的Runnable ..

 公共无效的startRecording(){
    Log.i(媒体,在方法开始录音);
    //评论这台录音机=新MediaRecorder();
    Log.i(媒体,创变);
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Log.i(媒体,1);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    Log.i(媒体,2);
    recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
    Log.i(媒体,3);
    recorder.setOutputFile(getFilePath());
    尝试{
        Log.i(媒体,prePAR);
        。录音机prepare();
        Log.i(媒体,前);
        recorder.start();
        Log.i(媒体,后);
    }赶上(例外五){
        e.printStackTrace();
    }

}
 

尝试,让我们知道结果......

I am developing an android application that simply start and stop recording using Buttons. I used threads. I created three classes.. One to start recording.. one to stop recording and the main class..

The problem is that I can see the file in my mobile but it is empty and the mobile give me a msg "Unable to play video" .. I want it to work with the threads.. I dont want other methods..

This is my code The main class:

public class MediaRecorderSampleActivity extends Activity {


    Button start;
    Button stop ; 
    private MediaRecorder recorder ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button)findViewById(R.id.startbtn); 
        stop = (Button)findViewById(R.id.stopbtn); 
        start.setOnClickListener(new btnClick());
        stop.setOnClickListener(new StopbtnClick());
        }



    class btnClick implements View.OnClickListener {
                public void onClick(View arg0) {
            Log.i("Media", "Start Clicked...");
            Thread startThread = new Thread ( new startRe (recorder));
            Log.i("Media", "start Thread Created");
            startThread.start() ; 
             Log.i("Media", "start Recording");

                }           
    }


    class StopbtnClick implements View.OnClickListener {
        public void onClick(View arg0) {
              Log.i("Media", "Stop Clicked...");
        // TODO Auto-generated method stub
            Thread stopThread = new Thread ( new stopRecording (recorder));
              Log.i("Media", "stop Thread Created");
    stopThread.start();
    Log.i("Media", "stop Recording");
        }

  }

      }

The StartRecording class

public class startRe implements Runnable {
private MediaRecorder recorder;


startRe( MediaRecorder r ) {
    Log.i("Media", "start cons");
    this.recorder = r ; 

}
public void run() {
    // TODO Auto-generated method stub
    Log.i("Media", "IN RUN start Recording");
    startRecording();
}


public void startRecording() {
    Log.i("Media", "IN Method start Recording");
    recorder = new MediaRecorder();
    Log.i("Media", "create variable");
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Log.i("Media", "1");
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    Log.i("Media", "2");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    Log.i("Media", "3");
    recorder.setOutputFile(getFilePath());
    try{
        Log.i("Media", "prepar");
        recorder.prepare();
        Log.i("Media", "before");
        recorder.start();
        Log.i("Media", "after");
    }catch (Exception e){
        e.printStackTrace();
    }

}


private String getFilePath() {
    String filePath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filePath, "MediaRecorderSample");

    if(!file.exists())
        file.mkdirs();

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp4" );
}


}

The stopClass

 public class stopRecording implements Runnable {
private MediaRecorder recorder ;

public stopRecording(MediaRecorder recorder2) {
    Log.i("Media", "Stop in Cos");
    // TODO Auto-generated constructor stub
    try {
    this.recorder = recorder2 ; }
    catch ( Exception e )
    {       
        Log.i("Media", "Stop out  Cos" + e.getMessage()) ;
        } 

}
public void run() {
    Log.i("Media", "Stop in RUN");
    stopRecording();
    Log.i("Media", "Stop out of RUN");

}

解决方案

There is a issue in how you are using the object of the MediaRecorder. You need to create the object in the Activity class and then pass the object to two Runnable..

So you need to make following changes:

Create the object in the Activity class as following code:

 private MediaRecorder recorder ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button)findViewById(R.id.startbtn); 
        stop = (Button)findViewById(R.id.stopbtn); 
        start.setOnClickListener(new btnClick());
        stop.setOnClickListener(new StopbtnClick());
         // Create the object in Activity so that both Runnable works on the same object...
         recorder = new MediaRecorder();
        }

pass the same object to both of the Runnable class as you already doing.

Don't create the object in the startRecording() method since it would create a local object and assign it to a local variable that would not be accessed from the stopRecording Runnable..

public void startRecording() {
    Log.i("Media", "IN Method start Recording");
    // comment this recorder = new MediaRecorder();
    Log.i("Media", "create variable");
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Log.i("Media", "1");
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    Log.i("Media", "2");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    Log.i("Media", "3");
    recorder.setOutputFile(getFilePath());
    try{
        Log.i("Media", "prepar");
        recorder.prepare();
        Log.i("Media", "before");
        recorder.start();
        Log.i("Media", "after");
    }catch (Exception e){
        e.printStackTrace();
    }

}

try and let us know the result...

这篇关于Android的媒体录制使用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆