尚硅谷_杨光福_手机影音总内容

更新时间:2023-04-28 20:01:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

321影音

讲师:杨光福

01_主要内容和项目演示-20

主要内容

1,视频播放器

2,音乐播放器

3,电视直播

4,在浏览器里面播放视频

5,歌词同步

6,产品如何实现盈利

02_项目结构

01_工程创建splash页面,启动页面的布局文件1_splahs页面代码

xmlns:android="64d51833284ac850ad0242a1/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/base_bg">

android:id="@+id/ic_launcher"

android:layout_width="80dip"

android:layout_height="800dip"

android:layout_centerInParent="true"

android:src="@drawable/ic_launcher"/>

android:layout_width="wrap_content"

android:layout_height="100dip"

android:layout_below="@id/ic_launcher"

android:layout_centerHorizontal="true"

android:gravity="center_vertical"

android:orientation="horizontal">

android:layout_width="30dip"

android:layout_height="30dip"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="正在启动中..."

android:textColor="#ffffff"/>

2_SplahsActivity代码

public class SplashActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(64d51833284ac850ad0242a1yout.activity_splash);

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

enterMainActiviti();

}

}, 2000);

}

/**

* 进入主页面,并且关闭当前页面

*/

protected void enterMainActiviti() {

Intent intent = new Intent(this,MainActivity.class);

startActivity(intent);

finish();

}

}

02_基类的标题栏页面

1_创建基类

/**

* 基类

* @author afu

*

*/

public class BaseActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(64d51833284ac850ad0242a1yout.activity_base);

}

}

2_创建布局文件activity_base.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="45dp"

android:background="@drawable/base_titlebar_bg">

android:background="@drawable/button_left_selector"

android:layout_width="45dip"

android:layout_height="45dp"/>

android:layout_width="wrap_content"

android:layout_height="45dp"

android:layout_gravity="center_horizontal"

android:gravity="center_vertical"

android:text="功能列表"

android:textColor="#ffffff"

android:textSize="20sp"/>

android:background="@drawable/button_right_selector"

android:layout_width="45dip"

android:layout_height="45dp"

android:layout_gravity="right"/>

03_进入主页面,并且关闭当前页面

1_创建主页面,并继承BaseActivity

/**

* 主页面

* @author Administrator

*

*/

public class HomeActivity extends BaseActivity {

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

}

}

2_延迟2秒进入主页面并且关闭当前页面

public class SplashActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(64d51833284ac850ad0242a1yout.activity_splash);

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

//进入主页面

enterHome();

}

}, 2000);

}

/**

* 进入主页面

*/

protected void enterHome() {

Intent intent = new Intent(this,HomeActivity.class);

startActivity(intent);

//把当前的启动页面关闭-避免在主页面退出的时候有回调启动页面

finish();

}

}

04_基类的按钮的点击事件和加载子页面

1_设置点击事件

/**

* 加载子页面的布局文件-有子类去实现

* @return

*/

public abstract View getContentView();

/**

* 设置单击事件

*/

private void setOnclickListener() {

btn_left.setOnClickListener(this);

btn_right.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn_left://左边按钮的点击事件

clickLeftButton();

break;

case R.id.btn_right://右边按钮的点击事件

clickRightButton();

break;

}

}

/**

* 处理左边按钮的点击事件

*/

public abstract void clickRightButton();

/**

* 处理右边按钮的点击事件

*/

public abstract void clickLeftButton() ;

2_加载子页面

/**

* 初始化View

*/

private void initView() {

btn_left = (Button) findViewById(R.id.btn_left);

btn_right = (Button) findViewById(R.id.btn_right);

tv_title = (TextView) findViewById(64d51833284ac850ad0242a1_title);

ll_child_content = (LinearLayout)

findViewById(R.id.ll_child_content);

//加载子页面的布局文件

View child = getContentView();

//导入包要导入线程布局-父亲控件是什么就导入什么的包

LayoutParams params = new

LayoutParams(64d51833284ac850ad0242a1youtParams.MATCH_PARENT, -1);

//把子页面的布局文件添加进来

ll_child_content.addView(child , params );

}

05_设置按钮状态和标题的方法

/**

* 设置左边按钮是否显示

* @param visibility

*/

public void setLeftButton(int visibility){

btn_left.setVisibility(visibility);

}

/**

* 设置左边按钮是否显示

* @param visibility

*/

public void setRightButton(int visibility){

btn_right.setVisibility(visibility);

}

/**

* 设置标题内容

* @param title

*/

public void setTitle(String title){

tv_title.setText(title);

}

07_主页面完成

1_布局文件用gridView

android:background="@drawable/base_bg"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/gridview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginLeft="50dip"

android:layout_marginRight="50dip"

android:verticalSpacing="20dip"

android:layout_marginTop="30dip"

android:horizontalSpacing="50dip"

android:numColumns="2"/>

2_home_item.xml内容

android:layout_width="wrap_content"

android:gravity="center_horizontal"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/iv_icon"

android:layout_width="80dip"

android:layout_height="80dip" />

3_代码部分

