首页 技术 正文
技术 2022年11月17日
0 收藏 737 点赞 2,231 浏览 4338 个字

第一个:用Activity实现

activity_music_play1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.hanqi.test2.MusicPlay1"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放状态:"
android:id="@+id/tv_1"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放"
android:onClick="play_onclick"/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停"
android:onClick="pause_onclick"/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止"
android:onClick="stop_onclick"/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="退出"
android:onClick="exit_onclick"/> </LinearLayout></LinearLayout>

MusicPlay1.java

package com.hanqi.test2;import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;public class MusicPlay1 extends AppCompatActivity {
TextView tv_1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_play1); tv_1 =(TextView)findViewById(R.id.tv_1); tv_1.setText("播放状态:停止播放"); } //媒体播放器的类
private MediaPlayer mediaPlayer; public void play_onclick(View v)
{
if (mediaPlayer == null) {
//调用MediaPlayer的静态方法create()
mediaPlayer = MediaPlayer.create(this, R.raw.love);
}
mediaPlayer.start(); tv_1.setText("播放状态:正在播放..."); }
public void stop_onclick(View v)
{
if (mediaPlayer != null) {
mediaPlayer.stop(); //停止
mediaPlayer.reset(); //重置
mediaPlayer.release(); //释放资源
mediaPlayer = null; //赋空
}
tv_1.setText("播放状态:停止播放"); }
public void pause_onclick(View v)
{
//isPlaying:状态
if (mediaPlayer != null && mediaPlayer.isPlaying())
{
mediaPlayer.pause(); tv_1.setText("播放状态:暂停播放");
}
}
public void exit_onclick(View v)
{
stop_onclick(v); finish();
}
}

效果图:

Android课程—简单的音乐播放器

第二个:用Service实现

可以实现后台播放  不影响其他程序运行  重新进入也不会重新播放歌曲

MusicPlay2.java

package com.hanqi.test2;import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;public class MusicPlay2 extends AppCompatActivity {
TextView tv_1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_play1); tv_1 =(TextView)findViewById(R.id.tv_1); tv_1.setText("播放状态:停止播放"); } public void play_onclick(View v)
{
Intent intent = new Intent(this,MusicPlayService.class); intent.putExtra("action","play"); startService(intent); tv_1.setText("播放状态1:正在播放...");
}
public void stop_onclick(View v)
{
Intent intent = new Intent(this,MusicPlayService.class); intent.putExtra("action","stop"); startService(intent); tv_1.setText("播放状态1:停止播放");
}
public void pause_onclick(View v)
{
Intent intent = new Intent(this,MusicPlayService.class); intent.putExtra("action","pause"); startService(intent); tv_1.setText("播放状态1:暂停播放");
}
public void exit_onclick(View v)
{
stop_onclick(v); finish();
}
}

MusicPlayService.java

package com.hanqi.test2;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;public class MusicPlayService extends Service {
public MusicPlayService() {
} @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
private MediaPlayer mediaPlayer; @Override
public int onStartCommand(Intent intent, int flags, int startId) { //获取意图传递的信息
String action = intent.getStringExtra("action");
switch (action)
{
case "play": if (mediaPlayer == null)
{
mediaPlayer = MediaPlayer.create(this,R.raw.love);
} mediaPlayer.start(); break; case "stop":
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
break;
case "pause": mediaPlayer.pause();
break;
}
return super.onStartCommand(intent, flags, startId);
}
}

效果图:

Android课程—简单的音乐播放器

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,103
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,579
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,427
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,199
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,834
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,917