生活應用: 2月 2020

2020年2月20日 星期四

聲音

  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/textView1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_marginTop="30dp"  
  17.         android:text="Audio Controller" />  
  18.   
  19.     <Button  
  20.         android:id="@+id/button1"  
  21.         style="?android:attr/buttonStyleSmall"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_alignLeft="@+id/textView1"  
  25.         android:layout_below="@+id/textView1"  
  26.         android:layout_marginTop="48dp"  
  27.         android:text="start" />  
  28.   
  29.     <Button  
  30.         android:id="@+id/button2"  
  31.         style="?android:attr/buttonStyleSmall"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_alignTop="@+id/button1"  
  35.         android:layout_toRightOf="@+id/button1"  
  36.         android:text="pause" />  
  37.   
  38.     <Button  
  39.         android:id="@+id/button3"  
  40.         style="?android:attr/buttonStyleSmall"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_alignTop="@+id/button2"  
  44.         android:layout_toRightOf="@+id/button2"  
  45.         android:text="stop" />  
  46.   
  47. </RelativeLayout>  
  48. package com.example.audioplay;  
  49.   
  50. import android.media.MediaPlayer;  
  51. import android.os.Bundle;  
  52. import android.os.Environment;  
  53. import android.app.Activity;  
  54. import android.view.Menu;  
  55. import android.view.View;  
  56. import android.view.View.OnClickListener;  
  57. import android.widget.Button;  
  58.   
  59. public class MainActivity extends Activity {  
  60.     Button start,pause,stop;  
  61.     @Override  
  62.     protected void onCreate(Bundle savedInstanceState) {  
  63.         super.onCreate(savedInstanceState);  
  64.         setContentView(R.layout.activity_main);  
  65.           
  66.         start=(Button)findViewById(R.id.button1);  
  67.         pause=(Button)findViewById(R.id.button2);  
  68.         stop=(Button)findViewById(R.id.button3);  
  69.         //creating media player  
  70.         final MediaPlayer mp=new MediaPlayer();  
  71.         try{  
  72.                 //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3  
  73.         mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Music/maine.mp3");  
  74.           
  75.         mp.prepare();  
  76.         }catch(Exception e){e.printStackTrace();}  
  77.           
  78.         start.setOnClickListener(new OnClickListener() {  
  79.             @Override  
  80.             public void onClick(View v) {  
  81.                 mp.start();  
  82.             }  
  83.         });  
  84.         pause.setOnClickListener(new OnClickListener() {  
  85.             @Override  
  86.             public void onClick(View v) {  
  87.                 mp.pause();  
  88.             }  
  89.         });  
  90.         stop.setOnClickListener(new OnClickListener() {  
  91.             @Override  
  92.             public void onClick(View v) {  
  93.                 mp.stop();  
  94.             }  
  95.         });  
  96.     }  
  97. }  
android audio control example output 1

// button20 = (Button)findViewById(R.id.button20);
final MediaPlayer mp=MediaPlayer.create(this, R.raw.hello);//

//button20.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View View) {
        mp.start();
    }
});  //
///////////////////////////////////////


//////////////////////////////////////////////////

式碼中的extends和implements讓我感到很迷惑,現在終於弄明白它們之間的區別和用法了。

[c-sharp] view plain copy

//定義一個Runner介面   
public inerface Runner   
{  
   int ID = 1;  
   void run ();  
}   

[java] view plain copy

//定義一個介面Animal,它繼承於父類介面Runner  
interface Animal extends Runner  
{  
   void breathe ();  
}  

[c-sharp] view plain copy

//定義Fish類,它實現了Animal介面的方法run()和breather()  
class Fish implements Animal  
{  
   public void run ()    //實現了Animal方法run()  
 {  
    System.out.println(“fish is swimming”);  
 }  
public void breather()  //實現Animal的breather()方法
 {  
    System.out.println(“fish is bubbing”);     
 }  
}  
 
//定義了一個抽象類LandAnimal,它實現了介面Animal的方法。  
abstract LandAnimal implements Animal  
{  
    
   public void breather ()  
 {  
    System.out.println(“LandAnimal is breathing”);  
 }  
}  
//定義了一個類Student,它繼承了類Person,並實現了Runner介面的方法run()。  
class Student extends Person implements Runner  
{  
    ……  
    public void run ()  
     {  
          System.out.println(“the student is running”);  
     }  
    ……  
}  
   
//定義了一個介面Flyer  
interface Flyer  
{  
   void fly ();  
}  
   
//定義了一個類Bird,它實現了Runner和Flyer這兩個介面定義的方法。  
class Bird implements Runner , Flyer  
{  
   public void run ()   //Runner介面定義的方法run()。  
    {  
        System.out.println(“the bird is running”);  
    }  
   public void fly ()   //Flyer介面定義的方法fly()。  
    {  
        System.out.println(“the bird is flying”);  
    }  
}  
   
//TestFish類  
class TestFish  
{  
   public static void main (String args[])  
    {  
       Fish f = new Fish();  
       int j = 0;  
       j = Runner.ID;  
       j = f.ID;  
    }