public class HomeActivity extends BaseActivity {

private int[] ids =

{R.drawable.video,R.drawable.music,64d51833284ac850ad0242a1work,R.drawable.all,R. drawable.live,R.drawable.advert};

private GridView gridview;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setLeftButton(View.GONE);

setTitle("手机影音");

gridview = (GridView) findViewById(R.id.gridview);

gridview.setAdapter(new HomeAdapter());

/**

* 设置点击事件

*/

gridview.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView parent, View view,

int position, long id) {

switch (position) {

case 0:// 视频

Toast.makeText(getApplicationContext(), "点击视频了", 1).show();

break;

default:

Toast.makeText(getApplicationContext(), "改功能还没有实现", 1).show();

break;

}

}

});

}

private class HomeAdapter extends BaseAdapter{

@Override

public int getCount() {

return ids.length;

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return 0;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View view = View.inflate(HomeActivity.this, 64d51833284ac850ad0242a1yout.home_item, null);

ImageView iv = (ImageView) view.findViewById(R.id.iv_icon);

iv.setImageResource(ids[position]);

return view;

}

}

@Override

public void clickRightButton() {

Toast.makeText(this, "右边按钮被点击了", 0).show();

}

@Override

public void clickLeftButton() {

Toast.makeText(this, "左边按钮被点击了", 0).show();

}

@Override

public View getContentView() {

//把布局文件转换View

View view = View.inflate(this, 64d51833284ac850ad0242a1yout.activity_home, null);

return view;

}

}

03_本地视频列表

01_视频列表显示

1_视频列表的布局文件

xmlns:android="64d51833284ac850ad0242a1/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/base_bg">

android:id="@+id/lv_video_list"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/tv_novideo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="没有视频..."

android:textColor="#ffffff"

android:textSize="18sp"

/>

2_得到手机里面的视频和屏幕3MB以下视频

/**

* 得到手机里面的视频媒体扫描器

*/

private void getVideData() {

videoLists = new ArrayList();

// 在子线程去得到视频

new Thread() {

public void run() {

// 查找视频的路径

Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

String[] projection = {

MediaStore.Video.Media.TITLE,

MediaStore.Video.Media.DURATION,

MediaStore.Video.Media.SIZE,

MediaStore.Video.Media.DATA };

Cursor cursor = getContentResolver().query(uri, projection,

null, null, null);

while(cursor.moveToNext()){

VideoItem videoItem = new VideoItem();

String title = cursor.getString(0);//标题

videoItem.setTitle(title);

String duration = cursor.getString(1);//视频总时长

videoItem.setDuration(duration);

String size = cursor.getString(2);//视频文件大小

videoItem.setSize(size);

String data = cursor.getString(3);//视频在sdcard的绝对路径

videoItem.setData(data);

//添加到列表中

videoLists.add(videoItem);

}

cursor.close();

handler.sendEmptyMessage(0);

};

}.start();

}

3_视频显示出来

private Handler handler = new Handler(){

public void handleMessage(android.os.Message msg) {

lv_video_list.setAdapter(new VideoListAdapter());

if(videoLists!=null&&videoLists.size()>0){

tv_novideo.setVisibility(View.GONE);

}else{

tv_novideo.setVisibility(View.VISIBLE);

}

};

};

4_适配器

