Minggu, 03 Mei 2015

Materi Pertemuan 12 (Multimedia 1)


Speech To Text
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Speech To Text English" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Speak" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>

Java
 import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EnglishActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_english);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                try {
                    startActivityForResult(intent, 1);

                    EditText editText1 = (EditText) findViewById(R.id.editText1);
                    editText1.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "Device Not Supported", Toast.LENGTH_SHORT);
                    t.show();
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case 1: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                EditText editText1 = (EditText) findViewById(R.id.editText1);
                editText1.setText(text.get(0));
            }
            break;
        }

        }
    }
}



Session
Create
 import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SessionActivity extends Activity {
    SharedPreferences sharedPreferences;
    final static String SHARED_NAME_STRING="sharedp";
    final static String NAME_STRING="user";
    EditText editText;
    Button vOK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session);
        editText = (EditText)findViewById(R.id.nama);
        sharedPreferences=getSharedPreferences(SHARED_NAME_STRING, MODE_PRIVATE);
        String nameString = sharedPreferences.getString(NAME_STRING, "");      
              
        vOK = (Button) findViewById(R.id.ok);
        vOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = editText.getText().toString();
                Intent intent = new Intent(SessionActivity.this, SessionGetActivity.class);
                intent.putExtra("user", string);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(NAME_STRING, string);
                editor.commit();
                startActivity(intent);
                finish();
            }
        });
    }

}

Layoutnya
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SessionActivity" >   
   
    <EditText
        android:id="@+id/nama"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="5dp"
        android:hint="Please Enter Your Name"
        android:background="#0fffff"
        android:ems="10" >
    </EditText>

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/nama"
        android:layout_below="@+id/nama"
        android:text="OK" />

</RelativeLayout>


Get 
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SessionGetActivity extends Activity {
    SharedPreferences sharedPreferences;
    TextView userTextView;
    Button vBACK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session_get);       
        userTextView = (TextView)findViewById(R.id.personTextView);
        userTextView.setText("Name: "+getIntent().getStringExtra(SessionActivity.NAME_STRING));
        vBACK = (Button) findViewById(R.id.back);
        vBACK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences = getSharedPreferences(SessionActivity.NAME_STRING, MODE_PRIVATE);
                sharedPreferences.edit().clear().commit();
                Intent intent = new Intent(SessionGetActivity.this, SessionActivity.class);           
                startActivity(intent);       
                finish();
            }
        });
    }

}

Layoutnya
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SessionGetActivity" >

    <TextView
            android:id="@+id/personTextView"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"          
            android:text="Medium Text" />

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/personTextView"
        android:layout_below="@+id/personTextView"
        android:text="BACK" />

</RelativeLayout>

MP3
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4a4a4a">
   
    <!-- Player Header -->
   
    <!-- Song Thumbnail Image -->

 <WebView
     android:id="@+id/my_webview"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_above="@+id/player_footer_bg"
     android:layout_weight="0.84"
     android:background="@drawable/bg" />
   
    <!-- Player Footer -->

    <LinearLayout
        android:id="@+id/player_footer_bg"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"       
        android:gravity="center" >
       
        <!-- Player Buttons -->

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"           
            android:paddingLeft="10dp"
            android:paddingRight="10dp">
            <!-- Previous Button -->
            <!-- Backward Button -->
            <!-- Play Button -->

            <ImageButton
                android:id="@+id/btnPlay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@null"
                android:src="@drawable/btn_play" />

            <!-- Forward Button -->
            <!-- Next Button -->
        </LinearLayout>
    </LinearLayout>
   
    <!-- Progress Bar/Seek bar -->
   
    <!-- Timer Display -->
   
    <!-- Repeat / Shuffle buttons -->

    <ImageView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="131dp"
        android:src="@drawable/a" />

</RelativeLayout>

Java
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Mp3Activity extends Activity implements AnimationListener {
    private MediaPlayer mp;   
    private ImageButton btnPlay;
    String content;
    Animation animFadein;
    Animation animFadeOut;
    private ImageView    tes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mp3);
        btnPlay = (ImageButton) findViewById(R.id.btnPlay);
        tes = (ImageView) findViewById(R.id.textView1);
        content = "file:///android_asset/aku.html";
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setTitle("Aku Suka Caramu");
        mp = MediaPlayer.create(this, R.raw.akusukacaramu);
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
        animFadein.setAnimationListener(this);
        animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);       
        animFadeOut.setAnimationListener(this);
        final WebView wv;
        wv = (WebView) findViewById(R.id.my_webview);
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        btnPlay.setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View arg0) {               
                if(mp.isPlaying()){
                    if(mp!=null){
                        mp.pause();
                        wv.startAnimation(animFadeOut);
                        tes.startAnimation(animFadein);
                        // Changing button image to play button
                        btnPlay.setImageResource(R.drawable.btn_play);
                    }
                }else{
                    // Resume song
                    if(mp!=null){
                        mp.start();
                        wv.loadUrl(content);
                        wv.startAnimation(animFadein);
                        tes.startAnimation(animFadeOut);
                        // Changing button image to pause button
                        btnPlay.setImageResource(R.drawable.btn_pause);
                    }
                }
               
            }
        });
    }

    public void onBackPressed() {
        mp.stop();
        finish();
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    }

}
File Pendukung
Buat Folder bernama 'anim'
Berikut file 'fade_in.xml'
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

Berikut file 'fade_out.xml'
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>

Simpan file 'aku.html'  pada folder assets
Download di sini

Simpan file 'akusukacaramu.mpe' pada folder raw
Download di sini


Button Play
Download di sini
Button Pause
di sini
Image pause
di sini
Image pause pressed
di sini
Image play
di sini
Image play press
di sini


Text To Speech
Layout
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="15dip"
        android:text="Text To Speech"
        android:textColor="#0587d9"
        android:textSize="26dip"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/txtText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:layout_marginTop="20dip"
        android:hint="Enter some text to speak" />

    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="Speak Out" />

</LinearLayout>

Java
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TTSActivity extends Activity implements OnInitListener {
    /** Called when the activity is first created. */
    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tts);
        tts = new TextToSpeech(this, this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        txtText = (EditText) findViewById(R.id.txtText);
        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                speakOut();
            }
        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);
            // tts.setPitch(5); // set pitch level
            // tts.setSpeechRate(2); // set speech speed rate
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    }

    private void speakOut() {
        // TODO Auto-generated method stub
        String text = txtText.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

Tidak ada komentar:

Posting Komentar