示例
这是演示登录应用程序的示例。它创建一个基本的应用程序,仅给您三种尝试登录应用程序的机会。 要试验该示例,您可以在实际设备或仿真器上运行它。
- 您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。
- 修改src/MainActivity.java文件以添加MediaPlayer代码。
- 修改res/layout/activity_main以添加相应的XML组件
- 在MediaPlayer下创建一个名称为raw的新文件夹,并在其中放置一个名称为.mp3的mp3音乐文件。
- 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。
以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。
package com.jc2182.demo;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity {
private Button b1,b2,b3,b4;
private ImageView iv;
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();;
private int forwardTime = 5000;
private int backwardTime = 5000;
private SeekBar seekbar;
private TextView tx1,tx2,tx3;
public static int oneTimeOnly = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
iv = (ImageView)findViewById(R.id.imageView);
tx1 = (TextView)findViewById(R.id.textView2);
tx2 = (TextView)findViewById(R.id.textView3);
tx3 = (TextView)findViewById(R.id.textView4);
tx3.setText("demo.mp3");
mediaPlayer = MediaPlayer.create(this, R.raw.demo);
seekbar = (SeekBar)findViewById(R.id.seekBar);
seekbar.setClickable(false);
b2.setEnabled(false);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "播放音频",Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
tx2.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) finalTime)))
);
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
b2.setEnabled(true);
b3.setEnabled(false);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "暂停音频",Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
b2.setEnabled(false);
b3.setEnabled(true);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int)startTime;
if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"您向前跳了5秒",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"无法向前跳5秒",Toast.LENGTH_SHORT).show();
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int)startTime;
if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"您向后跳了5秒",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"无法向后跳5秒",Toast.LENGTH_SHORT).show();
}
}
});
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS. toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
}
以下是res/layout/activity_main.xml文件的内容-
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:text="音频播放"
android:textSize="35dp" />
<TextView
android:id="@+id/textView"
android:layout_width="261dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:text="蝴蝶教程"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="129dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#22112200"
android:src="@drawable/logo" />
<Button
android:id="@+id/button"
android:layout_width="51dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="@string/forward" />
<Button
android:id="@+id/button2"
android:layout_width="59dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView"
android:layout_alignParentBottom="true"
android:layout_marginLeft="39dp"
android:layout_marginBottom="4dp"
android:text="@string/pause" />
<Button
android:id="@+id/button3"
android:layout_width="117dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_marginStart="73dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="3dp"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="@string/back" />
<Button
android:id="@+id/button4"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button3"
android:layout_alignParentBottom="true"
android:layout_marginStart="76dp"
android:layout_marginLeft="76dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="@string/rewind" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignStart="@+id/textview"
android:layout_alignLeft="@+id/textview"
android:layout_alignEnd="@+id/textview"
android:layout_alignRight="@+id/textview"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginEnd="-6dp"
android:layout_marginRight="-6dp"
android:layout_marginBottom="120dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="105dp"
android:layout_height="39dp"
android:layout_above="@+id/seekBar"
android:layout_marginEnd="-30dp"
android:layout_marginRight="-30dp"
android:layout_marginBottom="8dp"
android:layout_toStartOf="@+id/textView"
android:layout_toLeftOf="@+id/textView"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="57dp"
android:layout_height="35dp"
android:layout_above="@+id/seekBar"
android:layout_alignEnd="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="120dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
以下是res/layout/activity_main.xml文件的内容-
<resources>
<string name="app_name">Demo</string>
<string name="back"><![CDATA[<]]></string>
<string name="rewind"><![CDATA[<<]]></string>
<string name="forward"><![CDATA[>>]]></string>
<string name="pause">||</string>
</resources>
让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面-
尝试去点击按钮查看效果