private class VideoListAdapter extends BaseAdapter{

@Override

public int getCount() {

return videoLists.size();

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

TextView view = new TextView(VideoListActivity.this);

view.setText(videoLists.get(position).toString());

return view;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

}

02_视频列表的Item完成和相关数据处理

1_videolist_item布局文件

xmlns:android="64d51833284ac850ad0242a1/apk/res/android"

android:layout_width="match_parent"

android:layout_height="100dip" >

android:layout_marginTop="10dip"

android:id="@+id/iv_icon"

android:layout_width="50dip"

android:layout_height="50dip"

android:layout_centerVertical="true"

android:layout_marginLeft="5dip"

android:src="@drawable/video_default_icon"/>

android:layout_marginTop="10dip"

android:layout_marginLeft="10dip"

android:id="@+id/tv_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/iv_icon"

android:text="视频的名称"

android:textColor="#ffffff"

android:textSize="20sp"/>

android:layout_marginLeft="10dip"

android:id="@+id/tv_duration"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/tv_name"

android:layout_toRightOf="@id/iv_icon"

android:text="视频的时间"

android:textColor="#ffffff"

android:textSize="14sp"/>

android:id="@+id/tv_size"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:layout_marginRight="5dip"

android:text="视频的时间"

android:textColor="#ffffff"

android:textSize="14sp"/>

android:layout_marginTop="10dip"

android:layout_width="fill_parent"

android:layout_height="0.1dip"

android:layout_alignParentBottom="true"

android:layout_marginLeft="5dip"

android:layout_marginRight="5dip"

android:background="#66ffffff"/>

2_代码实现

private class VideoListAdapter extends BaseAdapter{

@Override

public int getCount() {

return videoLists.size();

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

//把布局文件-->View对象

View view;

ViewHolder holder;

if(convertView != null){

view = convertView;

holder = (ViewHolder) view.getTag();

}else{

view = View.inflate(VideoListActivity.this,

64d51833284ac850ad0242a1yout.videolist_item, null);

holder = new ViewHolder();

64d51833284ac850ad0242a1_name = (TextView)

view.findViewById(64d51833284ac850ad0242a1_name);

64d51833284ac850ad0242a1_duration = (TextView)

view.findViewById(64d51833284ac850ad0242a1_duration);

64d51833284ac850ad0242a1_size = (TextView)

view.findViewById(64d51833284ac850ad0242a1_size);

view.setTag(holder);//把对应的关系保存起来

}

//得到具体的某一条视频的信息

VideoItem videoItem = videoLists.get(position);

64d51833284ac850ad0242a1_name.setText(videoItem.getTitle());

//把毫秒转换成:1:20:30这里形式

String timeStr =

utils.stringForTime(Integer.parseInt(videoItem.getDuration()));

64d51833284ac850ad0242a1_duration.setText(timeStr);

String sizeStr =

Formatter.formatFileSize(VideoListActivity.this,

Long.parseLong(videoItem.getSize()));

64d51833284ac850ad0242a1_size.setText(sizeStr);

return view;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

}

3_用到工具类

import java.util.Formatter;

import java.util.Locale;

public class Utils {

private StringBuilder mFormatBuilder;

private Formatter mFormatter;

public Utils() {

// 转换成字符串的时间

mFormatBuilder = new StringBuilder();

mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

}

/**

* 把毫秒转换成:1:20:30这里形式

* @param timeMs

* @return

*/

public String stringForTime(int timeMs) {

int totalSeconds = timeMs / 1000;

int seconds = totalSeconds % 60;

int minutes = (totalSeconds / 60) % 60;

int hours = totalSeconds / 3600;

mFormatBuilder.setLength(0);

if (hours > 0) {

return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds)

.toString();

} else {

return mFormatter.format("%02d:%02d", minutes, seconds).toString();

}

}

}

03_快速实现简单播放器

1_布局文件

xmlns:android="64d51833284ac850ad0242a1/apk/res/android"

android:layout_width="match_parent"

android:background="#000000"

android:layout_height="match_parent">

android:layout_centerHorizontal="true"

android:id="@+id/videoview"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

2_在基类设置方法隐藏标题栏

/**

* 设置隐藏基类标题栏

* @param visibility

*/

public void setBaseBar(int visibility){

fl_basebar.setVisibility(visibility);

}

3_播放器代码VideoPlayerActivity

public class VideoPlayerActivity extends BaseActivity {

private VideoView videoview;

private Uri uri;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setBaseBar(View.GONE);

videoview = (VideoView) findViewById(R.id.videoview);

//得到播放地址

uri = getIntent().getData();

videoview.setVideoURI(uri);

//监听准备好了

videoview.setOnPreparedListener(new OnPreparedListener() {

@Override

public void onPrepared(MediaPlayer mp) {

// 开发播放

videoview.start();

}

});

//设置控制面板

videoview.setMediaController(new MediaController(this));

}

@Override

public View getContentView() {

return View.inflate(VideoPlayer.this,

64d51833284ac850ad0242a1yout.activity_videoplayer, null);

}

@Override

public void clickRightButton() {

// TODO Auto-generated method stub

}

@Override

public void clickLeftButton() {

// TODO Auto-generated method stub

}

}

4_点击一个视频播放起来

VideoItem videoItem = videoLists.get(position);

String path = videoItem.getData();

Intent intent = new

Intent(VideoListActivity.this,VideoPlayerActivity.class);

intent.setData(Uri.parse(path));

startActivity(intent);

04_屏幕各个Activity页面横竖屏切换执行onCreate方法

在功能清单文件的每个Activity配置

android:name="com.atguigu.mobileplayer.video.VideoPlayerActivity"

android:configChanges="keyboardHidden|screenSize|orientation"

android:screenOrientation="landscape"/>

04_播放器的实现

01_播放器控制栏顶部

1_布局文件

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/bg_player_status"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dip"

android:layout_weight="1"

android:ellipsize="end"

android:singleLine="true"

android:text="视频名称"

android:textColor="#ffffff"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:src="@drawable/ic_battery_10"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="right"

android:layout_marginRight="10dp"

android:ellipsize="end"

android:gravity="right|center_vertical"

android:singleLine="true"

android:text="系统时间"

android:textColor="#ffffff"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/bg_player_top_control"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/btn_voice_selector"/>

android:id="@+id/seekBar1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:layout_weight="1"

android:max="100"

android:progress="0"

android:minHeight="6dip"

android:maxHeight="6dip"

android:progressDrawable="@drawable/progress_horizontal"

android:thumb="@drawable/progress_thumb"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="10dip"

android:background="@drawable/btn_switch_selector"/>

02_播放器控制栏底部的完成

1_底部布局文件如下

xmlns:android="64d51833284ac850ad0242a1/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#000000">

android:id="@+id/videoview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerHorizontal="true"/>

本文来源:https://www.bwwdw.com/article/i16q.html

Top