android实习程序——音乐播放器(简单播放)

做一下知识点的准备
android四大组件
actvity
service
广播接收者
内容提供者
 
 
MVC
activity c
View     v
 
界面是如何呈现的?
Window
通过getWindow()方法来获得窗口
 
将view让入窗口呈现
setContentView(view);
 
xml是一种数据的封装格式
字符串
如何把字符串转换成view对象
反射技术--解析xml
 
在android中是用什么解析的
Inflater
用LayoutInflater.from(this)来获取Inflater
 
用inflate(R.layout.main, null);
 
-----------------------
在自定义view时需要传入一个应用程序上下文对象
 
书---
书的封面--作者,出版社,发行日期。。。。。。
 
我在给你书的时候只撕给你书中的一页纸,你能得到这些信息吗?
 
把书页和封面全部装订在一起就行
 
组合
---------------------------
如何给你张白纸,你如何将它变成美丽的画
 
 
在android中的界面就是画出来的
onDraw
 
Canvas画布
Paint画笔
 
所有的类名首字母大写
 
总结:
activity
window
View
三者之间的关系?
 
Inflater的作用?
 
Context的作用?
 
如何自定义view?
 
---------------------------
 
Adapter的作用
 
做软件就是要进行数据的处理
处理完了还要展示给用户
 
在写adapter要关注的两点
数据源
界面
ArrayAdapter()
SimpleAdapter()
 
---------------------------
setContentView(R.layout.main)有什么缺点
它用的是反射,一定会消耗资源,性能不好,不适合需要高性能的情况,也不适合复杂界面
 
游戏
 
2个项目
1108班学员做的
简单通讯录
五子棋
---------------------------
要有良好的java基础
空指针
java是面向对象的设计语言
是靠对象和类来调用方法来完成业务逻辑的。
 
service本身是和Activity在同一线程的
如果要使service能执行异步的操作必须在service中开启新的线程。
 
那问题来了,为什么不在Activity中直接开启线程,而要用到service。
 
因为service的级别要高,不容易被系统杀死。
 
 
android中的进程等级
Foreground process 前台进程
Visible process 可见进程
Service process 服务进程
Background process 后台进程
Empty process 空进程
 
 
android在系统资源不够时会自动杀死某些进程
 
Service有两种启动方式
startService--无需交互自动执行时使用
bindService--需要进行交互时
 
ServiceConnection服务连接器
 
创建一个服务
 
在onStart方法中建立于服务的连接
绑定服务
bindService(_Intent,
     conn, 
     BIND_AUTO_CREATE);
 
连接器有两个方法
一个在服务连接时掉用
传入一个IBinder用于我们连接服务的钥匙
 
服务连接后会调用IBinder方法
在这个方法中创建媒体播放器
MediaPlayer.create(this, R.raw.hetang);
将钥匙返回去
钥匙的功能是将服务本身返回
供外部使用
 
在Activiy接收服务对象
从钥匙中拿到我们要的对象
((MyBinder)service).getKey()
 
得到了服务的实例以后我们就可以使用服务中的方法,来调动媒体播放器了
 
 
!!!!
得到服务的目的就是要调用服务的方法来控制音乐的播放
!!!!
 
记得在onDestroy中要解除绑定
 
一个在服务断开连接时调用
 
======================================
音乐播放器
MusicMediaActivity.java
package com.tarena.Music;
 
import com.tarena.Music.MusicService.MyBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
 
public class MusicMediaActivity extends Activity {
    private ImageButton mButton;
    private MusicService mMusicService;
    
    private ServiceConnection conn=new ServiceConnection() {
 
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
 
}
 
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//从钥匙中拿出服务对象
mMusicService=((MyBinder)service).getKey();
}
};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton=(ImageButton)findViewById(R.id.pause);
        mButton.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
mMusicService.start();
}
});
    }
    
    @Override
    protected void onStart() {
     super.onStart();
     Intent _Intent = 
     new Intent(this,MusicService.class);
     //三个参数
     //第一个是Intent
     //第二个是服务连接器,用于和服务的连接
     //第三个是标志位,用于开启服务的方式
     //BIND_AUTO_CREATE表示绑定时自动连接
     bindService(_Intent,
     conn, 
     BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onDestroy() {
     unbindService(conn);
     super.onDestroy();
    }
}
-----------------------------------------------
musicService.java
package com.tarena.Music;
 
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
 
public class MusicService extends Service {
private MediaPlayer mMediaPlayer;
@Override
public IBinder onBind(Intent intent) {
mMediaPlayer= MediaPlayer.create(this, R.raw.hetang);
Log.i("MyLog","服务已绑定");
return new MyBinder();
}
 
/**
 * 封装连接服务的钥匙类
 * 这个类的功能就是把服务类暴露给外部使用。
 * 
 * @author Administrator
 *
 */
class MyBinder extends Binder{
/**
 * @return将服务本身这个实例返回
 */
public MusicService getKey(){
return MusicService.this;
}
}
 
/**
 * 音乐开始
 */
public void start(){
if (!mMediaPlayer.isPlaying()) {
Log.i("MyLog","music start");
mMediaPlayer.start();
 
}
}
//暂停
//停止
 
}
 
--------------------------------------
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/background">
    
    <ImageButton
    android:id="@+id/initImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/init"  
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>
    
    
       <ListView
        android:id="@+id/music_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_marginBottom="70dp"
        android:layout_below="@id/initImage"
        >
    </ListView> 
         
 
       <SeekBar
        android:id="@+id/SeekBar1"       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout"/>
 
    <LinearLayout
           android:id="@+id/linearLayout"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal" 
           android:layout_alignParentBottom="true"
           android:gravity="bottom"
           android:layout_marginLeft="54dp"
          android:layout_marginRight="48dp"
          android:layout_marginBottom="4dp"
           >
     
<ImageButton
android:id="@+id/prev"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/prev"  
    android:gravity="left"
    android:layout_marginRight="40dp"/>
 
    <ImageButton
    android:id="@+id/pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pause"  
    android:layout_marginRight="40dp"/>
      
    <ImageButton
    android:id="@+id/next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/next"  
android:gravity="right"
android:layout_gravity="right"/>
           
       </LinearLayout>
 
 
 
</RelativeLayout>
 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值