Autore Topic: recorder audio video e invio file via email  (Letto 597 volte)

Offline riccardofarris

  • Utente junior
  • **
  • Post: 71
  • Respect: 0
    • Mostra profilo
  • Sistema operativo:
    windows7
recorder audio video e invio file via email
« il: 26 Aprile 2011, 11:55:11 CEST »
0
salve a tutti!!!sono un nuovo utente e sto muovendo i primi passi con android e sto incominciando a trovare un po di difficolta!devo creare un'applicazione che registri sia file audio che video permetta di eseguirli e in ultima istanza vorrei riuscire a inviarli tramite email.
per prima cosa ho fatto un'applicazione che registra ed esegue file audio video, i file audio sono ok mentre quando provo a registrare il video va in crash l'appl. e questo è il primo problema.
separatamente ho provato a fare un'app x inviare email ma va in crash e non so il perchè,qualcuno può aiutarmi!!!ora posto i 2 codici e i logcat relativi!!!grazie a tutti !!!!

audiorecorder:

Codice (Java): [Seleziona]
package com.ricky.workspace;
/*
 * The application needs to have the permission to write to external storage
 * if the output file is written to the external storage, and also the
 * permission to record audio. These permissions must be set in the
 * application's AndroidManifest.xml file, with something like:
 *
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.RECORD_AUDIO" />
 *
 */


import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;

public class audiorecorder extends Activity {
    /** Called when the activity is first created. */
        private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    private static String mFileVideoName = null;

    private RecordButton mRecordButton = null;
    private RecordVideoButton mRecordVideoButton = null;
    private PlayVideoButton   mPlayVideoButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        //File mediaFile = new File(mFileName);
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }
   
   
    //record and play video
   
   
    class RecordVideoButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecordVideo(mStartRecording);
                if (mStartRecording) {
                    setText("Stop Video recording");
                } else {
                    setText("Start Video recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordVideoButton(Context ctx) {
            super(ctx);
            setText("Start Video recording");
            setOnClickListener(clicker);
        }
    }

    class PlayVideoButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlayVideo(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop Video playing");
                } else {
                    setText("Start Video playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayVideoButton(Context ctx) {
            super(ctx);
            setText("Start Video playing");
            setOnClickListener(clicker);
        }
    }
   
    private void onRecordVideo(boolean start) {
        if (start) {
            startRecordingVideo();
        } else {
            stopRecordingVideo();
        }
    }

    private void onPlayVideo(boolean start) {
        if (start) {
            startPlayingVideo();
        } else {
            stopPlayingVideo();
        }
    }

    private void startPlayingVideo() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileVideoName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlayingVideo() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecordingVideo() {
        //File mediaFile = new File(mFileName);
        mRecorder = new MediaRecorder();
        mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);        
        mRecorder.setOutputFile(mFileVideoName);
       

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecordingVideo() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }


    public audiorecorder() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
         
        mFileName += "/audiotestrecorder.3gp";
        mFileVideoName = Environment.getExternalStorageDirectory().getAbsolutePath();
         
        mFileVideoName += "/videotestrecorder.3gp";
       
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        RelativeLayout mainLayout = new RelativeLayout(this);

        TableLayout tableLayout = new TableLayout(this);
       
                TableRow row1 = new TableRow(this);
                TableRow row2 = new TableRow(this);

 
       
        mRecordButton = new RecordButton(this);
       
        row1.addView(mRecordButton);  

        mPlayButton = new PlayButton(this);
        row1.addView(mPlayButton);

        mRecordVideoButton = new RecordVideoButton(this);
        row2.addView(mRecordVideoButton);

            mPlayVideoButton = new PlayVideoButton(this);
            row2.addView(mPlayVideoButton);

            tableLayout.addView(row1);
            tableLayout.addView(row2);
            mainLayout.addView(tableLayout, new RelativeLayout.LayoutParams(
                                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        setContentView(mainLayout);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

questo è il manifesto xml:

Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.ricky.workspace"
     android:versionCode="1"
     android:versionName="1.0">
     
      <ScrollView android:layout_height="fill_parent"
               android:layout_width="fill_parent">

             
               
                <LinearLayout android:orientation="vertical"
                       android:layout_height="fill_parent" android:layout_width="fill_parent">

                       
                       
                </LinearLayout>
        </ScrollView>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".audiorecorder"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
</manifest>

il file per l'invio email è:

Codice (Java): [Seleziona]
package com.ricky.workspace;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import com.ricky.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class provaEmail extends Activity {
    /** Called when the activity is first created. */
       
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);}
       
    public static void sendEmail(Context context, String emailTo, String emailCC,
        String subject, String emailText, List<String> filePaths){
       

    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"riccardofarris@hotmail.com"});
    if(emailCC != null) emailIntent.putExtra(android.content.Intent.EXTRA_CC, new String[]{"ciao"});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "ciao"   );
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "ciao");
   
//    startActivity(Intent.createChooser(emailIntent, "Invia email..."));    

   

   
   //has to be an ArrayList
   ArrayList<Uri> uris = new ArrayList<Uri>();

    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths){
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

    context.startActivity(Intent.createChooser(emailIntent, "Invia email..."));
   
    }
}



« Ultima modifica: 27 Aprile 2011, 09:41:24 CEST da 7h38ugg3r, Reason: ho aggiunto i tag code per il codice »

Offline 7h38ugg3r

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 1200
  • Respect: +133
    • riccardofischetti
    • th38ugg3r
    • @7h38ugg3r
    • Mostra profilo
  • Dispositivo Android:
    Galaxy-S GT I-9000/ ASUS Eee Pad Transformer
  • Play Store ID:
    Riccardo Fischetti
  • Sistema operativo:
    OS X Lion / Linux Mint 11 (Katya)
Re:recorder audio video e invio file via email
« Risposta #1 il: 27 Aprile 2011, 09:41:58 CEST »
0
manca il logcat...
7h38ugg3r<=>thebugger
Non conosci Java? Allora sei nel posto sbagliato!

http://www.androidpatterns.com/